Skip to content

Commit

Permalink
Tidying.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich2 committed Oct 14, 2018
1 parent d4ea750 commit 17e451f
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 154 deletions.
6 changes: 4 additions & 2 deletions Core/src/ostrat/geom/Angle.scala
Expand Up @@ -13,20 +13,22 @@ trait AngleLike extends Any
def arcDistance (radiusDist: Dist): Dist = radians * radiusDist
}

/** Angle value class. Its particularly important not to use this class to represent Latitudes as they are between +- 180 degrees. */
/** Angle value class. Its particularly important not to use this class to represent Latitudes as the Angle class has a normal range +- 180 degrees,
* while Latitudes have a normal range +- 90 degrees. */
final class Angle private(val radians: Double) extends AnyVal with AngleLike
{ override def toString = degStr2
def degStr2: String = degs.str2 -"\u00B0"
def toVec2: Vec2 = Vec2(math.cos(radians), math.sin(radians))
def radians360: Double = ife(radians < 0, Pi2 - radians, radians)
def +(other: Angle) = Angle(radians + other.radians)

/** returns an angle between -Pi and Pi */
def angleTo(other: Angle): Angle = other.radians -radians match
{ case r if r > Pi => Angle(r - Pi2)
case r if r < -Pi => Angle(Pi2 + r)
case r => Angle(r)
}

def addRadians(other: Double) = Angle(radians + other)
def subRadians(other: Double) = Angle(radians - other)
def * (factor: Double): Angle = Angle(radians * factor)
Expand Down
6 changes: 3 additions & 3 deletions Core/src/ostrat/geom/Arc.scala
Expand Up @@ -3,7 +3,7 @@ package ostrat
package geom
import Colour.Black

/** Super trait to Arc and ArcDraw */
/** Super trait to Arc and ArcDraw and Arc fill which has not been implemented yet. */
trait ArcLike extends CurveLike
{ def xCen: Double
def yCen: Double
Expand All @@ -30,12 +30,13 @@ case class Arc(xStart: Double, yStart: Double, xCen: Double, yCen: Double, xEnd:
def fTrans(f: Vec2 => Vec2): Arc = Arc(f(pStart), f(pCen), f(pEnd))
}

/** The companion object for the Arc class. */
object Arc
{
def apply(pStart: Vec2, pCen: Vec2, pEnd: Vec2): Arc = new Arc(pStart.x, pStart.y, pCen.x, pCen.y, pEnd.x, pEnd.y)
}

/** A funtional paint element to Draw an Arc */
/** A functional paint element to Draw an Arc. Defined by the arc, the line width, the colour and the layer. */
case class ArcDraw(xStart: Double, yStart: Double, xCen: Double, yCen: Double, xEnd: Double, yEnd: Double, lineWidth: Double, colour: Colour,
layer: Int) extends PaintElem[ArcDraw] with ArcLike
{ def typeSym = 'ArcDraw
Expand All @@ -49,4 +50,3 @@ object ArcDraw
def apply(pStart: Vec2, pCen: Vec2, pEnd: Vec2, lineWidth: Double = 1.0, colour: Colour = Black, layer: Int = 0): ArcDraw =
new ArcDraw(pStart.x, pStart.y, pCen.x, pCen.y, pEnd.x, pEnd.y, lineWidth, colour, layer)
}

2 changes: 1 addition & 1 deletion Core/src/ostrat/geom/BoundingRect.scala
Expand Up @@ -2,7 +2,7 @@
package ostrat
package geom

/** An intermediate class for describing the bounding rectangle for a Polygon or Shape. */
/** An intermediate class for describing the vertical / horisontal bounding rectangle for a Polygon or Shape. Defined by 4 Double values. */
case class BoundingRect(minX: Double, maxX: Double, minY: Double, maxY: Double)
{ def topLeft = Vec2(minX, maxY)
def topRight = Vec2(maxX, maxY)
Expand Down
3 changes: 3 additions & 0 deletions Core/src/ostrat/geom/CurveSeg.scala
Expand Up @@ -95,16 +95,19 @@ ProdD7 with Transable[CurveSeg] with CurveSegLike
}
}

/** This provides factory methods to create a LineSeg. There is no independent LineSeg class. This is one of 3 factory objects to CurveSeg. */
object LineSeg
{ def apply(pEnd: Vec2): CurveSeg = new CurveSeg(10, 0, 0, 0, 0, pEnd.x, pEnd.y)
def apply(xEnd: Double, yEnd: Double): CurveSeg = new CurveSeg(10, 0, 0, 0, 0, xEnd, yEnd)
}

/** This provides factory methods to create an ArcSeg. There is no independent ArcSeg class. This is one of 3 factory objects to CurveSeg. */
object ArcSeg
{ def apply(pCen: Vec2, pEnd: Vec2): CurveSeg = new CurveSeg(11, 0, 0, pCen.x, pCen.y, pEnd.x, pEnd.y)
//def apply(xCen: Double, yCen: Double, xEnd: Double, yEnd: Double): CurveSeg = new CurveSeg(PosInf, 0, xCen, yCen, xEnd, yEnd)
}

/** This provides factory methods to create a BezierSeg. There is no independent BezierSeg class. This is one of 3 factory objects to CurveSeg. */
object BezierSeg
{ def apply(pC1: Vec2, pC2: Vec2, pEnd: Vec2): CurveSeg = new CurveSeg(12, pC1.x, pC1.y, pC2.x, pC2.y, pEnd.x, pEnd.y)
//def apply(xC1: Double, yC1: Double, xC2: Double, yC2: Double, xEnd: Double, yEnd: Double): CurveSeg = new CurveSeg(xC1, yC1, xC2, yC2, xEnd, yEnd)
Expand Down
1 change: 1 addition & 0 deletions Core/src/ostrat/geom/Dist3.scala
Expand Up @@ -38,6 +38,7 @@ final class Dist3(val xMetres: Double, val yMetres: Double, val zMetres: Double)
}
}

/** Companion object for the Dist3 class. */
object Dist3
{
def metres(xMetres: Double, yMetres: Double, zMetres: Double): Dist3 = new Dist3(xMetres, yMetres, zMetres)
Expand Down
26 changes: 13 additions & 13 deletions Core/src/ostrat/geom/Square.scala
Expand Up @@ -2,21 +2,21 @@
package ostrat
package geom

/** Factory object for squares. There is no companon Square class. */
object Square
{
def apply(width: Double, cen: Vec2 = Vec2Z): Polygon = apply(width, cen.x, cen.y)
def apply(width: Double, xCen: Double, yCen: Double): Polygon = Polygon(
xCen - width / 2 vv yCen + width / 2,
xCen + width / 2 vv yCen + width / 2,
xCen + width / 2 vv yCen - width / 2,
xCen - width/2 vv yCen - width / 2)
def apply(width: Double, cen: Vec2 = Vec2Z): Polygon = apply(width, cen.x, cen.y)
def apply(width: Double, xCen: Double, yCen: Double): Polygon = Polygon(
xCen - width / 2 vv yCen + width / 2,
xCen + width / 2 vv yCen + width / 2,
xCen + width / 2 vv yCen - width / 2,
xCen - width/2 vv yCen - width / 2)

def fill(width: Double, colour: Colour, cen: Vec2 = Vec2Z, layer: Int = 0) = apply(width, cen.x, cen.y).fill(colour, layer)
def fill(width: Double, colour: Colour, xCen: Double, yCen: Double) = apply(width, xCen, yCen).fill(colour)
def fill(width: Double, colour: Colour, cen: Vec2 = Vec2Z, layer: Int = 0): PolyFill = apply(width, cen.x, cen.y).fill(colour, layer)
def fill(width: Double, colour: Colour, xCen: Double, yCen: Double): PolyFill = apply(width, xCen, yCen).fill(colour)

def curvedSegs(width: Double, radius: Double): List[CurveSeg] =
{
val w = width / 2
(0 to 3).toList.flatMap(i => List( LineSeg(w - radius, w), ArcSeg(w - radius vv w -radius, w vv w - radius)).rotateRadians(-i * math.Pi / 2))
}
def curvedSegs(width: Double, radius: Double): List[CurveSeg] =
{ val w = width / 2
(0 to 3).toList.flatMap(i => List( LineSeg(w - radius, w), ArcSeg(w - radius vv w -radius, w vv w - radius)).rotateRadians(-i * math.Pi / 2))
}
}
28 changes: 12 additions & 16 deletions Core/src/ostrat/geom/TransDistable.scala
Expand Up @@ -6,21 +6,17 @@ package geom
* trait Transable does the same for fTrans(f: Vec2 => Vec2): T. */
trait TransDistable[T] extends Any
{
def fTrans(f: Dist2 => Dist2): T
def slate(offset: Dist2): T = fTrans(_ + offset)
def slate(xOffset: Dist, yOffset: Dist): T = fTrans(_.addXY(xOffset, yOffset))
def slateX(xOffset: Dist): T = fTrans(_.addX(xOffset))
def slateY(yOffset: Dist): T = fTrans(_.addY(yOffset))
def scale(factor: Double): T = fTrans(_ * factor)
def fTrans(f: Dist2 => Dist2): T
def slate(offset: Dist2): T = fTrans(_ + offset)
def slate(xOffset: Dist, yOffset: Dist): T = fTrans(_.addXY(xOffset, yOffset))
def slateX(xOffset: Dist): T = fTrans(_.addX(xOffset))
def slateY(yOffset: Dist): T = fTrans(_.addY(yOffset))
def scale(factor: Double): T = fTrans(_ * factor)

def rotate(angle: Angle): T = fTrans(_.rotate(angle))
def rotateRadians(r: Double): T = fTrans(_.rotateRadians(r))
//def scaleY(factor: Double): T = fTrans(_.scaleY(factor))
/** this.asInstanceOf[T] */
def identity: T = this.asInstanceOf[T]
//def mirrorX: T = fTrans(_.mirrorX)
//def mirrorY: T = fTrans(_.mirrorY)
//def mirror4: List[T] = List(fTrans(v => v), fTrans(_.mirrorX), fTrans(_.mirrorY), fTrans(- _))
//def withNegate: Seq[T] = Seq(identity, fTrans(- _))
def inverseY: T = fTrans(v => Dist2(v.x, -v.y))
def rotate(angle: Angle): T = fTrans(_.rotate(angle))
def rotateRadians(r: Double): T = fTrans(_.rotateRadians(r))

/** this.asInstanceOf[T] */
def identity: T = this.asInstanceOf[T]
def inverseY: T = fTrans(v => Dist2(v.x, -v.y))
}
146 changes: 44 additions & 102 deletions Core/src/ostrat/geom/Transable.scala
Expand Up @@ -16,112 +16,54 @@ trait Transable[T] extends Any
def rotate(angle: Angle): T = fTrans(_.rotate(angle))
def rotateRadians(r: Double): T = fTrans(_.rotateRadians(r))
def scaleY(factor: Double): T = fTrans(_.scaleY(factor))
/** this.asInstanceOf[T] */
def identity: T = this.asInstanceOf[T]
def mirrorX: T = fTrans(_.mirrorX)
def mirrorY: T = fTrans(_.mirrorY)
def mirror4: List[T] = List(fTrans(v => v), fTrans(_.mirrorX), fTrans(_.mirrorY), fTrans(- _))
def withNegate: Seq[T] = Seq(identity, fTrans(- _))
def inverseY: T = fTrans(v => Vec2(v.x, -v.y))
/** this.asInstanceOf[T] */
def identity: T = this.asInstanceOf[T]
def mirrorX: T = fTrans(_.mirrorX)
def mirrorY: T = fTrans(_.mirrorY)
def mirror4: List[T] = List(fTrans(v => v), fTrans(_.mirrorX), fTrans(_.mirrorY), fTrans(- _))
def withNegate: Seq[T] = Seq(identity, fTrans(- _))
def inverseY: T = fTrans(v => Vec2(v.x, -v.y))



import math.Pi
/** Rotates 30 degrees anti-clockwise or + Pi/3 */
def anti30: T = rotate(Angle(Pi / 6))
/** Rotates 45 degrees anti-clockwise or + Pi/4 */
def anti45: T = rotate(Angle(Pi / 4))
/** Rotates 60 degrees anti-clockwise or + Pi/3 */
def anti60: T = rotate(Angle(Pi / 3))
/** Rotates 90 degrees anti-clockwise or + Pi/2 */
def anti90: T = rotate(Angle(Pi / 2))
/** Rotates 120 degrees anti-clockwise or + 2 * Pi/3 */
def anti120: T = rotate(Angle(2 * Pi / 3))
/** Rotates 135 degrees anti-clockwise or + 3 * Pi/4 */
def anti135: T = rotate(Angle(3 * Pi / 4))
/** Rotates 150 degrees anti-clockwise or + 5 * Pi/6 */
def anti150: T = rotate(Angle(5 * Pi / 6))
import math.Pi
/** Rotates 30 degrees anti-clockwise or + Pi/3 */
def anti30: T = rotate(Angle(Pi / 6))
/** Rotates 45 degrees anti-clockwise or + Pi/4 */
def anti45: T = rotate(Angle(Pi / 4))
/** Rotates 60 degrees anti-clockwise or + Pi/3 */
def anti60: T = rotate(Angle(Pi / 3))
/** Rotates 90 degrees anti-clockwise or + Pi/2 */
def anti90: T = rotate(Angle(Pi / 2))
/** Rotates 120 degrees anti-clockwise or + 2 * Pi/3 */
def anti120: T = rotate(Angle(2 * Pi / 3))
/** Rotates 135 degrees anti-clockwise or + 3 * Pi/4 */
def anti135: T = rotate(Angle(3 * Pi / 4))
/** Rotates 150 degrees anti-clockwise or + 5 * Pi/6 */
def anti150: T = rotate(Angle(5 * Pi / 6))

/** Rotates 30 degrees clockwise or - Pi/3 */
def clk30: T = rotate(Angle(-Pi / 6))
/** Rotates 45 degrees clockwise or - Pi/4 */
def clk45: T = rotate(Angle(-Pi / 4))
/** Rotates 60 degrees clockwise or - Pi/3 */
def clk60: T = rotate(Angle(-Pi / 3))
/** Rotates 90 degrees clockwise or - Pi / 2 */
def clk90: T = rotate(Angle(-Pi / 2))
/** Rotates 120 degrees clockwise or - 2 * Pi/3 */
def clk120: T = rotate(Angle(-2 * Pi / 3))
/** Rotates 135 degrees clockwise or - 3 * Pi/ 4 */
def clk135: T = rotate(Angle(-3 * Pi / 4))
/** Rotates 150 degrees clockwise or - 5 * Pi/ 6 */
def clk150: T = rotate(Angle(-5 * Pi / 6))
/** Rotates 30 degrees clockwise or - Pi/3 */
def clk30: T = rotate(Angle(-Pi / 6))
/** Rotates 45 degrees clockwise or - Pi/4 */
def clk45: T = rotate(Angle(-Pi / 4))
/** Rotates 60 degrees clockwise or - Pi/3 */
def clk60: T = rotate(Angle(-Pi / 3))
/** Rotates 90 degrees clockwise or - Pi / 2 */
def clk90: T = rotate(Angle(-Pi / 2))
/** Rotates 120 degrees clockwise or - 2 * Pi/3 */
def clk120: T = rotate(Angle(-2 * Pi / 3))
/** Rotates 135 degrees clockwise or - 3 * Pi/ 4 */
def clk135: T = rotate(Angle(-3 * Pi / 4))
/** Rotates 150 degrees clockwise or - 5 * Pi/ 6 */
def clk150: T = rotate(Angle(-5 * Pi / 6))

/** Produces a regular cross of a sequence of four of the elements rotated */
def rCross: Seq[T] = (1 to 4).map(i => rotate(deg90 * i))
/** Produces a regular cross of a sequence of four of the elements rotated */
def rCross: Seq[T] = (1 to 4).map(i => rotate(deg90 * i))
}

object Transable
{
// import scala.collection.mutable.{ Builder }
// import scala.collection._
implicit class ImplictTransableList[TT <: Transable[_ ]](tList: List[TT]) extends Transable[List[TT]]
{
def fTrans(f: Vec2 => Vec2): List[TT] = tList.map(_.fTrans(f).asInstanceOf[TT])
def flatMirror4: List[TT] = tList.flatMap(_.mirror4.asInstanceOf[Seq[TT]])
//def flatWithNegate: List[TT] = tList.flatMap(_.withNegate.asInstanceOf[Seq[TT]])
}

// implicit class ImplicitTransableTrav[TransT <: Transable[TransT], Repr](travLike: TraversableLike[TransT, Repr])// extends Traversable[Repr]
// {
//
// def fTrans(f: Vec2 => Vec2)(implicit bf: generic.CanBuildFrom[Repr, TransT, Repr]): Repr =
// {
// def builder: Builder[TransT,Repr] =
// { // extracted to keep method size under 35 bytes, so that it can be JIT-inlined
// val b = bf(travLike.repr)
// b.sizeHint(travLike)
// b
// }
// val b = builder
// for (transable <- travLike) b += transable.fTrans(f)
// b.result
// }
// def slate (offset: Vec2)(implicit bf: generic.CanBuildFrom[Repr, TransT, Repr]): Repr =
// {
// def builder: Builder[TransT, Repr] =
// { // extracted to keep method size under 35 bytes, so that it can be JIT-inlined
// val b = bf(travLike.repr)
// b.sizeHint(travLike)
// b
// }
// val b = builder
// for (transable <- travLike) b += transable.slate(offset)
// b.result
// }
// def slate(xOff: Double, yOff: Double)(implicit bf: generic.CanBuildFrom[Repr, TransT, Repr]): Repr =
// {
// def builder =
// { // extracted to keep method size under 35 bytes, so that it can be JIT-inlined
// val b = bf(travLike.repr)
// b.sizeHint(travLike)
// b
// }
// val b = builder
// for (transable <- travLike) b += transable.slate(xOff, yOff)
// b.result
// }
// def scale(factor: Double)(implicit bf: generic.CanBuildFrom[Repr, TransT, Repr]): Repr =
// {
// def builder =
// { // extracted to keep method size under 35 bytes, so that it can be JIT-inlined
// val b = bf(travLike.repr)
// b.sizeHint(travLike)
// b
// }
// val b = builder
// for (transable <- travLike) b += transable.scale(factor)
// b.result
// }
// }
}
implicit class ImplictTransableList[TT <: Transable[_ ]](tList: List[TT]) extends Transable[List[TT]]
{
def fTrans(f: Vec2 => Vec2): List[TT] = tList.map(_.fTrans(f).asInstanceOf[TT])
def flatMirror4: List[TT] = tList.flatMap(_.mirror4.asInstanceOf[Seq[TT]])
}
}
25 changes: 13 additions & 12 deletions Core/src/ostrat/geom/Trapezium.scala
Expand Up @@ -2,24 +2,25 @@
package ostrat
package geom

object Trapezium {

/** Probably worth keeping */
object Trapezium
{
}

object TrapezoidIsosceles
{
def apply(baseWidth: Double, topWidth: Double, height: Double): Polygon =
Polygon(-topWidth /2 vv height/2,
topWidth/2 vv height / 2,
baseWidth/2 vv - height/2,
- baseWidth/2 vv - height/2)
def apply(baseWidth: Double, topWidth: Double, height: Double): Polygon = Polygon(
-topWidth /2 vv height/2,
topWidth/2 vv height / 2,
baseWidth/2 vv - height/2,
- baseWidth/2 vv - height/2)
}

object Diamond
{
def apply() = Polygon(
0 vv 0.5,
Tan30 / 2 vv 0,
0 vv - 0.5,
-Tan30/2 vv 0)
def apply() = Polygon(
0 vv 0.5,
Tan30 / 2 vv 0,
0 vv - 0.5,
-Tan30/2 vv 0)
}
3 changes: 3 additions & 0 deletions Core/src/ostrat/geom/Vec2sLike.scala
Expand Up @@ -20,6 +20,8 @@ trait Vec2sLike extends Any
}
}

/** Array[Double] based collection class for Vec2s which doesn't reperesent a closed Polygon. Conversion to and form the Polygon class should not
* entail a runtime cost. */
class Vec2s(val arr: Array[Double]) extends AnyVal with Transable[Vec2s] with Vec2sLike
{
@inline def lengthFull: Int = arr.length / 2
Expand All @@ -41,6 +43,7 @@ class Vec2s(val arr: Array[Double]) extends AnyVal with Transable[Vec2s] with Ve
def draw(lineWidth: Double, colour: Colour = Colour.Black): Vec2sDraw = Vec2sDraw(this, lineWidth, colour)
}

