Socket
Socket
Sign inDemoInstall

kld-affine

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kld-affine - npm Package Compare versions

Comparing version 0.1.0 to 1.0.0

.editorconfig

99

lib/Matrix2D.js

@@ -11,2 +11,5 @@ /**

* Matrix2D
* [a c e]
* [b d f]
* [0 0 1]
*

@@ -74,2 +77,3 @@ * @param {Number} a

Matrix2D.IDENTITY = new Matrix2D(1, 0, 0, 1, 0, 0);
Matrix2D.IDENTITY.isIdentity = function () { return true; };

@@ -84,4 +88,12 @@ // TODO: rotate, skew, etc. matrices as well?

*/
Matrix2D.prototype.multiply = function(that) {
return new Matrix2D(
Matrix2D.prototype.multiply = function (that) {
if (this.isIdentity()) {
return that;
}
if (that.isIdentity()){
return this;
}
return new this.constructor(
this.a * that.a + this.c * that.b,

@@ -101,7 +113,12 @@ this.b * that.a + this.d * that.b,

*/
Matrix2D.prototype.inverse = function() {
Matrix2D.prototype.inverse = function () {
if (this.isIdentity()){
return this;
}
var det1 = this.a * this.d - this.b * this.c;
if ( det1 == 0.0 )
if ( det1 === 0.0 ) {
throw("Matrix is not invertible");
}

@@ -112,3 +129,3 @@ var idet = 1.0 / det1;

return new Matrix2D(
return new this.constructor(
this.d * idet,

@@ -131,3 +148,3 @@ -this.b * idet,

Matrix2D.prototype.translate = function(tx, ty) {
return new Matrix2D(
return new this.constructor(
this.a,

@@ -149,3 +166,3 @@ this.b,

Matrix2D.prototype.scale = function(scale) {
return new Matrix2D(
return new this.constructor(
this.a * scale,

@@ -171,3 +188,3 @@ this.b * scale,

return new Matrix2D(
return new this.constructor(
this.a * scale,

@@ -190,3 +207,3 @@ this.b * scale,

Matrix2D.prototype.scaleNonUniform = function(scaleX, scaleY) {
return new Matrix2D(
return new this.constructor(
this.a * scaleX,

@@ -213,3 +230,3 @@ this.b * scaleX,

return new Matrix2D(
return new this.constructor(
this.a * scaleX,

@@ -234,3 +251,3 @@ this.b * scaleX,

return new Matrix2D(
return new this.constructor(
this.a * c + this.c * s,

@@ -258,3 +275,3 @@ this.b * c + this.d * s,

return new Matrix2D(
return new this.constructor(
this.a * c + this.c * s,

@@ -280,3 +297,3 @@ this.b * c + this.d * s,

return new Matrix2D(
return new this.constructor(
this.a * c + this.c * s,

@@ -297,3 +314,3 @@ this.b * c + this.d * s,

Matrix2D.prototype.flipX = function() {
return new Matrix2D(
return new this.constructor(
-this.a,

@@ -314,3 +331,3 @@ -this.b,

Matrix2D.prototype.flipY = function() {
return new Matrix2D(
return new this.constructor(
this.a,

@@ -334,3 +351,3 @@ this.b,

return new Matrix2D(
return new this.constructor(
this.a,

@@ -354,5 +371,5 @@ this.b,

Matrix2D.prototype.skewY = function(radians) {
var t = Math.tan(angle);
var t = Math.tan(radians);
return matrix_new(
return new this.constructor(
this.a + this.c * t,

@@ -391,3 +408,3 @@ this.b + this.d * t,

Matrix2D.prototype.isInvertible = function() {
this.a * this.d - this.b * this.c !== 0.0;
return this.a * this.d - this.b * this.c !== 0.0;
};

@@ -407,3 +424,43 @@

///////////////////////////////////////////////////////////////////
/**
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
@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 {{ 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;
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)
};
};
/**
* equals

@@ -440,6 +497,6 @@ *

);
}
};
if (typeof module !== "undefined") {
module.exports = Matrix2D;
}
}

@@ -41,3 +41,3 @@ /**

Point2D.prototype.clone = function() {
return new Point2D(this.x, this.y);
return new this.constructor(this.x, this.y);
};

@@ -52,3 +52,3 @@

Point2D.prototype.add = function(that) {
return new Point2D(this.x+that.x, this.y+that.y);
return new this.constructor(this.x+that.x, this.y+that.y);
};

@@ -63,3 +63,3 @@

Point2D.prototype.subtract = function(that) {
return new Point2D(this.x-that.x, this.y-that.y);
return new this.constructor(this.x-that.x, this.y-that.y);
};

@@ -74,3 +74,3 @@

Point2D.prototype.multiply = function(scalar) {
return new Point2D(this.x*scalar, this.y*scalar);
return new this.constructor(this.x*scalar, this.y*scalar);
};

@@ -85,3 +85,3 @@

Point2D.prototype.divide = function(scalar) {
return new Point2D(this.x/scalar, this.y/scalar);
return new this.constructor(this.x/scalar, this.y/scalar);
};

@@ -96,3 +96,3 @@

Point2D.prototype.equals = function(that) {
return ( this.x == that.x && this.y == that.y );
return ( this.x === that.x && this.y === that.y );
};

@@ -112,3 +112,3 @@

return new Point2D(
return new this.constructor(
this.x * omt + that.x * t,

@@ -139,3 +139,3 @@ this.y * omt + that.y * t

Point2D.prototype.min = function(that) {
return new Point2D(
return new this.constructor(
Math.min( this.x, that.x ),

@@ -153,3 +153,3 @@ Math.min( this.y, that.y )

Point2D.prototype.max = function(that) {
return new Point2D(
return new this.constructor(
Math.max( this.x, that.x ),

@@ -167,3 +167,3 @@ Math.max( this.y, that.y )

Point2D.prototype.transform = function(matrix) {
return new Point2D(
return new this.constructor(
matrix.a * this.x + matrix.c * this.y + matrix.e,

@@ -170,0 +170,0 @@ matrix.b * this.x + matrix.d * this.y + matrix.f

@@ -43,3 +43,3 @@ /**

Vector2D.fromPoints = function(p1, p2) {
return new Vector2D(
return new this.constructor(
p2.x - p1.x,

@@ -86,3 +86,3 @@ p2.y - p1.y

return this.x*that.y - this.y*that.x;
}
};

@@ -115,3 +115,3 @@ /**

Vector2D.prototype.add = function(that) {
return new Vector2D(this.x + that.x, this.y + that.y);
return new this.constructor(this.x + that.x, this.y + that.y);
};

@@ -126,3 +126,3 @@

Vector2D.prototype.subtract = function(that) {
return new Vector2D(this.x - that.x, this.y - that.y);
return new this.constructor(this.x - that.x, this.y - that.y);
};

@@ -137,3 +137,3 @@

Vector2D.prototype.multiply = function(scalar) {
return new Vector2D(this.x * scalar, this.y * scalar);
return new this.constructor(this.x * scalar, this.y * scalar);
};

@@ -148,3 +148,3 @@

Vector2D.prototype.divide = function(scalar) {
return new Vector2D(this.x / scalar, this.y / scalar);
return new this.constructor(this.x / scalar, this.y / scalar);
};

@@ -160,8 +160,3 @@

var cos = this.dot(that) / (this.length() * that.length());
if (cos < -1) {
cos = -1;
}
else if (cos > 1) {
cos = 1;
}
cos = Math.max(-1, Math.min(cos, 1));
var radians = Math.acos(cos);

@@ -178,3 +173,3 @@

Vector2D.prototype.perp = function() {
return new Vector2D(-this.y, this.x);
return new this.constructor(-this.y, this.x);
};

@@ -212,3 +207,3 @@

Vector2D.prototype.transform = function(matrix) {
return new Vector2D(
return new this.constructor(
matrix.a * this.x + matrix.c * this.y,

@@ -215,0 +210,0 @@ matrix.b * this.x + matrix.d * this.y

{
"name": "kld-affine",
"version": "0.1.0",
"version": "1.0.0",
"description": "A collection of classes used in affine geometry",

@@ -5,0 +5,0 @@ "author": {

@@ -165,3 +165,3 @@ var Point2D = require('../lib/Point2D'),

var p1 = new Point2D(10, 0);
var v = new Vector2D(Math.PI / 4.0, Math.PI / 4.0)
var v = new Vector2D(Math.PI / 4.0, Math.PI / 4.0);
var m = new Matrix2D().rotateFromVector(v);

@@ -168,0 +168,0 @@ var p2 = p1.transform(m);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc