Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,21 @@ Crafty.math.Vector2D = (function () {
return this.x * vecRH.x + this.y * vecRH.y;
}; // dotProduct

/**@
* #.crossProduct
* @comp Crafty.math.Vector2D
*
* Calculates the z component of the cross product of the two vectors augmented to 3D.
*
* @public
* @sign public {Number} crossProduct(Vector2D);
* @param {Vector2D} vecRH
* @returns {Number} the resultant cross product
*/
Vector2D.prototype.crossProduct = function (vecRH) {
return this.x * vecRH.y - this.y * vecRH.x;
}; // crossProduct

/**@
* #.equals
* @comp Crafty.math.Vector2D
Expand All @@ -388,12 +403,14 @@ Crafty.math.Vector2D = (function () {
* @public
* @sign public {Vector2D} getNormal([Vector2D]);
* @param {Vector2D=<0,0>} [vecRH]
* @param {Vector2D} [result] store the result in this vector
* @returns {Vector2D} the new normal vector
*/
Vector2D.prototype.getNormal = function (vecRH) {
Vector2D.prototype.getNormal = function (vecRH, result) {
result = result || new Vector2D();
if (vecRH === undefined)
return new Vector2D(-this.y, this.x); // assume vecRH is <0, 0>
return new Vector2D(vecRH.y - this.y, this.x - vecRH.x).normalize();
return result.setValues(-this.y, this.x); // assume vecRH is <0, 0>
return result.setValues(vecRH.y - this.y, this.x - vecRH.x).normalize();
}; // getNormal

/**@
Expand Down Expand Up @@ -633,12 +650,14 @@ Crafty.math.Vector2D = (function () {
* @param {Vector2D} a
* @param {Vector2D} b
* @param {Vector2D} c
* @param {Vector2D} [result]
* @return {Vector2D} the triple product as a new vector
*/
Vector2D.tripleProduct = function (a, b, c) {
Vector2D.tripleProduct = function (a, b, c, result) {
result = result || new Crafty.math.Vector2D();
var ac = a.dotProduct(c);
var bc = b.dotProduct(c);
return new Crafty.math.Vector2D(b.x * ac - a.x * bc, b.y * ac - a.y * bc);
return result.setValues(b.x * ac - a.x * bc, b.y * ac - a.y * bc);
};

return Vector2D;
Expand Down
16 changes: 13 additions & 3 deletions tests/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ test("dotProduct()", function() {
equal(v46.dotProduct(v12), 16, "<4,6>.dotProduct(<1,2>) = 16");
});

test("crossProduct()", function() {
var v12 = new Vector2D(1, 2);
var v34 = new Vector2D(3, 4);
var v46 = new Vector2D(4, 6);

equal(v12.crossProduct(v34), -2, "<1,2>.crossProduct(<3,4>) = -2");
equal(v34.crossProduct(v46), 2, "<3,4>.crossProduct(<4,6>) = 2");
equal(v46.crossProduct(v12), 2, "<4,6>.crossProduct(<1,2>) = 2");
});

test("equals()", function() {
var v12 = new Vector2D(1, 2);
var v34 = new Vector2D(3, 4);
Expand Down Expand Up @@ -161,7 +171,7 @@ test("normalize()", function() {
var v_79 = new Vector2D(-7, 9);

equal(v0.normalize().equals(new Vector2D(1, 0)), true, "<0,0>.normalize() = <1,0>");
equal(v01.normalize().equals(new Vector2D(0, 1)), true, "<1,0>.normalize() = <0,1>");
equal(v01.normalize().equals(new Vector2D(0, 1)), true, "<0,1>.normalize() = <0,1>");
equal(v_79.normalize().equals(new Vector2D(-0.6139406135149205, 0.7893522173763263)), true, "<-7,9>.normalize() = <-0.6139406135149205,0.7893522173763263>");
});

Expand All @@ -172,7 +182,7 @@ test("scale()", function() {
equal(v11.scale(2, -3).equals(new Vector2D(4, -6)), true, "<2,2>.scale(2, -3) = <4,-6>");
});

test("magnitudeSq()", function() {
test("scaleToMagnitude()", function() {
var v34 = new Vector2D(3, 4);

equal(v34.normalize().scaleToMagnitude(5).equals(new Vector2D(3, 4)), true, "<3,4>.normalize().scaleToMagnitude(5) = <3,4>");
Expand Down Expand Up @@ -205,7 +215,7 @@ test("translate()", function() {
var v11 = new Vector2D(1, 1);

equal(v11.translate(2).equals(new Vector2D(3, 3)), true, "<1,1>.translate(2) = <3,3>");
equal(v11.translate(2, -3).equals(new Vector2D(5, 0)), true, "<2,2>.translate(2, -3) = <5,0>");
equal(v11.translate(2, -3).equals(new Vector2D(5, 0)), true, "<3,3>.translate(2, -3) = <5,0>");
});

test("tripleProduct()", function() {
Expand Down