File: dependencies\Vector2D.js
/**
2D vector object with support functions for performing vector arithmetic.
@class Vector2D
@constructor
*/
function Vector2D(x, y){
this.x = x || 0;
this.y = y || 0;
}
/**
Adds the specified vector to this vector.
@method add
@param vector2D {Vector2D} Vector to be added.
@return {Vector2D} Returns this vector, allowing for operator chaining.
*/
Vector2D.prototype.add = function(vector2D){
this.x += vector2D.x;
this.y += vector2D.y;
return this;
};
/**
Adds the specified vector to this vector and stores the result in the output vector. Doesn't modify this vector.
@method added
@param vector2D {Vector2D} Vector to be added.
@param output {Vector2D} Vector that will hold the result.
@return {Vector2D} Returns the output vector, allowing for operator chaining.
*/
Vector2D.prototype.added = function(vector2D, output){
output.x = this.x + vector2D.x;
output.y = this.y + vector2D.y;
return output;
};
/**
Subtracts the specified vector from this vector.
@method subtract
@param vector2D {Vector2D} Vector to be subtracted.
@return {Vector2D} Returns this vector, allowing for operator chaining.
*/
Vector2D.prototype.subtract = function(vector2D){
this.x -= vector2D.x;
this.y -= vector2D.y;
return this;
};
/**
Subtracts the specified vector from this vector and stores the result in the output vector. Doesn't modify this vector.
@method subtracted
@param vector2D {Vector2D} Vector to be subtracted.
@param output {Vector2D} Vector that will hold the result.
@return {Vector2D} Returns the output vector, allowing for operator chaining.
*/
Vector2D.prototype.subtracted = function(vector2D, output){
output.x = this.x - vector2D.x;
output.y = this.y - vector2D.y;
return output;
};
/**
Multiplies this vector by the value.
@method multiply
@param value {Number}
@return {Vector2D} Returns this vector, allowing for operator chaining.
*/
Vector2D.prototype.multiply = function(value){
this.x *= value;
this.y *= value;
return this;
};
/**
Multiplies this vector by the value and stores the result in the output vector. Doesn't modify this vector.
@method multiplied
@param value {Number}
@param output {Vector2D} Vector that will hold the result.
@return {Vector2D} Returns the output vector, allowing for operator chaining.
*/
Vector2D.prototype.multiplied = function(value, output){
output.x = this.x * value;
output.y = this.y * value;
return output;
};
/**
Divides this vector by the value.
@method divide
@param value {Number}
@return {Vector2D} Returns this vector, allowing for operator chaining.
*/
Vector2D.prototype.divide = function(value){
this.x /= value;
this.y /= value;
return this;
};
/**
Divides this vector by the value and stores the result in the output vector. Doesn't modify this vector.
@method divided
@param value {Number}
@param output {Vector2D} Vector that will hold the result.
@return {Vector2D} Returns the output vector, allowing for operator chaining.
*/
Vector2D.prototype.divided = function(value, output){
output.x = this.x / value;
output.y = this.y / value;
return output;
};
/**
Inverts this vector.
@method invert
@return {Vector2D} Returns this vector, allowing for operator chaining.
*/
Vector2D.prototype.invert = function(){
this.x = -this.x;
this.y = -this.y;
return this;
};
/**
Inverts this vector and stores the result in the output vector. Doesn't modify this vector.
@method inverted
@param output {Vector2D} Vector that will hold the result.
@return {Vector2D} Returns the output vector, allowing for operator chaining.
*/
Vector2D.prototype.inverted = function(output){
output.x = -this.x;
output.y = -this.y;
return output;
};
/**
Normalizes this vector.
@method normalize
@return {Vector2D} Returns this vector, allowing for operator chaining.
*/
Vector2D.prototype.normalize = function(){
var length = Math.sqrt( (this.x * this.x) + (this.y * this.y) );
this.x /= length;
this.y /= length;
return this;
};
/**
Normalizes this vector and stores the result in the output vector. Doesn't modify this vector.
@method normalized
@param output {Vector2D} Vector that will hold the result.
@return {Vector2D} Returns the output vector, allowing for operator chaining.
*/
Vector2D.prototype.normalized = function(output){
var length = Math.sqrt( (this.x * this.x) + (this.y * this.y) );
output.x = this.x / length;
output.y = this.y / length;
return output;
};
/**
@method magnitude
@return {Number} The magnitude of this vector.
*/
Vector2D.prototype.magnitude = function(){
return Math.sqrt( (this.x * this.x) + (this.y * this.y) );
};
/**
@method magnitudeSquared
@return {Number} The squared magnitude of this vector.
*/
Vector2D.prototype.magnitudeSquared = function(){
return (this.x * this.x) + (this.y * this.y);
};
/**
@method dot
@param vector2D {Vector2D} The other vector the dot product will be performed with.
@return {Number} The dot product for this and the specified vector.
*/
Vector2D.prototype.dot = function(vector2D){
return (this.x * vector2D.x) + (this.y * vector2D.y);
};