Comparing version 0.0.1 to 0.1.0
912
index.js
@@ -1,8 +0,23 @@ | ||
exports = module.exports = Vector; | ||
exports = module.exports = Victor; | ||
function Vector (x, y) { | ||
if (!(this instanceof Vector)) { | ||
return new Vector(x, y); | ||
/** | ||
* # Victor - A JavaScript 2D vector class with methods for common vector operations | ||
*/ | ||
/** | ||
* Constructor. Will also work without the `new` keyword | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(100, 50); | ||
* var vec2 = Victor(42, 1337); | ||
* | ||
* @param {Number} x Value of the x axis | ||
* @param {Number} y Value of the y axis | ||
* @return {Victor} | ||
* @api public | ||
*/ | ||
function Victor (x, y) { | ||
if (!(this instanceof Victor)) { | ||
return new Victor(x, y); | ||
} | ||
this.x = x || 0; | ||
@@ -12,117 +27,878 @@ this.y = y || 0; | ||
Vector.random = function (maxX, maxY) { | ||
/** | ||
* # Properties | ||
*/ | ||
/** | ||
* The X axis | ||
* | ||
* ### Examples: | ||
* var vec = new Victor.fromArray(42, 21); | ||
* | ||
* vec.x; | ||
* // => 42 | ||
* | ||
* @api public | ||
*/ | ||
Victor.prototype.x = 0; | ||
/** | ||
* The Y axis | ||
* | ||
* ### Examples: | ||
* var vec = new Victor.fromArray(42, 21); | ||
* | ||
* vec.y; | ||
* // => 21 | ||
* | ||
* @api public | ||
*/ | ||
Victor.prototype.y = 0; | ||
/** | ||
* # Static | ||
*/ | ||
/** | ||
* Creates a new instance from an array | ||
* | ||
* ### Examples: | ||
* var vec = Victor.fromArray([42, 21]); | ||
* | ||
* vec.toString(); | ||
* // => x:42, y:21 | ||
* | ||
* @name Victor.fromArray | ||
* @param {Array} array Array with the x and y values at index 0 and 1 respectively | ||
* @return {Victor} The new instance | ||
* @api public | ||
*/ | ||
Victor.fromArray = function (arr) { | ||
return new Victor(arr[0] || 0, arr[1] || 0); | ||
}; | ||
/** | ||
* Creates a new instance from an object | ||
* | ||
* ### Examples: | ||
* var vec = Victor.fromObject({ x: 42, y: 21 }); | ||
* | ||
* vec.toString(); | ||
* // => x:42, y:21 | ||
* | ||
* @name Victor.fromObject | ||
* @param {Object} obj Object with the values for x and y | ||
* @return {Victor} The new instance | ||
* @api public | ||
*/ | ||
Victor.fromObject = function (obj) { | ||
return new Victor(obj.x || 0, obj.y || 0); | ||
}; | ||
Victor.add = function (vecA, vecB) { | ||
return new Victor(vecA.x + vecB.x, vecA.y + vecB.y); | ||
}; | ||
Victor.subtract = function (vecA, vecB) { | ||
return new Victor(vecA.x - vecB.x, vecA.y - vecB.y); | ||
}; | ||
Victor.multiply = function (vec, scalar) { | ||
return new Victor(vec.x * scalar, vec.y * scalar); | ||
}; | ||
Victor.divide = function (vec, scalar) { | ||
return new Victor(vec.x / scalar, vec.y / scalar); | ||
}; | ||
Victor.mix = function (vecA, vecB, amount) { | ||
amount = amount || 0.5 | ||
var x = (1 - amount) * vecA.x + amount * vecB.x; | ||
var y = (1 - amount) * vecA.y + amount * vecB.y; | ||
return new Victor(x, y); | ||
}; | ||
Victor.random = function (maxX, maxY) { | ||
var x = Math.floor(Math.random() * maxX), | ||
y = Math.floor(Math.random() * maxY); | ||
return new Vector(x, y); | ||
return new Victor(x, y); | ||
}; | ||
Vector.prototype.copy = function (vec) { | ||
return new Vector(this.x, this.y); | ||
/** | ||
* # Manipulation | ||
* | ||
* These functions are chainable. | ||
*/ | ||
/** | ||
* Adds another vector's X axis to this one | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(10, 10); | ||
* var vec2 = new Victor(20, 30); | ||
* | ||
* vec1.addX(vec2); | ||
* vec1.toString(); | ||
* // => x:30, y:10 | ||
* | ||
* @param {Victor} vector The other vector you want to add to this one | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.addX = function (vec) { | ||
this.x += vec.x; | ||
return this; | ||
}; | ||
Vector.prototype.add = function (vec) { | ||
this.x += vec.x; | ||
/** | ||
* Adds another vector's Y axis to this one | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(10, 10); | ||
* var vec2 = new Victor(20, 30); | ||
* | ||
* vec1.addY(vec2); | ||
* vec1.toString(); | ||
* // => x:10, y:40 | ||
* | ||
* @param {Victor} vector The other vector you want to add to this one | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.addY = function (vec) { | ||
this.y += vec.y; | ||
return this; | ||
}; | ||
Vector.prototype.addCopy = function (vec) { | ||
return new Vector( | ||
this.x + vec.x, | ||
this.y + vec.y | ||
); | ||
/** | ||
* Adds another vector to this one | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(10, 10); | ||
* var vec2 = new Victor(20, 30); | ||
* | ||
* vec1.add(vec2); | ||
* vec1.toString(); | ||
* // => x:30, y:40 | ||
* | ||
* @param {Victor} vector The other vector you want to add to this one | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.add = function (vec) { | ||
this.addX(vec); | ||
this.addY(vec); | ||
return this; | ||
}; | ||
Vector.prototype.sub = function (vec) { | ||
/** | ||
* Subtracts the X axis of another vector from this one | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(100, 50); | ||
* var vec2 = new Victor(20, 30); | ||
* | ||
* vec1.subtractX(vec2); | ||
* vec1.toString(); | ||
* // => x:80, y:50 | ||
* | ||
* @param {Victor} vector The other vector you want subtract from this one | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.subtractX = function (vec) { | ||
this.x -= vec.x; | ||
this.y -= vec.y; | ||
return this; | ||
}; | ||
Vector.prototype.subCopy = function (vec) { | ||
return new Vector( | ||
this.x - vec.x, | ||
this.y - vec.y | ||
); | ||
/** | ||
* Subtracts the Y axis of another vector from this one | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(100, 50); | ||
* var vec2 = new Victor(20, 30); | ||
* | ||
* vec1.subtractY(vec2); | ||
* vec1.toString(); | ||
* // => x:100, y:20 | ||
* | ||
* @param {Victor} vector The other vector you want subtract from this one | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.subtractY = function (vec) { | ||
this.y -= vec.y; | ||
return this; | ||
}; | ||
Vector.prototype.divide = function (vec) { | ||
this.x /= vec.x; | ||
this.y /= vec.y; | ||
/** | ||
* Subtracts another vector from this one | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(100, 50); | ||
* var vec2 = new Victor(20, 30); | ||
* | ||
* vec1.subtract(vec2); | ||
* vec1.toString(); | ||
* // => x:80, y:20 | ||
* | ||
* @param {Victor} vector The other vector you want subtract from this one | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.subtract = function (vec) { | ||
this.subtractX(vec); | ||
this.subtractY(vec); | ||
return this; | ||
}; | ||
Vector.prototype.divideCopy = function (vec) { | ||
return new Vector( | ||
this.x / vec.x, | ||
this.y / vec.y | ||
); | ||
/** | ||
* Divides the X axis by a number | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(100, 50); | ||
* | ||
* vec.divideX(2); | ||
* vec.toString(); | ||
* // => x:50, y:50 | ||
* | ||
* @param {Number} number The number to divide the axis by | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.divideX = function (scalar) { | ||
this.x /= scalar; | ||
return this; | ||
}; | ||
Vector.prototype.multiply = function (vec) { | ||
this.x *= vec.x; | ||
this.y *= vec.y; | ||
/** | ||
* Divides the Y axis by a number | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(100, 50); | ||
* | ||
* vec.divideY(2); | ||
* vec.toString(); | ||
* // => x:100, y:25 | ||
* | ||
* @param {Number} number The number to divide the axis by | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.divideY = function (scalar) { | ||
this.y /= scalar; | ||
return this; | ||
}; | ||
Vector.prototype.multiplyCopy = function (vec) { | ||
return new Vector( | ||
this.x * vec.x, | ||
this.y * vec.y | ||
); | ||
/** | ||
* Divides both vector axis by a number | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(100, 50); | ||
* | ||
* vec.divide(2); | ||
* vec.toString(); | ||
* // => x:50, y:25 | ||
* | ||
* @param {Number} number The number to divide the axis by | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.divide = function (scalar) { | ||
this.divideX(scalar); | ||
this.divideY(scalar); | ||
return this; | ||
}; | ||
Vector.prototype.distanceX = function (vec) { | ||
return this.x - vec.x; | ||
/** | ||
* Multiplies the X axis by a number | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(100, 50); | ||
* | ||
* vec.multiplyX(2); | ||
* vec.toString(); | ||
* // => x:200, y:50 | ||
* | ||
* @param {Number} number The number to multiply the axis with | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.multiplyX = function (scalar) { | ||
this.x *= scalar; | ||
return this; | ||
}; | ||
Vector.prototype.distanceY = function (vec) { | ||
return this.y - vec.y; | ||
/** | ||
* Multiplies the Y axis by a number | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(100, 50); | ||
* | ||
* vec.multiplyY(2); | ||
* vec.toString(); | ||
* // => x:100, y:100 | ||
* | ||
* @param {Number} number The number to multiply the axis with | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.multiplyY = function (scalar) { | ||
this.y *= scalar; | ||
return this; | ||
}; | ||
Vector.prototype.distance = function (vec) { | ||
var dx = this.distanceX(vec), | ||
dy = this.distanceY(vec); | ||
return Math.sqrt(dx * dx + dy * dy); | ||
/** | ||
* Multiplies both vector axis by a number | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(100, 50); | ||
* | ||
* vec.multiply(2); | ||
* vec.toString(); | ||
* // => x:200, y:100 | ||
* | ||
* @param {Number} number The number to multiply the axis with | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.multiply = function (scalar) { | ||
this.multiplyX(scalar); | ||
this.multiplyY(scalar); | ||
return this; | ||
}; | ||
Vector.prototype.length = function () { | ||
return Math.sqrt(this.x * this.x + this.y * this.y); | ||
/** | ||
* Normalize | ||
* | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.normalize = function () { | ||
if (this.length() === 0) { | ||
this.x = 1; | ||
this.y = 0; | ||
} else { | ||
this.divide(this.length()); | ||
} | ||
return this; | ||
}; | ||
Vector.prototype.limit = function (max, factor) { | ||
Victor.prototype.norm = Victor.prototype.normalize; | ||
/** | ||
* If the absolute vector axis is greater than `max`, multiplies the axis by `factor` | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(100, 50); | ||
* | ||
* vec.limit(80, 0.9); | ||
* vec.toString(); | ||
* // => x:90, y:50 | ||
* | ||
* @param {Number} max The maximum value for both x and y axis | ||
* @param {Number} factor Factor by which the axis are to be multiplied with | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.limit = function (max, factor) { | ||
if (Math.abs(this.x) > max){ this.x *= factor; } | ||
if (Math.abs(this.y) > max){ this.y *= factor; } | ||
return this; | ||
}; | ||
Vector.prototype.randomize = function (maxX, maxY) { | ||
var x = Math.floor(Math.random() * maxX), | ||
y = Math.floor(Math.random() * maxY); | ||
/** | ||
* Randomizes both vector axis with a value between 2 vectors | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(100, 50); | ||
* | ||
* vec.randomize(new Victor(50, 60), new Victor(70, 80`)); | ||
* vec.toString(); | ||
* // => x:67, y:73 | ||
* | ||
* @param {Victor} topLeft first vector | ||
* @param {Victor} bottomRight second vector | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.randomize = function (topLeft, bottomRight) { | ||
this.randomizeX(topLeft, bottomRight); | ||
this.randomizeY(topLeft, bottomRight); | ||
return new Vector(x, y); | ||
return this; | ||
}; | ||
Vector.prototype.randomizeX = function (max) { | ||
var x = Math.floor(Math.random() * max); | ||
this.x = x; | ||
/** | ||
* Randomizes the y axis with a value between 2 vectors | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(100, 50); | ||
* | ||
* vec.randomizeX(new Victor(50, 60), new Victor(70, 80`)); | ||
* vec.toString(); | ||
* // => x:55, y:50 | ||
* | ||
* @param {Victor} topLeft first vector | ||
* @param {Victor} bottomRight second vector | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.randomizeX = function (topLeft, bottomRight) { | ||
var min = Math.min(topLeft.x, bottomRight.x); | ||
var max = Math.max(topLeft.x, bottomRight.x); | ||
this.x = random(min, max); | ||
return this; | ||
}; | ||
Vector.prototype.randomizeY = function (max) { | ||
var y = Math.floor(Math.random() * max); | ||
this.y = y; | ||
/** | ||
* Randomizes the y axis with a value between 2 vectors | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(100, 50); | ||
* | ||
* vec.randomizeY(new Victor(50, 60), new Victor(70, 80`)); | ||
* vec.toString(); | ||
* // => x:100, y:66 | ||
* | ||
* @param {Victor} topLeft first vector | ||
* @param {Victor} bottomRight second vector | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.randomizeY = function (topLeft, bottomRight) { | ||
var min = Math.min(topLeft.y, bottomRight.y); | ||
var max = Math.max(topLeft.y, bottomRight.y); | ||
this.y = random(min, max); | ||
return this; | ||
}; | ||
Vector.prototype.randomizeAny = function (maxX, maxY) { | ||
if (!! Math.round(Math.random() * 1)) { | ||
this.randomizeX(maxX); | ||
/** | ||
* Randomly randomizes either axis between 2 vectors | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(100, 50); | ||
* | ||
* vec.randomizeAny(new Victor(50, 60), new Victor(70, 80)); | ||
* vec.toString(); | ||
* // => x:100, y:77 | ||
* | ||
* @param {Victor} topLeft first vector | ||
* @param {Victor} bottomRight second vector | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.randomizeAny = function (topLeft, bottomRight) { | ||
if (!! Math.round(Math.random())) { | ||
this.randomizeX(topLeft, bottomRight); | ||
} else { | ||
this.randomizeY(maxY); | ||
this.randomizeY(topLeft, bottomRight); | ||
} | ||
return this; | ||
}; | ||
Vector.prototype.unfloat = function () { | ||
/** | ||
* Rounds both axis to an integer value | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(100.2, 50.9); | ||
* | ||
* vec.unfloat(); | ||
* vec.toString(); | ||
* // => x:100, y:51 | ||
* | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.unfloat = function () { | ||
this.x = Math.round(this.x); | ||
this.y = Math.round(this.y); | ||
return this; | ||
}; | ||
Vector.prototype.unfloatCopy = function () { | ||
return new Vector(Math.round(this.x), Math.round(this.y)); | ||
/** | ||
* Performs a linear blend / interpolation of the X axis towards another vector | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(100, 100); | ||
* var vec2 = new Victor(200, 200); | ||
* | ||
* vec1.mixX(vec2, 0.5); | ||
* vec.toString(); | ||
* // => x:150, y:100 | ||
* | ||
* @param {Victor} vector The other vector | ||
* @param {Number} amount The blend amount (optional, default: 0.5) | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.mixX = function (vec, amount) { | ||
amount = amount || 0.5; | ||
this.x = (1 - amount) * this.x + amount * vec.x; | ||
return this; | ||
}; | ||
/** | ||
* Performs a linear blend / interpolation of the Y axis towards another vector | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(100, 100); | ||
* var vec2 = new Victor(200, 200); | ||
* | ||
* vec1.mixY(vec2, 0.5); | ||
* vec.toString(); | ||
* // => x:100, y:150 | ||
* | ||
* @param {Victor} vector The other vector | ||
* @param {Number} amount The blend amount (optional, default: 0.5) | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.mixY = function (vec, amount) { | ||
amount = amount || 0.5; | ||
this.y = (1 - amount) * this.y + amount * vec.y; | ||
return this; | ||
}; | ||
/** | ||
* Performs a linear blend / interpolation towards another vector | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(100, 100); | ||
* var vec2 = new Victor(200, 200); | ||
* | ||
* vec1.mix(vec2, 0.5); | ||
* vec.toString(); | ||
* // => x:150, y:150 | ||
* | ||
* @param {Victor} vector The other vector | ||
* @param {Number} amount The blend amount (optional, default: 0.5) | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.mix = function (vec, amount) { | ||
amount = amount || 0.5; | ||
this.mixX(vec, amount); | ||
this.mixY(vec, amount); | ||
return this; | ||
}; | ||
/** | ||
* # Products | ||
*/ | ||
/** | ||
* Creates a clone of this vector | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(10, 10); | ||
* var vec2 = vec1.clone(); | ||
* | ||
* vec2.toString(); | ||
* // => x:10, y:10 | ||
* | ||
* @return {Victor} A clone of the vector | ||
* @api public | ||
*/ | ||
Victor.prototype.clone = function () { | ||
return new Victor(this.x, this.y); | ||
}; | ||
/** | ||
* Copies another vector's X component in to its own | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(10, 10); | ||
* var vec2 = new Victor(20, 20); | ||
* var vec2 = vec1.copyX(vec1); | ||
* | ||
* vec2.toString(); | ||
* // => x:20, y:10 | ||
* | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.copyX = function (vec) { | ||
this.x = vec.x; | ||
return this; | ||
}; | ||
/** | ||
* Copies another vector's Y component in to its own | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(10, 10); | ||
* var vec2 = new Victor(20, 20); | ||
* var vec2 = vec1.copyY(vec1); | ||
* | ||
* vec2.toString(); | ||
* // => x:10, y:20 | ||
* | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.copyY = function (vec) { | ||
this.y = vec.y; | ||
return this; | ||
}; | ||
/** | ||
* Copies another vector's X and Y components in to its own | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(10, 10); | ||
* var vec2 = new Victor(20, 20); | ||
* var vec2 = vec1.copy(vec1); | ||
* | ||
* vec2.toString(); | ||
* // => x:20, y:20 | ||
* | ||
* @return {Victor} `this` for chaining capabilities | ||
* @api public | ||
*/ | ||
Victor.prototype.copy = function (vec) { | ||
this.copyX(vec); | ||
this.copyY(vec); | ||
return this; | ||
}; | ||
/** | ||
* Calculates the dot product of this vector and another | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(100, 50); | ||
* var vec2 = new Victor(200, 60); | ||
* | ||
* vec1.dot(vec2); | ||
* // => 23000 | ||
* | ||
* @param {Victor} vector The second vector | ||
* @return {Number} Dot product | ||
* @api public | ||
*/ | ||
Victor.prototype.dot = function (vec2) { | ||
return this.x * vec2.x + this.y * vec2.y; | ||
}; | ||
Victor.prototype.horizontalAngle = function () { | ||
return Math.atan2(this.y, this.x); | ||
}; | ||
Victor.prototype.horizontalAngleDeg = function () { | ||
return radian2degrees(this.horizontalAngle()); | ||
}; | ||
Victor.prototype.verticalAngle = function () { | ||
return Math.atan2(this.x, this.y); | ||
}; | ||
Victor.prototype.verticalAngleDeg = function () { | ||
return radian2degrees(this.verticalAngle()); | ||
}; | ||
Victor.prototype.angle = Victor.prototype.horizontalAngle; | ||
Victor.prototype.angleDeg = Victor.prototype.horizontalAngleDeg; | ||
Victor.prototype.direction = Victor.prototype.horizontalAngle; | ||
Victor.prototype.rotate = function (angle) { | ||
var nx = (this.x * Math.cos(angle)) - (this.y * Math.sin(angle)); | ||
var ny = (this.x * Math.sin(angle)) + (this.y * Math.cos(angle)); | ||
this.x = nx; | ||
this.y = ny; | ||
}; | ||
Victor.prototype.rotateDeg = function (angle) { | ||
angle = degrees2radian(angle); | ||
this.rotate(angle); | ||
}; | ||
Victor.prototype.rotateBy = function (rotation) { | ||
var angle = this.angle() + rotation; | ||
this.rotate(angle); | ||
}; | ||
Victor.prototype.rotateByDeg = function (rotation) { | ||
rotation = degrees2radian(rotation); | ||
this.rotateBy(rotation); | ||
}; | ||
/** | ||
* Calculates the distance of the X axis between this vector and another | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(100, 50); | ||
* var vec2 = new Victor(200, 60); | ||
* | ||
* vec1.distanceX(vec2); | ||
* // => -100 | ||
* | ||
* @param {Victor} vector The second vector | ||
* @return {Number} Distance | ||
* @api public | ||
*/ | ||
Victor.prototype.distanceX = function (vec) { | ||
return this.x - vec.x; | ||
}; | ||
/** | ||
* Same as `distanceX()` but always returns an absolute number | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(100, 50); | ||
* var vec2 = new Victor(200, 60); | ||
* | ||
* vec1.absDistanceX(vec2); | ||
* // => 100 | ||
* | ||
* @param {Victor} vector The second vector | ||
* @return {Number} Absolute distance | ||
* @api public | ||
*/ | ||
Victor.prototype.absDistanceX = function (vec) { | ||
return Math.abs(this.distanceX(vec)); | ||
}; | ||
/** | ||
* Calculates the distance of the Y axis between this vector and another | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(100, 50); | ||
* var vec2 = new Victor(200, 60); | ||
* | ||
* vec1.distanceY(vec2); | ||
* // => -10 | ||
* | ||
* @param {Victor} vector The second vector | ||
* @return {Number} Distance | ||
* @api public | ||
*/ | ||
Victor.prototype.distanceY = function (vec) { | ||
return this.y - vec.y; | ||
}; | ||
/** | ||
* Same as `distanceY()` but always returns an absolute number | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(100, 50); | ||
* var vec2 = new Victor(200, 60); | ||
* | ||
* vec1.distanceY(vec2); | ||
* // => 10 | ||
* | ||
* @param {Victor} vector The second vector | ||
* @return {Number} Absolute distance | ||
* @api public | ||
*/ | ||
Victor.prototype.absDistanceY = function (vec) { | ||
return Math.abs(this.distanceY(vec)); | ||
}; | ||
/** | ||
* Calculates the euclidean distance between this vector and another | ||
* | ||
* ### Examples: | ||
* var vec1 = new Victor(100, 50); | ||
* var vec2 = new Victor(200, 60); | ||
* | ||
* vec1.distance(vec2); | ||
* // => 100.4987562112089 | ||
* | ||
* @param {Victor} vector The second vector | ||
* @return {Number} Distance | ||
* @api public | ||
*/ | ||
Victor.prototype.distance = function (vec) { | ||
var dx = this.distanceX(vec), | ||
dy = this.distanceY(vec); | ||
return Math.sqrt(dx * dx + dy * dy); | ||
}; | ||
/** | ||
* Calculates the length or magnitude of the vector | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(100, 50); | ||
* | ||
* vec.length(); | ||
* // => 111.80339887498948 | ||
* | ||
* @return {Number} Length / Magnitude | ||
* @api public | ||
*/ | ||
Victor.prototype.length = function () { | ||
return Math.sqrt(this.x * this.x + this.y * this.y); | ||
}; | ||
Victor.prototype.magnitude = Victor.prototype.length; | ||
/** | ||
* # Utility Methods | ||
*/ | ||
/** | ||
* Returns an string representation of the vector | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(10, 20); | ||
* | ||
* vec.toString(); | ||
* // => x:10, y:20 | ||
* | ||
* @return {String} | ||
* @api public | ||
*/ | ||
Victor.prototype.toString = function () { | ||
return 'x:' + this.x + ', y:' + this.y; | ||
}; | ||
/** | ||
* Returns an array representation of the vector | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(10, 20); | ||
* | ||
* vec.toArray(); | ||
* // => [10, 20] | ||
* | ||
* @return {Array} | ||
* @api public | ||
*/ | ||
Victor.prototype.toArray = function () { | ||
return [ this.x, this.y ]; | ||
}; | ||
/** | ||
* Returns an object representation of the vector | ||
* | ||
* ### Examples: | ||
* var vec = new Victor(10, 20); | ||
* | ||
* vec.toObject(); | ||
* // => { x: 10, y: 20 } | ||
* | ||
* @return {Object} | ||
* @api public | ||
*/ | ||
Victor.prototype.toObject = function () { | ||
return { x: this.x, y: this.y }; | ||
}; | ||
var degrees = 180 / Math.PI; | ||
function random (min, max) { | ||
return Math.floor(Math.random() * (max - min + 1) + min); | ||
} | ||
function radian2degrees (rad) { | ||
return rad * degrees; | ||
} | ||
function degrees2radian (deg) { | ||
return deg / degrees; | ||
} |
{ | ||
"name": "victor", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "A JavaScript 2D vector class with methods for common vector operations", | ||
"keywords" : [ "vector", "d2" ], | ||
"keywords": [ | ||
"vector", | ||
"2d" | ||
], | ||
"main": "index.js", | ||
@@ -11,3 +14,12 @@ "repository": { | ||
}, | ||
"scripts": { | ||
"build": "(echo '/*!' ; cat ./LICENSE ; echo '*/' ; browserify index.js --standalone Victor) > build/victor.js && cat build/victor.js | uglifyjs -mc --comments '/^!/' > build/victor.min.js", | ||
"doc": "markdox index.js -o documentation/api.md", | ||
"test": "mocha --ui bdd --reporter spec" | ||
}, | ||
"author": "Max Kueng <me@maxkueng.com> (http://maxkueng.com/)", | ||
"contributors": [ | ||
"Max Kueng (http://maxkueng.com/)", | ||
"George Crabtree (http://georgecrabtree.com/)" | ||
], | ||
"licenses": [ | ||
@@ -18,3 +30,10 @@ { | ||
} | ||
] | ||
], | ||
"devDependencies": { | ||
"browserify": "^3.33.0", | ||
"uglify-js": "^2.4.13", | ||
"chai": "~1.9.1", | ||
"mocha": "~1.18.2", | ||
"markdox": "^0.1.4" | ||
} | ||
} |
@@ -1,4 +0,48 @@ | ||
Victor | ||
====== | ||
![Victor](./artwork/logo.png) | ||
============================= | ||
work in progress | ||
[![Build Status](https://secure.travis-ci.org/maxkueng/victor.png?branch=master)](http://travis-ci.org/maxkueng/victor) | ||
A Javascript 2D Vector Maths Library, built using the UMD standards, so you can use it as a global object or with any module loader. It works in both Node.js and the browser. | ||
Check out the website for [documentation](http://victorjs.org/). | ||
## Installation | ||
### Node.js / Browserify | ||
```bash | ||
npm install victor --save | ||
``` | ||
```javascript | ||
var Victor = require('victor'); | ||
var vec = new Victor(42, 1337); | ||
``` | ||
### Bower | ||
```bash | ||
bower install victor --save | ||
``` | ||
### Global object | ||
Include the pre-built script. | ||
```html | ||
<script src="./build/victor.js"></script> | ||
<script> | ||
var vec = new Victor(42, 1337); | ||
</script> | ||
``` | ||
## Build & test | ||
```bash | ||
npm run build | ||
``` | ||
```bash | ||
npm test | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23532
7
861
49
5
1