function Vector2(x, y) {
this.set(x, y);
}
Written by Rahat Ahmed.
function Vector2(x, y) {
this.set(x, y);
}
Vector2.prototype.add = function(x, y) {
if(x instanceof Vector2)
{
this.x += x.x;
this.y += x.y;
}
else
{
this.x += x || 0;
this.y += y || 0;
}
return this;
};
Returns the angle in radians of this vector relative to the x-axis (counter-clockwise) in the range 0 to 2 * PI.
Rotates this vector to the given angle in radians. Returns this vector for chaining.
Vector2.prototype.angle = function(rad) {
if(rad !== undefined)
return this.set(this.length(), 0).rotate(rad);
var angle = Math.atan2(this.y, this.x);
if(angle < 0) angle += Math.PI*2;
return angle;
};
Same as angle() but in degrees.
Same as angle(radians) but in degrees.
Vector2.prototype.angleDeg = function(deg) {
if(deg !== undefined)
return this.angle(deg / 180 * Math.PI);
return this.angle() * 180 / Math.PI;
};
Vector2.prototype.clone = Vector2.prototype.copy = function() {
return new Vector2(this.x, this.y);
};
Vector2.prototype.cross = function(x, y) {
if(x instanceof Vector2)
return this.x * x.y - this.y * x.x;
return this.x * y - this.y * x;
};
Vector2.prototype.distance = function(x, y) {
var distSq = this.distanceSq(x, y);
if(distSq === undefined)
return undefined;
return Math.sqrt(distSq);
};
Vector2.prototype.distanceSq = function(x, y) {
var dx, dy;
if(x instanceof Vector2)
{
dx = x.x - this.x;
dy = x.y - this.y;
}
else if(y !== undefined)
{
dx = x - this.x;
dy = y - this.y;
}
else
return undefined;
return dx * dx + dy * dy;
};
Vector2.prototype.dot = function(x, y) {
if(x instanceof Vector2)
return this.x * x.x + this.y * x.y;
return this.x * x + this.y * y;
};
Returns true if this and another vector2 are equal.
Returns true if this and another vector2 are equal within an epsilon.
Returns true if this vector equals given x, y components.
Returns true if this vector equasl given x, y components within an epsilon.
Vector2.prototype.equals = function(x, y, epsilon) {
if(x instanceof Vector2)
{
y = y || 0;
return Math.abs(this.x - x.x) <= y && Math.abs(this.y - x.y) <= y;
}
else if(y !== undefined)
{
epsilon = epsilon || 0;
return Math.abs(this.x - x) <= epsilon && Math.abs(this.y - y) <= epsilon;
}
else
return false;
};
Vector2.prototype.length = function() {
return Math.sqrt(this.lengthSq());
};
Vector2.prototype.lengthSq = function() {
return this.x * this.x + this.y * this.y;
};
Vector2.prototype.negate = function() {
return this.scale(-1);
};
Vector2.prototype.normalize = function() {
return this.scale(1/this.length());
};
Rotates this vector by an angle in degrees counter-clockwise. Returns this vector for chaining.
Vector2.prototype.rotate = function(rad) {
var cos = Math.cos(rad);
var sin = Math.sin(rad);
return this.set(this.x * cos - this.y * sin,
this.x * sin + this.y * cos);
};
Vector2.prototype.rotateDeg = function(deg) {
return this.rotate(deg / 180 * Math.PI)
};
Scales this vector by a scalar. Second argument to scale y separate from x is optional. Returns this vector for chaining.
Vector2.prototype.scale = function(scaleX, scaleY) {
this.x *= scaleX;
this.y *= (scaleY || scaleX);
return this;
};
Vector2.prototype.set = function(x, y) {
if(x instanceof Vector2)
{
this.x = x.x;
this.y = x.y;
}
else
{
this.x = x || 0;
this.y = y || 0;
}
return this;
};
Set this vector by angle in degrees and magnitude. Returns this vector for chaining.
Vector2.prototype.setPolar = function(rad, length) {
return this.set(length, 0).rotate(rad);
};
Vector2.prototype.setPolarDeg = function(deg, length) {
return this.setPolar(deg / 180 * Math.PI, length);
};
Vector2.prototype.sub = function(x, y) {
if(y !== undefined)
{
this.x -= x;
this.y -= y;
}
else
{
this.x -= x.x;
this.y -= x.y;
}
return this;
};
Vector2.prototype.toString = function() {
return "(" + this.x + ", " + this.y + ")";
};
module.exports = Vector2;