-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmatrix.js
More file actions
66 lines (62 loc) · 1.77 KB
/
Copy pathmatrix.js
File metadata and controls
66 lines (62 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function Matrix(a, b, c, d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
Matrix.fromRotation = function(angle) {
angle = angle * Math.PI / 180
var sinA = Math.sin(angle);
var cosA = Math.cos(angle);
return new Matrix(cosA, sinA, -sinA, cosA);
}
Matrix.fromCoordinateAxes = function(xAxis, yAxis) {
return new Matrix(
xAxis.y, yAxis.x,
xAxis.x, yAxis.y);
}
Matrix.fromPrincipleAxis = function(axis) {
return new Matrix(
axis.y, axis.x,
-axis.x, axis.y);
}
Matrix.prototype = {
determinant: function() {
return this.a * this.d - this.b * this.c;
},
inverse: function() {
if (this.inverseMatrix == null) {
this.inverseMatrix = new Matrix(this.d, -this.b, -this.c, this.a).divideBy(this.determinant());
this.inverseMatrix.inverseMatrix = this;
}
return this.inverseMatrix;
},
divideBy: function(k) {
return new Matrix(this.a / k, this.b / k, this.c / k, this.d / k);
},
times: function(that) {
if(that instanceof Matrix)
return new Matrix(
this.a * that.a + this.b * that.c,
this.a * that.b + this.b * that.d,
this.c * that.a + this.d * that.c,
this.c * that.b + this.d * that.d);
else if(that instanceof Vector)
return new Vector(
this.a * that.x + this.b * that.y,
this.c * that.x + this.d * that.y);
else
return new Matrix(this.a * that, this.b * that, this.c * that, this.d * that);
},
toString: function() {
alert("...");
return "Matrix: [[" + this.a + "," + this.b + "],[" + this.c + "," + this.d + "]]";
},
equals: function(that) {
return that != null && this.a == that.a && this.b == that.b && this.c == that.c && this.d == that.d;
}
};
Matrix.identity = new Matrix(1, 0, 0, 1);
Matrix.rotate90 = new Matrix(0, 1, -1, 0);
Matrix.rotate180 = new Matrix(-1, 0, 0, -1)
Matrix.rotate270 = new Matrix(0, -1, 1, 0);