Advertisement
Guest User

Untitled

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