kld-affine
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -7,3 +7,21 @@ /** | ||
function setReadonlyProperty(object, property, value) { | ||
Object.defineProperty(object, property, { | ||
value: value, | ||
writable: false, | ||
enumerable: true, | ||
configurable: false | ||
}); | ||
} | ||
/** | ||
* Identity matrix | ||
* | ||
* @returns {Matrix2D} | ||
*/ | ||
setReadonlyProperty(Matrix2D, "IDENTITY", new Matrix2D(1, 0, 0, 1, 0, 0)); | ||
setReadonlyProperty(Matrix2D.IDENTITY, "isIdentity", () => true); | ||
/** | ||
* Matrix2D | ||
@@ -24,52 +42,165 @@ * | ||
function Matrix2D(a, b, c, d, e, f) { | ||
Object.defineProperties(this, { | ||
"a": { | ||
value: (a !== undefined) ? a : 1, | ||
writable: false, | ||
enumerable: true, | ||
configurable: false | ||
}, | ||
"b": { | ||
value: (b !== undefined) ? b : 0, | ||
writable: false, | ||
enumerable: true, | ||
configurable: false | ||
}, | ||
"c": { | ||
value: (c !== undefined) ? c : 0, | ||
writable: false, | ||
enumerable: true, | ||
configurable: false | ||
}, | ||
"d": { | ||
value: (d !== undefined) ? d : 1, | ||
writable: false, | ||
enumerable: true, | ||
configurable: false | ||
}, | ||
"e": { | ||
value: (e !== undefined) ? e : 0, | ||
writable: false, | ||
enumerable: true, | ||
configurable: false | ||
}, | ||
"f": { | ||
value: (f !== undefined) ? f : 0, | ||
writable: false, | ||
enumerable: true, | ||
configurable: false | ||
} | ||
}); | ||
setReadonlyProperty(this, "a", (a !== undefined) ? a : 1); | ||
setReadonlyProperty(this, "b", (b !== undefined) ? b : 0); | ||
setReadonlyProperty(this, "c", (c !== undefined) ? c : 0); | ||
setReadonlyProperty(this, "d", (d !== undefined) ? d : 1); | ||
setReadonlyProperty(this, "e", (e !== undefined) ? e : 0); | ||
setReadonlyProperty(this, "f", (f !== undefined) ? f : 0); | ||
} | ||
// *** STATIC METHODS | ||
/** | ||
* Identity matrix | ||
* translation | ||
* | ||
* @param {Number} tx | ||
* @param {Number} ty | ||
* @returns {Matrix2D} | ||
*/ | ||
// TODO: consider using Object#defineProperty to make this read-only | ||
Matrix2D.IDENTITY = new Matrix2D(1, 0, 0, 1, 0, 0); | ||
Matrix2D.IDENTITY.isIdentity = function () { return true; }; | ||
Matrix2D.translation = function(tx, ty) { | ||
return new Matrix2D(1, 0, 0, 1, tx, ty); | ||
}; | ||
/** | ||
* scaling | ||
* | ||
* @param {Number} scale | ||
* @returns {Matrix2D} | ||
*/ | ||
Matrix2D.scaling = function(scale) { | ||
return new Matrix2D(scale, 0, 0, scale, 0, 0); | ||
}; | ||
/** | ||
* scalingAt | ||
* | ||
* @param {Number} scale | ||
* @param {Point2D} center | ||
* @returns {Matrix2D} | ||
*/ | ||
Matrix2D.scalingAt = function(scale, center) { | ||
// TODO: calculate all math inline | ||
return Matrix2D | ||
.translation(center.x, center.y) | ||
.scale(scale) | ||
.translate(-center.x, -center.y); | ||
} | ||
/** | ||
* nonUniformScaling | ||
* | ||
* @param {Number} scaleX | ||
* @param {Number} scaleY | ||
* @returns {Matrix2D} | ||
*/ | ||
Matrix2D.nonUniformScaling = function(scaleX, scaleY) { | ||
return new Matrix2D(scaleX, 0, 0, scaleY, 0, 0); | ||
}; | ||
/** | ||
* nonUniformScalingAt | ||
* | ||
* @param {Number} scaleX | ||
* @param {Number} scaleY | ||
* @param {Point2D} center | ||
* @returns {Matrix2D} | ||
*/ | ||
Matrix2D.nonUniformScalingAt = function(scaleX, scaleY, center) { | ||
// TODO: calculate all math inline | ||
return Matrix2D | ||
.translation(center.x, center.y) | ||
.scaleNonUniform(scaleX, scaleY) | ||
.translate(-center.x, -center.y); | ||
}; | ||
/** | ||
* rotation | ||
* | ||
* @param {Number} radians | ||
* @returns {Matrix2D} | ||
*/ | ||
Matrix2D.rotation = function(radians) { | ||
let c = Math.cos(radians); | ||
let s = Math.sin(radians); | ||
return new Matrix2D(c, s, -s, c, 0, 0); | ||
}; | ||
/** | ||
* rotationAt | ||
* | ||
* @param {Number} radians | ||
* @param {Point2D} center | ||
* @returns {Matrix2D} | ||
*/ | ||
Matrix2D.rotationAt = function(radians, center) { | ||
// TODO: calculate all math inline | ||
return Matrix2D | ||
.translation(center.x, center.y) | ||
.rotate(radians) | ||
.translate(-center.x, -center.y); | ||
}; | ||
/** | ||
* rotationFromVector | ||
* | ||
* @param {Vector2D} | ||
* @returns {Matrix2D} | ||
*/ | ||
Matrix2D.rotationFromVector = function(vector) { | ||
var unit = vector.unit(); | ||
var c = unit.x; // cos | ||
var s = unit.y; // sin | ||
return new Matrix2D(c, s, -s, c, 0, 0); | ||
}; | ||
/** | ||
* xFlip | ||
* | ||
* @returns {Matrix2D} | ||
*/ | ||
Matrix2D.xFlip = function() { | ||
return new Matrix2D(-1, 0, 0, 1, 0, 0); | ||
}; | ||
/** | ||
* yFlip | ||
* | ||
* @returns {Matrix2D} | ||
*/ | ||
Matrix2D.yFlip = function() { | ||
return new Matrix2D(1, 0, 0, -1, 0, 0); | ||
}; | ||
/** | ||
* xSkew | ||
* | ||
* @param {Number} radians | ||
* @returns {Matrix2D} | ||
*/ | ||
Matrix2D.xSkew = function(radians) { | ||
var t = Math.tan(radians); | ||
return new Matrix2D(1, 0, t, 1, 0, 0); | ||
}; | ||
/** | ||
* ySkew | ||
* | ||
* @param {Number} radians | ||
* @returns {Matrix2D} | ||
*/ | ||
Matrix2D.ySkew = function(radians) { | ||
var t = Math.tan(radians); | ||
return new Matrix2D(1, t, 0, 1, 0, 0); | ||
}; | ||
// *** METHODS | ||
/** | ||
* multiply | ||
@@ -338,4 +469,4 @@ * | ||
this.b, | ||
this.a * t + this.c, | ||
this.b * t + this.d, | ||
this.c + this.a * t, | ||
this.d + this.b * t, | ||
this.e, | ||
@@ -342,0 +473,0 @@ this.f |
{ | ||
"name": "kld-affine", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "A collection of classes used in affine geometry", | ||
@@ -9,3 +9,3 @@ "author": { | ||
}, | ||
"license": "BSD", | ||
"license": "BSD-3-Clause", | ||
"repository": { | ||
@@ -12,0 +12,0 @@ "type": "git", |
let assert = require('assert'), | ||
Matrix2D = require('./../lib/Matrix2D'); | ||
lib = require('../index'), | ||
Matrix2D = lib.Matrix2D, | ||
Point2D = lib.Point2D, | ||
Vector2D = lib.Vector2D; | ||
describe('Matrix2D', () => { | ||
it("new matrix", () => { | ||
let m = new Matrix2D(); | ||
describe('Methods', () => { | ||
it("new matrix", () => { | ||
let m = new Matrix2D(); | ||
assert.equal(m.a, 1); | ||
assert.equal(m.b, 0); | ||
assert.equal(m.c, 0); | ||
assert.equal(m.d, 1); | ||
assert.equal(m.e, 0); | ||
assert.equal(m.f, 0); | ||
}); | ||
assert.equal(m.a, 1); | ||
assert.equal(m.b, 0); | ||
assert.equal(m.c, 0); | ||
assert.equal(m.d, 1); | ||
assert.equal(m.e, 0); | ||
assert.equal(m.f, 0); | ||
}); | ||
it("IDENTITY", () => { | ||
let m = Matrix2D.IDENTITY; | ||
it("to string", () => { | ||
let m = new Matrix2D(); | ||
assert.equal(m.a, 1); | ||
assert.equal(m.b, 0); | ||
assert.equal(m.c, 0); | ||
assert.equal(m.d, 1); | ||
assert.equal(m.e, 0); | ||
assert.equal(m.f, 0); | ||
assert.equal(m.toString(), "matrix(1,0,0,1,0,0)"); | ||
}); | ||
/* | ||
it("multiply", () => {}); | ||
it("inverse", () => {}); | ||
it("translate", () => {}); | ||
it("scale", () => {}); | ||
it("scaleAt", () => {}); | ||
it("scaleNonUniform", () => {}); | ||
it("scaleNonUniformAt", () => {}); | ||
it("rotate", () => {}); | ||
it("rotateAt", () => {}); | ||
it("rotateFromVector", () => {}); | ||
it("flipX", () => {}); | ||
it("flipY", () => {}); | ||
it("skewX", () => {}); | ||
it("skewY", () => {}); | ||
it("isIdentity", () => {}); | ||
it("isInvertible", () => {}); | ||
it("getScale", () => {}); | ||
it("equals", () => {}); | ||
*/ | ||
}); | ||
it("to string", () => { | ||
let m = new Matrix2D(); | ||
describe('Statics', () => { | ||
it("IDENTITY", () => { | ||
let m = Matrix2D.IDENTITY; | ||
assert.equal(m.toString(), "matrix(1,0,0,1,0,0)"); | ||
assert.equal(m.a, 1); | ||
assert.equal(m.b, 0); | ||
assert.equal(m.c, 0); | ||
assert.equal(m.d, 1); | ||
assert.equal(m.e, 0); | ||
assert.equal(m.f, 0); | ||
}); | ||
it("translation", () => { | ||
let tx = 10; | ||
let ty = 20; | ||
let m1 = Matrix2D.translation(tx, ty); | ||
let m2 = (new Matrix2D()).translate(tx, ty); | ||
assert(m1.equals(m2), `${m1.toString()} != ${m2.toString()}`); | ||
}); | ||
it("scaling", () => { | ||
let s = 1.5; | ||
let m1 = Matrix2D.scaling(s); | ||
let m2 = (new Matrix2D()).scale(s); | ||
assert(m1.equals(m2), `${m1.toString()} != ${m2.toString()}`); | ||
}); | ||
it("scalingAt", () => { | ||
let s = 1.5; | ||
let center = new Point2D(10, 20); | ||
let m1 = Matrix2D.scalingAt(s, center); | ||
let m2 = (new Matrix2D()).scaleAt(s, center); | ||
assert(m1.equals(m2), `${m1.toString()} != ${m2.toString()}`); | ||
}); | ||
it("non-uniform scaling", () => { | ||
let sx = 1.5; | ||
let sy = 0.5; | ||
let m1 = Matrix2D.nonUniformScaling(sx, sy); | ||
let m2 = (new Matrix2D()).scaleNonUniform(sx, sy); | ||
assert(m1.equals(m2), `${m1.toString()} != ${m2.toString()}`); | ||
}); | ||
it("non-uniform scalingAt", () => { | ||
let sx = 1.5; | ||
let sy = 0.5; | ||
let center = new Point2D(10, 20); | ||
let m1 = Matrix2D.nonUniformScalingAt(sx, sy, center); | ||
let m2 = (new Matrix2D()).scaleNonUniformAt(sx, sy, center); | ||
assert(m1.equals(m2), `${m1.toString()} != ${m2.toString()}`); | ||
}); | ||
it("rotation", () => { | ||
let a = 45.0 * Math.PI / 180.0; | ||
let m1 = Matrix2D.rotation(a); | ||
let m2 = (new Matrix2D()).rotate(a); | ||
assert(m1.equals(m2), `${m1.toString()} != ${m2.toString()}`); | ||
}); | ||
it("rotationAt", () => { | ||
let a = 45.0 * Math.PI / 180.0; | ||
let center = new Point2D(10, 20); | ||
let m1 = Matrix2D.rotationAt(a, center); | ||
let m2 = (new Matrix2D()).rotateAt(a, center); | ||
assert(m1.precisionEquals(m2, 1e-15), `${m1.toString()} != ${m2.toString()}`); | ||
}); | ||
it("rotation from vector", () => { | ||
let v = new Vector2D(10, 20); | ||
let m1 = Matrix2D.rotationFromVector(v); | ||
let m2 = (new Matrix2D()).rotateFromVector(v); | ||
assert(m1.equals(m2), `${m1.toString()} != ${m2.toString()}`); | ||
}); | ||
it("x flip", () => { | ||
let m1 = Matrix2D.xFlip(); | ||
let m2 = (new Matrix2D()).flipX(); | ||
assert(m1.equals(m2), `${m1.toString()} != ${m2.toString()}`); | ||
}); | ||
it("y flip", () => { | ||
let m1 = Matrix2D.yFlip(); | ||
let m2 = (new Matrix2D()).flipY(); | ||
assert(m1.equals(m2), `${m1.toString()} != ${m2.toString()}`); | ||
}); | ||
it("x skew", () => { | ||
let a = 30 * Math.PI / 180.0; | ||
let m1 = Matrix2D.xSkew(a); | ||
let m2 = (new Matrix2D()).skewX(a); | ||
assert(m1.equals(m2), `${m1.toString()} != ${m2.toString()}`); | ||
}); | ||
it("y skew", () => { | ||
let a = 30 * Math.PI / 180.0; | ||
let m1 = Matrix2D.ySkew(a); | ||
let m2 = (new Matrix2D()).skewY(a); | ||
assert(m1.equals(m2), `${m1.toString()} != ${m2.toString()}`); | ||
}); | ||
}); | ||
/* | ||
it("multiply", () => {}); | ||
it("inverse", () => {}); | ||
it("translate", () => {}); | ||
it("scale", () => {}); | ||
it("scaleAt", () => {}); | ||
it("scaleNonUniform", () => {}); | ||
it("scaleNonUniformAt", () => {}); | ||
it("rotate", () => {}); | ||
it("rotateAt", () => {}); | ||
it("rotateFromVector", () => {}); | ||
it("flipX", () => {}); | ||
it("flipY", () => {}); | ||
it("skewX", () => {}); | ||
it("skewY", () => {}); | ||
it("isIdentity", () => {}); | ||
it("isInvertible", () => {}); | ||
it("getScale", () => {}); | ||
it("equals", () => {}); | ||
*/ | ||
}); |
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
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
54564
28
0
1326