Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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