1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
dojo.provide("dojo.gfx.matrix");
|
|
|
12 |
dojo.require("dojo.lang.common");
|
|
|
13 |
dojo.require("dojo.math.*");
|
|
|
14 |
dojo.gfx.matrix.Matrix2D = function (arg) {
|
|
|
15 |
if (arg) {
|
|
|
16 |
if (arg instanceof Array) {
|
|
|
17 |
if (arg.length > 0) {
|
|
|
18 |
var m = dojo.gfx.matrix.normalize(arg[0]);
|
|
|
19 |
for (var i = 1; i < arg.length; ++i) {
|
|
|
20 |
var l = m;
|
|
|
21 |
var r = dojo.gfx.matrix.normalize(arg[i]);
|
|
|
22 |
m = new dojo.gfx.matrix.Matrix2D();
|
|
|
23 |
m.xx = l.xx * r.xx + l.xy * r.yx;
|
|
|
24 |
m.xy = l.xx * r.xy + l.xy * r.yy;
|
|
|
25 |
m.yx = l.yx * r.xx + l.yy * r.yx;
|
|
|
26 |
m.yy = l.yx * r.xy + l.yy * r.yy;
|
|
|
27 |
m.dx = l.xx * r.dx + l.xy * r.dy + l.dx;
|
|
|
28 |
m.dy = l.yx * r.dx + l.yy * r.dy + l.dy;
|
|
|
29 |
}
|
|
|
30 |
dojo.mixin(this, m);
|
|
|
31 |
}
|
|
|
32 |
} else {
|
|
|
33 |
dojo.mixin(this, arg);
|
|
|
34 |
}
|
|
|
35 |
}
|
|
|
36 |
};
|
|
|
37 |
dojo.extend(dojo.gfx.matrix.Matrix2D, {xx:1, xy:0, yx:0, yy:1, dx:0, dy:0});
|
|
|
38 |
dojo.mixin(dojo.gfx.matrix, {identity:new dojo.gfx.matrix.Matrix2D(), flipX:new dojo.gfx.matrix.Matrix2D({xx:-1}), flipY:new dojo.gfx.matrix.Matrix2D({yy:-1}), flipXY:new dojo.gfx.matrix.Matrix2D({xx:-1, yy:-1}), translate:function (a, b) {
|
|
|
39 |
if (arguments.length > 1) {
|
|
|
40 |
return new dojo.gfx.matrix.Matrix2D({dx:a, dy:b});
|
|
|
41 |
}
|
|
|
42 |
return new dojo.gfx.matrix.Matrix2D({dx:a.x, dy:a.y});
|
|
|
43 |
}, scale:function (a, b) {
|
|
|
44 |
if (arguments.length > 1) {
|
|
|
45 |
return new dojo.gfx.matrix.Matrix2D({xx:a, yy:b});
|
|
|
46 |
}
|
|
|
47 |
if (typeof a == "number") {
|
|
|
48 |
return new dojo.gfx.matrix.Matrix2D({xx:a, yy:a});
|
|
|
49 |
}
|
|
|
50 |
return new dojo.gfx.matrix.Matrix2D({xx:a.x, yy:a.y});
|
|
|
51 |
}, rotate:function (angle) {
|
|
|
52 |
var c = Math.cos(angle);
|
|
|
53 |
var s = Math.sin(angle);
|
|
|
54 |
return new dojo.gfx.matrix.Matrix2D({xx:c, xy:s, yx:-s, yy:c});
|
|
|
55 |
}, rotateg:function (degree) {
|
|
|
56 |
return dojo.gfx.matrix.rotate(dojo.math.degToRad(degree));
|
|
|
57 |
}, skewX:function (angle) {
|
|
|
58 |
return new dojo.gfx.matrix.Matrix2D({xy:Math.tan(angle)});
|
|
|
59 |
}, skewXg:function (degree) {
|
|
|
60 |
return dojo.gfx.matrix.skewX(dojo.math.degToRad(degree));
|
|
|
61 |
}, skewY:function (angle) {
|
|
|
62 |
return new dojo.gfx.matrix.Matrix2D({yx:-Math.tan(angle)});
|
|
|
63 |
}, skewYg:function (degree) {
|
|
|
64 |
return dojo.gfx.matrix.skewY(dojo.math.degToRad(degree));
|
|
|
65 |
}, normalize:function (matrix) {
|
|
|
66 |
return (matrix instanceof dojo.gfx.matrix.Matrix2D) ? matrix : new dojo.gfx.matrix.Matrix2D(matrix);
|
|
|
67 |
}, clone:function (matrix) {
|
|
|
68 |
var obj = new dojo.gfx.matrix.Matrix2D();
|
|
|
69 |
for (var i in matrix) {
|
|
|
70 |
if (typeof (matrix[i]) == "number" && typeof (obj[i]) == "number" && obj[i] != matrix[i]) {
|
|
|
71 |
obj[i] = matrix[i];
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
return obj;
|
|
|
75 |
}, invert:function (matrix) {
|
|
|
76 |
var m = dojo.gfx.matrix.normalize(matrix);
|
|
|
77 |
var D = m.xx * m.yy - m.xy * m.yx;
|
|
|
78 |
var M = new dojo.gfx.matrix.Matrix2D({xx:m.yy / D, xy:-m.xy / D, yx:-m.yx / D, yy:m.xx / D, dx:(m.yx * m.dy - m.yy * m.dx) / D, dy:(m.xy * m.dx - m.xx * m.dy) / D});
|
|
|
79 |
return M;
|
|
|
80 |
}, _multiplyPoint:function (m, x, y) {
|
|
|
81 |
return {x:m.xx * x + m.xy * y + m.dx, y:m.yx * x + m.yy * y + m.dy};
|
|
|
82 |
}, multiplyPoint:function (matrix, a, b) {
|
|
|
83 |
var m = dojo.gfx.matrix.normalize(matrix);
|
|
|
84 |
if (typeof a == "number" && typeof b == "number") {
|
|
|
85 |
return dojo.gfx.matrix._multiplyPoint(m, a, b);
|
|
|
86 |
}
|
|
|
87 |
return dojo.gfx.matrix._multiplyPoint(m, a.x, a.y);
|
|
|
88 |
}, multiply:function (matrix) {
|
|
|
89 |
var m = dojo.gfx.matrix.normalize(matrix);
|
|
|
90 |
for (var i = 1; i < arguments.length; ++i) {
|
|
|
91 |
var l = m;
|
|
|
92 |
var r = dojo.gfx.matrix.normalize(arguments[i]);
|
|
|
93 |
m = new dojo.gfx.matrix.Matrix2D();
|
|
|
94 |
m.xx = l.xx * r.xx + l.xy * r.yx;
|
|
|
95 |
m.xy = l.xx * r.xy + l.xy * r.yy;
|
|
|
96 |
m.yx = l.yx * r.xx + l.yy * r.yx;
|
|
|
97 |
m.yy = l.yx * r.xy + l.yy * r.yy;
|
|
|
98 |
m.dx = l.xx * r.dx + l.xy * r.dy + l.dx;
|
|
|
99 |
m.dy = l.yx * r.dx + l.yy * r.dy + l.dy;
|
|
|
100 |
}
|
|
|
101 |
return m;
|
|
|
102 |
}, _sandwich:function (m, x, y) {
|
|
|
103 |
return dojo.gfx.matrix.multiply(dojo.gfx.matrix.translate(x, y), m, dojo.gfx.matrix.translate(-x, -y));
|
|
|
104 |
}, scaleAt:function (a, b, c, d) {
|
|
|
105 |
switch (arguments.length) {
|
|
|
106 |
case 4:
|
|
|
107 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.scale(a, b), c, d);
|
|
|
108 |
case 3:
|
|
|
109 |
if (typeof c == "number") {
|
|
|
110 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.scale(a), b, c);
|
|
|
111 |
}
|
|
|
112 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.scale(a, b), c.x, c.y);
|
|
|
113 |
}
|
|
|
114 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.scale(a), b.x, b.y);
|
|
|
115 |
}, rotateAt:function (angle, a, b) {
|
|
|
116 |
if (arguments.length > 2) {
|
|
|
117 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.rotate(angle), a, b);
|
|
|
118 |
}
|
|
|
119 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.rotate(angle), a.x, a.y);
|
|
|
120 |
}, rotategAt:function (degree, a, b) {
|
|
|
121 |
if (arguments.length > 2) {
|
|
|
122 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.rotateg(degree), a, b);
|
|
|
123 |
}
|
|
|
124 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.rotateg(degree), a.x, a.y);
|
|
|
125 |
}, skewXAt:function (angle, a, b) {
|
|
|
126 |
if (arguments.length > 2) {
|
|
|
127 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.skewX(angle), a, b);
|
|
|
128 |
}
|
|
|
129 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.skewX(angle), a.x, a.y);
|
|
|
130 |
}, skewXgAt:function (degree, a, b) {
|
|
|
131 |
if (arguments.length > 2) {
|
|
|
132 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.skewXg(degree), a, b);
|
|
|
133 |
}
|
|
|
134 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.skewXg(degree), a.x, a.y);
|
|
|
135 |
}, skewYAt:function (angle, a, b) {
|
|
|
136 |
if (arguments.length > 2) {
|
|
|
137 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.skewY(angle), a, b);
|
|
|
138 |
}
|
|
|
139 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.skewY(angle), a.x, a.y);
|
|
|
140 |
}, skewYgAt:function (degree, a, b) {
|
|
|
141 |
if (arguments.length > 2) {
|
|
|
142 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.skewYg(degree), a, b);
|
|
|
143 |
}
|
|
|
144 |
return dojo.gfx.matrix._sandwich(dojo.gfx.matrix.skewYg(degree), a.x, a.y);
|
|
|
145 |
}});
|
|
|
146 |
|