kld-affine
Advanced tools
Comparing version 1.0.0 to 2.0.0
/** | ||
* | ||
* Matrix2D.js | ||
* | ||
* copyright 2001-2002, 2013 Kevin Lindsey | ||
* | ||
*/ | ||
@@ -11,2 +9,3 @@ | ||
* Matrix2D | ||
* | ||
* [a c e] | ||
@@ -63,8 +62,2 @@ * [b d f] | ||
}); | ||
// this.a = (a !== undefined) ? a : 1; | ||
// this.b = (b !== undefined) ? b : 0; | ||
// this.c = (c !== undefined) ? c : 0; | ||
// this.d = (d !== undefined) ? d : 1; | ||
// this.e = (e !== undefined) ? e : 0; | ||
// this.f = (f !== undefined) ? f : 0; | ||
} | ||
@@ -77,7 +70,6 @@ | ||
*/ | ||
// 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; }; | ||
// TODO: rotate, skew, etc. matrices as well? | ||
/** | ||
@@ -94,3 +86,3 @@ * multiply | ||
if (that.isIdentity()){ | ||
if (that.isIdentity()) { | ||
return this; | ||
@@ -115,3 +107,3 @@ } | ||
Matrix2D.prototype.inverse = function () { | ||
if (this.isIdentity()){ | ||
if (this.isIdentity()) { | ||
return this; | ||
@@ -402,3 +394,3 @@ } | ||
* | ||
* @returns {scaleX: Number, scaleY: Number} | ||
* @returns {{ scaleX: Number, scaleY: Number }} | ||
*/ | ||
@@ -412,39 +404,39 @@ Matrix2D.prototype.getScale = function() { | ||
/////////////////////////////////////////////////////////////////// | ||
/** | ||
Calculates matrix Singular Value Decomposition. <br/> | ||
Result are matrices T, R, S, R0 which multiplication gives this matrix. | ||
T - translation matrix | ||
R - rotation matrix | ||
S - scale matrix | ||
R0 - rotation matrix | ||
* getDecomposition | ||
* | ||
* Calculates matrix Singular Value Decomposition | ||
* | ||
* The resulting matrices, translation, rotation, scale, and rotation0, return | ||
* this matrix when they are muliplied together in the listed order | ||
* | ||
* @see Jim Blinn's article {@link http://dx.doi.org/10.1109/38.486688} | ||
* @see {@link http://math.stackexchange.com/questions/861674/decompose-a-2d-arbitrary-transform-into-only-scaling-and-rotation} | ||
* | ||
* @returns {{ translation: Matrix2D, rotation: Matrix2D, scale: Matrix2D, rotation0: Matrix2D }} | ||
*/ | ||
Matrix2D.prototype.getDecomposition = function () { | ||
var E = (this.a + this.d) * 0.5; | ||
var F = (this.a - this.d) * 0.5; | ||
var G = (this.b + this.c) * 0.5; | ||
var H = (this.b - this.c) * 0.5; | ||
@see Jim Blinn's article {@link http://dx.doi.org/10.1109/38.486688} | ||
@see {@link http://math.stackexchange.com/questions/861674/decompose-a-2d-arbitrary-transform-into-only-scaling-and-rotation} | ||
var Q = Math.sqrt(E * E + H * H); | ||
var R = Math.sqrt(F * F + G * G); | ||
var scaleX = Q + R; | ||
var scaleY = Q - R; | ||
@returns {{ T: Matrix2D, R: Matrix2D, S: Matrix2D, R0: Matrix2D }} | ||
*/ | ||
Matrix2D.prototype.getDecompositionTRSR = function () { | ||
var m00 = this.a; | ||
var m10 = this.b; | ||
var m01 = this.c; | ||
var m11 = this.d; | ||
var E = (m00 + m11) / 2; | ||
var F = (m00 - m11) / 2; | ||
var G = (m10 + m01) / 2; | ||
var H = (m10 - m01) / 2; | ||
var Q = Math.sqrt(E * E + H * H); | ||
var R = Math.sqrt(F * F + G * G); | ||
var sx = Q + R; | ||
var sy = Q - R; | ||
var a1 = Math.atan2(G, F); | ||
var a2 = Math.atan2(H, E); | ||
var theta = (a2 - a1) / 2; | ||
var phi = (a2 + a1) / 2; | ||
var a1 = Math.atan2(G, F); | ||
var a2 = Math.atan2(H, E); | ||
var theta = (a2 - a1) * 0.5; | ||
var phi = (a2 + a1) * 0.5; | ||
// TODO: Add static methods to generate translation, rotation, etc. | ||
// matrices directly | ||
return { | ||
T: new this.constructor(1, 0, 0, 1, this.e, this.f), | ||
R: this.constructor.IDENTITY.rotate(phi), | ||
S: new this.constructor(sx, 0, 0, sy, 0, 0), | ||
R0: this.constructor.IDENTITY.rotate(theta) | ||
translation: new this.constructor(1, 0, 0, 1, this.e, this.f), | ||
rotation: this.constructor.IDENTITY.rotate(phi), | ||
scale: new this.constructor(scaleX, 0, 0, scaleY, 0, 0), | ||
rotation0: this.constructor.IDENTITY.rotate(theta) | ||
}; | ||
@@ -476,11 +468,3 @@ }; | ||
Matrix2D.prototype.toString = function() { | ||
return ( | ||
"matrix(" + | ||
this.a + "," + | ||
this.b + "," + | ||
this.c + "," + | ||
this.d + "," + | ||
this.e + "," + | ||
this.f + ")" | ||
); | ||
return "matrix(" + [this.a, this.b, this.c, this.d, this.e, this.f].join(",") + ")"; | ||
}; | ||
@@ -487,0 +471,0 @@ |
@@ -43,3 +43,3 @@ /** | ||
Vector2D.fromPoints = function(p1, p2) { | ||
return new this.constructor( | ||
return new Vector2D( | ||
p2.x - p1.x, | ||
@@ -46,0 +46,0 @@ p2.y - p1.y |
{ | ||
"name": "kld-affine", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "A collection of classes used in affine geometry", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -10,2 +10,9 @@ var Vector2D = require('../lib/Vector2D'); | ||
exports.vectorFromPoints = function(beforeExit, assert) { | ||
var v = Vector2D.fromPoints({ x : 0, y : 0 }, { x : 10, y : 20 }); | ||
assert.equal(v.x, 10); | ||
assert.equal(v.y, 20); | ||
}; | ||
exports.fromPoints = function(beforeExit, assert) { | ||
@@ -12,0 +19,0 @@ }; |
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
30796
1067