-
Notifications
You must be signed in to change notification settings - Fork 0
Vector class
TorchRay makes heavy use of vector math (as opposed to working directly with angles). The Vector class is used any time an (x, y) or (dx, dy) pair is needed.
let vec = new Vector(Number x, Number y)
Returns a new Vector object with the given x and y values.
vec.x -> Number
The horizontal component of the vector (a.k.a. the "run"). May be modified directly.
vec.y -> Number
The vertical component of the vector (a.k.a. the "rise"). May be modified directly.
The following methods can be chained: add, sub, mul, rotate.
vec.add(Vector other) -> Vector
Adds the x and y components of other to those of vec. Returns a reference to the modified vec.
vec.sub(Vector other) -> Vector
Subtracts the x and y components of other from those of vec. Returns a reference to the modified vec.
vec.mul(Number factor) -> Vector
Multiplies the x and y components of vec by factor. Returns a reference to the modified vec.
vec.rotate(Number angle) -> Vector
Rotates vec by angle radians about the origin (0, 0). Returns a reference to the modified vec.
vec.compare(Vector other) -> Number
Returns a number that is positive if vec is longer than other; negative if vec is shorter than other; and zero if vec and length have the exact same magnitude (length). Note: this is not simply the difference between the magnitudes of the two vectors! See the length method below.
vec.length() -> Number
Returns the magnitude of the vector (e.g. the distance from (0, 0) to (x, y) using the Pythagorean Distance Formula).
vec.copy() -> Vector
Returns a new Vector that has the same x and y properties as vec. This is important because Vectors are mutable (i.e. performing actions like add() and mul() will modify the vector). If you only want the result of an operation and not its side-effects, you must copy it first and work on the copy.