Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mathutil {
- class Vector2D protected[mathutil] (val x: Double, val y: Double) {
- val length: Double = math.sqrt(x * x + y * y)
- def unary_- : Vector2D = new Vector2D(-x, -y)
- def normalize: Vector2D = new Vector2D(x / length, y / length)
- /** Returns the normal vector */
- def normal: Vector2D = new Vector2D(-y, x)
- def +(that: Vector2D): Vector2D = new Vector2D(x + that.x, y + that.y)
- def -(that: Vector2D): Vector2D = new Vector2D(x - that.x, y - that.y)
- def *(scalar: Double): Vector2D = new Vector2D(x * scalar, y * scalar)
- def /(scalar: Double): Vector2D = new Vector2D(x / scalar, y / scalar)
- /** Returns the scalar product */
- def *(that: Vector2D): Double = x * that.x + y * that.y
- /** Returns the angle in degrees */
- def \/(that: Vector2D): Double =
- math.toDegrees(math.acos((this * that) / (length * that.length)))
- }
- object Position {
- def apply(that: Vector2D): Vector2D = that
- def apply(x: Double, y: Double): Vector2D = new Vector2D(x, y)
- }
- object Direction {
- def apply(that: Vector2D): Vector2D = apply(that.x, that.y)
- def apply(x: Double, y: Double): Vector2D = new Vector2D(x, y).normalize
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement