-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix2D.js
More file actions
113 lines (94 loc) · 2.77 KB
/
Copy pathMatrix2D.js
File metadata and controls
113 lines (94 loc) · 2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
function Matrix2D(
n11, n12, n13,
n21, n22, n23,
n31, n32, n33
){
this.n11 = n11 || 1;
this.n12 = n12 || 0;
this.n13 = n13 || 0;
this.n21 = n21 || 0;
this.n22 = n22 || 1;
this.n23 = n23 || 0;
this.n31 = n31 || 0;
this.n32 = n32 || 0;
this.n33 = n33 || 1;
}
Matrix2D.prototype.clone = function()
{
return new Matrix2D(this.n11, this.n12, this.n13, this.n21, this.n22, this.n23, this.n31, this.n32, this.n33);
}
Matrix2D.prototype.concat = function(m)
{
var values = {};
values.n11 = this.n11 * m.n11 + this.n12 * m.n21 + this.n13 * m.n31;
values.n12 = this.n11 * m.n12 + this.n12 * m.n22 + this.n13 * m.n32;
values.n13 = this.n11 * m.n13 + this.n12 * m.n23 + this.n13 * m.n33;
values.n21 = this.n21 * m.n11 + this.n22 * m.n21 + this.n23 * m.n31;
values.n22 = this.n21 * m.n12 + this.n22 * m.n22 + this.n23 * m.n32;
values.n23 = this.n21 * m.n13 + this.n22 * m.n23 + this.n23 * m.n33;
values.n31 = this.n31 * m.n11 + this.n32 * m.n21 + this.n33 * m.n31;
values.n32 = this.n31 * m.n12 + this.n32 * m.n22 + this.n33 * m.n32;
values.n33 = this.n31 * m.n13 + this.n32 * m.n23 + this.n33 * m.n33;
this.initialize(values);
}
Matrix2D.prototype.initialize = function(values)
{
for(var i in values) this[i] = values[i];
}
Matrix2D.prototype.createBox = function(scalex, scaley, scalez, rotationx, rotationy, rotationz, tx, ty, tz)
{
this.identity();
if (rotationx != 0) this.rotateX(rotationx);
if (rotationy != 0) this.rotateY(rotationy);
if (rotationz != 0) this.rotateZ(rotationz);
if (scalex != 1 || scaley != 1 || scalez != 1) this.scale(scalex, scaley, scalez);
if (tx != 0 || ty != 0 || tz != 0) this.translate(tx, ty, tz);
}
Matrix2D.prototype.identity = function()
{
this.initialize({n11:1, n12:0, n13:0, n21:0, n22:1, n23:0, n31:0, n32:0, n33:1});
}
Matrix2D.prototype.rotate = function(angle)
{
var sin = Math.sin(angle);
var cos = Math.cos(angle);
this.concat(new Matrix2D(
cos, sin, 0,
-sin, cos, 0,
0, 0, 1)
);
}
Matrix2D.prototype.scale = function(sx, sy)
{
this.concat(new Matrix2D(
sx, 0, 0,
0, sy, 0,
0, 0, 1)
);
}
Matrix2D.prototype.translate = function(dx, dy)
{
this.n31 += dx;
this.n32 += dy;
}
Matrix2D.prototype.transformArray=function(arr)
{
var rVal=[];
var numPoints=arr.length/2;
for(var i=0;i<numPoints;i++)
{
var i2=i*2;
var x=arr[i2];
var y=arr[i2+1];
rVal[i2]=this.n11*x+this.n21*y+this.n31;
rVal[i2+1]=this.n12*x+this.n22*y+this.n32;
}
return rVal;
}
Matrix2D.prototype.toString = function()
{
return this.n11+","+this.n12+","+this.n13+","+this.n14+","+
this.n21+","+this.n22+","+this.n23+","+this.n24+","+
this.n31+","+this.n32+","+this.n33+","+this.n34+","+
this.n41+","+this.n42+","+this.n43+","+this.n44;
}