/** Companion object for the Vec2s collection class. */
object LineSegs
{
def apply(pStart: Vec2, pEnds: Vec2 *): Vec2s =
Expand Down
1 change: 1 addition & 0 deletions Core/src/ostrat/geom/Vec3.scala
Expand Up @@ -2,6 +2,7 @@
package ostrat
package geom
import math._

/** A 3 dimensional vector, can be used to represent 3 dimensional points and translations of 3 dimensional points. Right-handed coordinate
* system is the default */
final class Vec3 (val x: Double, val y: Double, val z: Double) extends ProdD3 with Stringer
Expand Down
4 changes: 2 additions & 2 deletions Core/src/ostrat/pCanv/CanvasPlatform.scala
Expand Up @@ -20,8 +20,8 @@ trait CanvasPlatform extends RectGeom
def clip(pts: Polygon): Unit
/** Returns the time in milliseconds */
def getTime: Double
/** A callback timer with an elapsed time from a given start point. The function is of form: (elapsedTime(in milliseconds), Startime(in millseconds)
* => Unit. The startTime is to be used to call the next frame at then end of the function, if another frame is needed */
/** A callback timer with an elapsed time from a given start point. The function is of form: (elapsedTime(in milliseconds), Startime (in
* milliseconds) => Unit. The startTime is to be used to call the next frame at then end of the function, if another frame is needed */
def frame(f: (Double, Double) => Unit, startTime: Double, frameLength: Integer = 15): Unit =
timeOut(() => f(getTime - startTime, startTime), frameLength)
def frameZero(f: (Double, Double) => Unit, frameLength: Integer = 15): Unit = frame(f, getTime)
Expand Down

0 comments on commit 17e451f

Please sign in to comment.