diff options
Diffstat (limited to 'src/main/scala/sims/util')
-rw-r--r-- | src/main/scala/sims/util/Polar.scala | 17 | ||||
-rw-r--r-- | src/main/scala/sims/util/Positioning.scala | 24 | ||||
-rw-r--r-- | src/main/scala/sims/util/RelativeVector.scala | 24 |
3 files changed, 65 insertions, 0 deletions
diff --git a/src/main/scala/sims/util/Polar.scala b/src/main/scala/sims/util/Polar.scala new file mode 100644 index 0000000..494c984 --- /dev/null +++ b/src/main/scala/sims/util/Polar.scala @@ -0,0 +1,17 @@ +/* + * Simple Mechanics Simulator (SiMS) + * copyright (c) 2009 Jakob Odersky + * made available under the MIT License +*/ + +package sims.util + +import sims.geometry._ +import scala.math._ + +/**Polar coordinates.*/ +case class Polar(distance: Double, angle: Double) { + + /**Returns the vector representation of these polar coordinates.*/ + def toCarthesian = Vector2D(distance * sin(angle), distance * cos(angle)) +} diff --git a/src/main/scala/sims/util/Positioning.scala b/src/main/scala/sims/util/Positioning.scala new file mode 100644 index 0000000..cf72276 --- /dev/null +++ b/src/main/scala/sims/util/Positioning.scala @@ -0,0 +1,24 @@ +/* + * Simple Mechanics Simulator (SiMS) + * copyright (c) 2009 Jakob Odersky + * made available under the MIT License +*/ + +package sims.util + +import sims.geometry._ +import sims.dynamics._ + +/**Utility functions for comfortable positioning of bodies.*/ +object Positioning { + + implicit def int2RelativeVector(x: Int): RelativeVector = new RelativeVector(x, 0) + implicit def double2RelativeVector(x: Double): RelativeVector = new RelativeVector(x, 0) + implicit def vector2RelativeVector(v: Vector2D): RelativeVector = new RelativeVector(v.x, v.y) + implicit def polar2Carthesian(p: Polar): Vector2D = p.toCarthesian + implicit def polar2RelativeVector(p: Polar): RelativeVector = vector2RelativeVector(p.toCarthesian) + + def position(s: Shape)(a: Vector2D) = { + s.pos = a + } +} diff --git a/src/main/scala/sims/util/RelativeVector.scala b/src/main/scala/sims/util/RelativeVector.scala new file mode 100644 index 0000000..374f92c --- /dev/null +++ b/src/main/scala/sims/util/RelativeVector.scala @@ -0,0 +1,24 @@ +/* + * Simple Mechanics Simulator (SiMS) + * copyright (c) 2009 Jakob Odersky + * made available under the MIT License +*/ + +package sims.util + +import sims.geometry._ +import sims.dynamics._ + +class RelativeVector(val x: Double, val y: Double) { + def above(point: Vector2D): Vector2D = point + Vector2D(0, x) + def below(point: Vector2D): Vector2D = point - Vector2D(0, x) + def left(point: Vector2D): Vector2D = point - Vector2D(x, 0) + def right(point: Vector2D): Vector2D = point + Vector2D(x, 0) + def from(point: Vector2D): Vector2D = point + Vector2D(x, y) + + def above(s: Shape): Vector2D = this.above(s.pos) + def below(s: Shape): Vector2D = this.below(s.pos) + def left(s: Shape): Vector2D = this.left(s.pos) + def right(s: Shape): Vector2D = this.right(s.pos) + def from(s: Shape): Vector2D = this.from(s.pos) +} |