// category vector() function vector(x, y) { // vector.x

the X axis of the vector. this.x = x; // vector.y

the Y axis of the vector. this.y = y; // vector.length()

returns the length of the vector. this.length = function() { return Math.sqrt((this.x * this.x) + (this.y * this.y)); } // vector.toAngle()

Get the angle towards the vector's direction in degrees. this.toAngle = function() { return (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90; } // vector.normalize()

returns a normalized copy of the vector. Original vector is not modified. this.normalize = function() { var newVector = this.getCopy(); if(newVector.length() != 0) { var length = newVector.length(); newVector.x /= length; newVector.y /= length; } return newVector; } // vector.lerpTo(otherVector, t)

Returns a new vector between this vector and 'otherVector' at point 't'. 0t = original vector, 1t = 'otherVector', 0.5t is halfway in between. this.lerpTo = function(otherVector, t) { var delta = otherVector.subtract(this); return this.add(delta.stretch(t)); } // vector.distanceTo(otherVector)

returns the distance to another vector. this.distanceTo = function(otherVector) { var dX = this.x - otherVector.x; var dY = this.y - otherVector.y; return new vector(dX, dY).length(); } // vector.add(otherVector)

adds the two vectors together, and returns as a copy. Original vector is not modified. this.add = function(otherVector) { var newVector = this.getCopy(); newVector.x += otherVector.x; newVector.y += otherVector.y; return newVector; } // vector.subtract(otherVector)

subtracts the second vector from this vector, and returns as a copy. Original vector is not modified. this.subtract = function(otherVector) { var newVector = this.getCopy(); newVector.x -= otherVector.x; newVector.y -= otherVector.y; return newVector; } // vector.multiply(otherVector)

multiplies the first vector X by the second vector's X, and the first's Y by the second's Y, and returns as a copy. Original vector is not modified. this.multiply = function(otherVector) { var newVector = this.getCopy(); newVector.x *= otherVector.x; newVector.y *= otherVector.y; return newVector; } // vector.divide(otherVector)

divides the first vector by the second, and returns as a copy. Original vector is not modified. this.divide = function(otherVector) { var newVector = this.getCopy(); newVector.x /= otherVector.x; newVector.y /= otherVector.y; return newVector; } // vector.stretch(length)

Multiplies the vector by the given numeric value, and returns as a copy. Original vector is not modified. this.stretch = function(length) { var newVector = this.getCopy(); newVector.x *= length; newVector.y *= length; return newVector; } // vector.getCopy()

returns a copy of this vector. this.getCopy = function() { return new vector(this.x, this.y); } }