Advertisement
Guest User

Untitled

a guest
Feb 26th, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.22 KB | None | 0 0
  1. package mathutil {
  2.   class Vector2D protected[mathutil] (val x: Double, val y: Double) {
  3.     val length: Double = math.sqrt(x * x + y * y)
  4.  
  5.     def unary_-  : Vector2D = new Vector2D(-x, -y)
  6.     def normalize: Vector2D = new Vector2D(x / length, y / length)
  7.  
  8.     /** Returns the normal vector */
  9.     def normal: Vector2D = new Vector2D(-y, x)
  10.  
  11.     def +(that: Vector2D): Vector2D = new Vector2D(x + that.x, y + that.y)
  12.     def -(that: Vector2D): Vector2D = new Vector2D(x - that.x, y - that.y)
  13.     def *(scalar: Double): Vector2D = new Vector2D(x * scalar, y * scalar)
  14.     def /(scalar: Double): Vector2D = new Vector2D(x / scalar, y / scalar)
  15.  
  16.     /** Returns the scalar product */
  17.     def *(that: Vector2D): Double = x * that.x + y * that.y
  18.  
  19.     /** Returns the angle in degrees */
  20.     def \/(that: Vector2D): Double =
  21.       math.toDegrees(math.acos((this * that) / (length * that.length)))
  22.   }
  23.  
  24.   object Position {
  25.     def apply(that: Vector2D): Vector2D       = that
  26.     def apply(x: Double, y: Double): Vector2D = new Vector2D(x, y)
  27.   }
  28.  
  29.   object Direction {
  30.     def apply(that: Vector2D): Vector2D       = apply(that.x, that.y)
  31.     def apply(x: Double, y: Double): Vector2D = new Vector2D(x, y).normalize
  32.   }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement