root / trunk / web / dojo / dojox / gfx / decompose.js @ 11
History | View | Annotate | Download (2.61 KB)
1 |
/*
|
---|---|
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.decompose"]){ |
9 |
dojo._hasResource["dojox.gfx.decompose"]=true; |
10 |
dojo.provide("dojox.gfx.decompose");
|
11 |
dojo.require("dojox.gfx.matrix");
|
12 |
(function(){
|
13 |
var m=dojox.gfx.matrix;
|
14 |
var eq=function(a,b){ |
15 |
return Math.abs(a-b)<=0.000001*(Math.abs(a)+Math.abs(b)); |
16 |
}; |
17 |
var _1=function(r1,m1,r2,m2){ |
18 |
if(!isFinite(r1)){
|
19 |
return r2;
|
20 |
}else{
|
21 |
if(!isFinite(r2)){
|
22 |
return r1;
|
23 |
} |
24 |
} |
25 |
m1=Math.abs(m1),m2=Math.abs(m2); |
26 |
return (m1*r1+m2*r2)/(m1+m2);
|
27 |
}; |
28 |
var _2=function(_3){ |
29 |
var M=new m.Matrix2D(_3); |
30 |
return dojo.mixin(M,{dx:0,dy:0,xy:M.yx,yx:M.xy}); |
31 |
}; |
32 |
var _4=function(_5){ |
33 |
return (_5.xx*_5.yy<0||_5.xy*_5.yx>0)?-1:1; |
34 |
}; |
35 |
var _6=function(_7){ |
36 |
var M=m.normalize(_7),b=-M.xx-M.yy,c=M.xx*M.yy-M.xy*M.yx,d=Math.sqrt(b*b-4*c),l1=-(b+(b<0?-d:d))/2,l2=c/l1,_8=M.xy/(l1-M.xx),_9=1,_a=M.xy/(l2-M.xx),_b=1; |
37 |
if(eq(l1,l2)){
|
38 |
_8=1,_9=0,_a=0,_b=1; |
39 |
} |
40 |
if(!isFinite(_8)){
|
41 |
_8=1,_9=(l1-M.xx)/M.xy;
|
42 |
if(!isFinite(_9)){
|
43 |
_8=(l1-M.yy)/M.yx,_9=1;
|
44 |
if(!isFinite(_8)){
|
45 |
_8=1,_9=M.yx/(l1-M.yy);
|
46 |
} |
47 |
} |
48 |
} |
49 |
if(!isFinite(_a)){
|
50 |
_a=1,_b=(l2-M.xx)/M.xy;
|
51 |
if(!isFinite(_b)){
|
52 |
_a=(l2-M.yy)/M.yx,_b=1;
|
53 |
if(!isFinite(_a)){
|
54 |
_a=1,_b=M.yx/(l2-M.yy);
|
55 |
} |
56 |
} |
57 |
} |
58 |
var d1=Math.sqrt(_8*_8+_9*_9),d2=Math.sqrt(_a*_a+_b*_b);
|
59 |
if(!isFinite(_8/=d1)){
|
60 |
_8=0;
|
61 |
} |
62 |
if(!isFinite(_9/=d1)){
|
63 |
_9=0;
|
64 |
} |
65 |
if(!isFinite(_a/=d2)){
|
66 |
_a=0;
|
67 |
} |
68 |
if(!isFinite(_b/=d2)){
|
69 |
_b=0;
|
70 |
} |
71 |
return {value1:l1,value2:l2,vector1:{x:_8,y:_9},vector2:{x:_a,y:_b}}; |
72 |
}; |
73 |
var _c=function(M,_d){ |
74 |
var _e=_4(M),a=_d.angle1=(Math.atan2(M.yx,M.yy)+Math.atan2(-_e*M.xy,_e*M.xx))/2,_f=Math.cos(a),sin=Math.sin(a); |
75 |
_d.sx=_1(M.xx/_f,_f,-M.xy/sin,sin); |
76 |
_d.sy=_1(M.yy/_f,_f,M.yx/sin,sin); |
77 |
return _d;
|
78 |
}; |
79 |
var _10=function(M,_11){ |
80 |
var _12=_4(M),a=_11.angle2=(Math.atan2(_12*M.yx,_12*M.xx)+Math.atan2(-M.xy,M.yy))/2,cos=Math.cos(a),sin=Math.sin(a); |
81 |
_11.sx=_1(M.xx/cos,cos,M.yx/sin,sin); |
82 |
_11.sy=_1(M.yy/cos,cos,-M.xy/sin,sin); |
83 |
return _11;
|
84 |
}; |
85 |
dojox.gfx.decompose=function(_13){ |
86 |
var M=m.normalize(_13),_14={dx:M.dx,dy:M.dy,sx:1,sy:1,angle1:0,angle2:0}; |
87 |
if(eq(M.xy,0)&&eq(M.yx,0)){ |
88 |
return dojo.mixin(_14,{sx:M.xx,sy:M.yy}); |
89 |
} |
90 |
if(eq(M.xx*M.yx,-M.xy*M.yy)){
|
91 |
return _c(M,_14);
|
92 |
} |
93 |
if(eq(M.xx*M.xy,-M.yx*M.yy)){
|
94 |
return _10(M,_14);
|
95 |
} |
96 |
var MT=_2(M),u=_6([M,MT]),v=_6([MT,M]),U=new m.Matrix2D({xx:u.vector1.x,xy:u.vector2.x,yx:u.vector1.y,yy:u.vector2.y}),VT=new m.Matrix2D({xx:v.vector1.x,xy:v.vector1.y,yx:v.vector2.x,yy:v.vector2.y}),S=new m.Matrix2D([m.invert(U),M,m.invert(VT)]); |
97 |
_c(VT,_14); |
98 |
S.xx*=_14.sx; |
99 |
S.yy*=_14.sy; |
100 |
_10(U,_14); |
101 |
S.xx*=_14.sx; |
102 |
S.yy*=_14.sy; |
103 |
return dojo.mixin(_14,{sx:S.xx,sy:S.yy}); |
104 |
}; |
105 |
})(); |
106 |
} |