Comparing version 0.1.3 to 0.2.0
19
index.js
@@ -204,1 +204,20 @@ module.exports = v; | ||
}; | ||
// reflect about axis originating from origin | ||
Vec2d.prototype.reflect = function(axis) { | ||
return this.reflectAboutLine(new Vec2d(0, 0), axis); | ||
}; | ||
Vec2d.prototype.reflectAboutLine = function(linePt1, linePt2) { | ||
var normal = new Vec2d( | ||
linePt2.x - linePt1.x, | ||
linePt2.y - linePt1.x); | ||
var temp = normal.x; | ||
normal.x = -normal.y; | ||
normal.y = temp; | ||
normal.normalize(); | ||
var dot2 = 2 * this.dot(normal); | ||
this.x -= dot2 * normal.x; | ||
this.y -= dot2 * normal.y; | ||
return this; | ||
}; |
{ | ||
"name": "vec2d", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "2d vector math with good unit tests", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -80,5 +80,7 @@ # vec2d | ||
- distanceSqrd | ||
✓ reflect | ||
✓ reflectAboutLine | ||
43 tests complete (10 ms) | ||
45 tests complete (11 ms) | ||
23 tests pending | ||
@@ -85,0 +87,0 @@ ``` |
28
test.js
@@ -48,4 +48,4 @@ var v = require('./') | ||
var v1 = v.unit(Math.PI / 2); | ||
assert.ok(Math.abs(0 - v1.x) < EPSILON); | ||
assert.ok(Math.abs(1 - v1.y) < EPSILON); | ||
assertCloseEnough(0, v1.x); | ||
assertCloseEnough(1, v1.y); | ||
}); | ||
@@ -56,4 +56,4 @@ }); | ||
var v1 = Vec2d.unit(Math.PI); | ||
assert.ok(Math.abs(-1 - v1.x) < EPSILON); | ||
assert.ok(Math.abs(0 - v1.y) < EPSILON); | ||
assertCloseEnough(-1, v1.x); | ||
assertCloseEnough(0, v1.y); | ||
}); | ||
@@ -180,2 +180,22 @@ it("offset", function() { | ||
it("distanceSqrd"); | ||
it("reflect", function() { | ||
var v1 = v(1, 0); | ||
var axis = v(0, 1); | ||
var v2 = v1.reflect(axis); | ||
assert.strictEqual(v1.x, -1); | ||
assert.strictEqual(v1.y, 0); | ||
}); | ||
it("reflectAboutLine", function() { | ||
var v1 = v(1, -1); | ||
var linePt1 = v(1, 1); | ||
var linePt2 = v(-1, -1); | ||
var v2 = v1.reflectAboutLine(linePt1, linePt2); | ||
assert.strictEqual(v2, v1); | ||
assertCloseEnough(v1.x, -1); | ||
assertCloseEnough(v1.y, 1); | ||
}); | ||
}); | ||
function assertCloseEnough(a, b) { | ||
assert.ok(Math.abs(a - b) < EPSILON); | ||
} |
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
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
11720
377
89