root / trunk / web / dojo / dojox / gfx / matrix.js
History | View | Annotate | Download (4.38 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.gfx.matrix"]){ |
||
9 | dojo._hasResource["dojox.gfx.matrix"]=true; |
||
10 | dojo.provide("dojox.gfx.matrix");
|
||
11 | (function(){
|
||
12 | var m=dojox.gfx.matrix;
|
||
13 | var _1={};
|
||
14 | m._degToRad=function(_2){ |
||
15 | return _1[_2]||(_1[_2]=(Math.PI*_2/180)); |
||
16 | }; |
||
17 | m._radToDeg=function(_3){ |
||
18 | return _3/Math.PI*180; |
||
19 | }; |
||
20 | m.Matrix2D=function(_4){ |
||
21 | if(_4){
|
||
22 | if(typeof _4=="number"){ |
||
23 | this.xx=this.yy=_4; |
||
24 | }else{
|
||
25 | if(_4 instanceof Array){ |
||
26 | if(_4.length>0){ |
||
27 | var _5=m.normalize(_4[0]); |
||
28 | for(var i=1;i<_4.length;++i){ |
||
29 | var l=_5,r=dojox.gfx.matrix.normalize(_4[i]);
|
||
30 | _5=new m.Matrix2D();
|
||
31 | _5.xx=l.xx*r.xx+l.xy*r.yx; |
||
32 | _5.xy=l.xx*r.xy+l.xy*r.yy; |
||
33 | _5.yx=l.yx*r.xx+l.yy*r.yx; |
||
34 | _5.yy=l.yx*r.xy+l.yy*r.yy; |
||
35 | _5.dx=l.xx*r.dx+l.xy*r.dy+l.dx; |
||
36 | _5.dy=l.yx*r.dx+l.yy*r.dy+l.dy; |
||
37 | } |
||
38 | dojo.mixin(this,_5);
|
||
39 | } |
||
40 | }else{
|
||
41 | dojo.mixin(this,_4);
|
||
42 | } |
||
43 | } |
||
44 | } |
||
45 | }; |
||
46 | dojo.extend(m.Matrix2D,{xx:1,xy:0,yx:0,yy:1,dx:0,dy:0}); |
||
47 | dojo.mixin(m,{identity:new m.Matrix2D(),flipX:new m.Matrix2D({xx:-1}),flipY:new m.Matrix2D({yy:-1}),flipXY:new m.Matrix2D({xx:-1,yy:-1}),translate:function(a,b){ |
||
48 | if(arguments.length>1){ |
||
49 | return new m.Matrix2D({dx:a,dy:b}); |
||
50 | } |
||
51 | return new m.Matrix2D({dx:a.x,dy:a.y}); |
||
52 | },scale:function(a,b){ |
||
53 | if(arguments.length>1){ |
||
54 | return new m.Matrix2D({xx:a,yy:b}); |
||
55 | } |
||
56 | if(typeof a=="number"){ |
||
57 | return new m.Matrix2D({xx:a,yy:a}); |
||
58 | } |
||
59 | return new m.Matrix2D({xx:a.x,yy:a.y}); |
||
60 | },rotate:function(_6){ |
||
61 | var c=Math.cos(_6);
|
||
62 | var s=Math.sin(_6);
|
||
63 | return new m.Matrix2D({xx:c,xy:-s,yx:s,yy:c}); |
||
64 | },rotateg:function(_7){ |
||
65 | return m.rotate(m._degToRad(_7));
|
||
66 | },skewX:function(_8){ |
||
67 | return new m.Matrix2D({xy:Math.tan(_8)}); |
||
68 | },skewXg:function(_9){ |
||
69 | return m.skewX(m._degToRad(_9));
|
||
70 | },skewY:function(_a){ |
||
71 | return new m.Matrix2D({yx:Math.tan(_a)}); |
||
72 | },skewYg:function(_b){ |
||
73 | return m.skewY(m._degToRad(_b));
|
||
74 | },reflect:function(a,b){ |
||
75 | if(arguments.length==1){ |
||
76 | b=a.y; |
||
77 | a=a.x; |
||
78 | } |
||
79 | var a2=a*a,b2=b*b,n2=a2+b2,xy=2*a*b/n2; |
||
80 | return new m.Matrix2D({xx:2*a2/n2-1,xy:xy,yx:xy,yy:2*b2/n2-1}); |
||
81 | },project:function(a,b){ |
||
82 | if(arguments.length==1){ |
||
83 | b=a.y; |
||
84 | a=a.x; |
||
85 | } |
||
86 | var a2=a*a,b2=b*b,n2=a2+b2,xy=a*b/n2;
|
||
87 | return new m.Matrix2D({xx:a2/n2,xy:xy,yx:xy,yy:b2/n2}); |
||
88 | },normalize:function(_c){ |
||
89 | return (_c instanceof m.Matrix2D)?_c:new m.Matrix2D(_c); |
||
90 | },clone:function(_d){ |
||
91 | var _e=new m.Matrix2D(); |
||
92 | for(var i in _d){ |
||
93 | if(typeof (_d[i])=="number"&&typeof (_e[i])=="number"&&_e[i]!=_d[i]){ |
||
94 | _e[i]=_d[i]; |
||
95 | } |
||
96 | } |
||
97 | return _e;
|
||
98 | },invert:function(_f){ |
||
99 | var M=m.normalize(_f),D=M.xx*M.yy-M.xy*M.yx,M=new m.Matrix2D({xx:M.yy/D,xy:-M.xy/D,yx:-M.yx/D,yy:M.xx/D,dx:(M.xy*M.dy-M.yy*M.dx)/D,dy:(M.yx*M.dx-M.xx*M.dy)/D}); |
||
100 | return M;
|
||
101 | },_multiplyPoint:function(_10,x,y){ |
||
102 | return {x:_10.xx*x+_10.xy*y+_10.dx,y:_10.yx*x+_10.yy*y+_10.dy}; |
||
103 | },multiplyPoint:function(_11,a,b){ |
||
104 | var M=m.normalize(_11);
|
||
105 | if(typeof a=="number"&&typeof b=="number"){ |
||
106 | return m._multiplyPoint(M,a,b);
|
||
107 | } |
||
108 | return m._multiplyPoint(M,a.x,a.y);
|
||
109 | },multiply:function(_12){ |
||
110 | var M=m.normalize(_12);
|
||
111 | for(var i=1;i<arguments.length;++i){ |
||
112 | var l=M,r=m.normalize(arguments[i]); |
||
113 | M=new m.Matrix2D();
|
||
114 | M.xx=l.xx*r.xx+l.xy*r.yx; |
||
115 | M.xy=l.xx*r.xy+l.xy*r.yy; |
||
116 | M.yx=l.yx*r.xx+l.yy*r.yx; |
||
117 | M.yy=l.yx*r.xy+l.yy*r.yy; |
||
118 | M.dx=l.xx*r.dx+l.xy*r.dy+l.dx; |
||
119 | M.dy=l.yx*r.dx+l.yy*r.dy+l.dy; |
||
120 | } |
||
121 | return M;
|
||
122 | },_sandwich:function(_13,x,y){ |
||
123 | return m.multiply(m.translate(x,y),_13,m.translate(-x,-y));
|
||
124 | },scaleAt:function(a,b,c,d){ |
||
125 | switch(arguments.length){ |
||
126 | case 4: |
||
127 | return m._sandwich(m.scale(a,b),c,d);
|
||
128 | case 3: |
||
129 | if(typeof c=="number"){ |
||
130 | return m._sandwich(m.scale(a),b,c);
|
||
131 | } |
||
132 | return m._sandwich(m.scale(a,b),c.x,c.y);
|
||
133 | } |
||
134 | return m._sandwich(m.scale(a),b.x,b.y);
|
||
135 | },rotateAt:function(_14,a,b){ |
||
136 | if(arguments.length>2){ |
||
137 | return m._sandwich(m.rotate(_14),a,b);
|
||
138 | } |
||
139 | return m._sandwich(m.rotate(_14),a.x,a.y);
|
||
140 | },rotategAt:function(_15,a,b){ |
||
141 | if(arguments.length>2){ |
||
142 | return m._sandwich(m.rotateg(_15),a,b);
|
||
143 | } |
||
144 | return m._sandwich(m.rotateg(_15),a.x,a.y);
|
||
145 | },skewXAt:function(_16,a,b){ |
||
146 | if(arguments.length>2){ |
||
147 | return m._sandwich(m.skewX(_16),a,b);
|
||
148 | } |
||
149 | return m._sandwich(m.skewX(_16),a.x,a.y);
|
||
150 | },skewXgAt:function(_17,a,b){ |
||
151 | if(arguments.length>2){ |
||
152 | return m._sandwich(m.skewXg(_17),a,b);
|
||
153 | } |
||
154 | return m._sandwich(m.skewXg(_17),a.x,a.y);
|
||
155 | },skewYAt:function(_18,a,b){ |
||
156 | if(arguments.length>2){ |
||
157 | return m._sandwich(m.skewY(_18),a,b);
|
||
158 | } |
||
159 | return m._sandwich(m.skewY(_18),a.x,a.y);
|
||
160 | },skewYgAt:function(_19,a,b){ |
||
161 | if(arguments.length>2){ |
||
162 | return m._sandwich(m.skewYg(_19),a,b);
|
||
163 | } |
||
164 | return m._sandwich(m.skewYg(_19),a.x,a.y);
|
||
165 | }}); |
||
166 | })(); |
||
167 | dojox.gfx.Matrix2D=dojox.gfx.matrix.Matrix2D; |
||
168 | } |