root / trunk / web / dojo / dojox / math / curves.js @ 9
History | View | Annotate | Download (3.85 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.math.curves"]){ |
9 |
dojo._hasResource["dojox.math.curves"]=true; |
10 |
dojo.provide("dojox.math.curves");
|
11 |
dojo.mixin(dojox.math.curves,{Line:function(_1,_2){ |
12 |
this.start=_1;
|
13 |
this.end=_2;
|
14 |
this.dimensions=_1.length;
|
15 |
for(var i=0;i<_1.length;i++){ |
16 |
_1[i]=Number(_1[i]); |
17 |
} |
18 |
for(var i=0;i<_2.length;i++){ |
19 |
_2[i]=Number(_2[i]); |
20 |
} |
21 |
this.getValue=function(n){ |
22 |
var _3=new Array(this.dimensions); |
23 |
for(var i=0;i<this.dimensions;i++){ |
24 |
_3[i]=((this.end[i]-this.start[i])*n)+this.start[i]; |
25 |
} |
26 |
return _3;
|
27 |
}; |
28 |
return this; |
29 |
},Bezier:function(_4){ |
30 |
this.getValue=function(_5){ |
31 |
if(_5>=1){ |
32 |
return this.p[this.p.length-1]; |
33 |
} |
34 |
if(_5<=0){ |
35 |
return this.p[0]; |
36 |
} |
37 |
var _6=new Array(this.p[0].length); |
38 |
for(var k=0;j<this.p[0].length;k++){ |
39 |
_6[k]=0;
|
40 |
} |
41 |
for(var j=0;j<this.p[0].length;j++){ |
42 |
var C=0; |
43 |
var D=0; |
44 |
for(var i=0;i<this.p.length;i++){ |
45 |
C+=this.p[i][j]*this.p[this.p.length-1][0]*dojox.math.bernstein(_5,this.p.length,i); |
46 |
} |
47 |
for(var l=0;l<this.p.length;l++){ |
48 |
D+=this.p[this.p.length-1][0]*dojox.math.bernstein(_5,this.p.length,l); |
49 |
} |
50 |
_6[j]=C/D; |
51 |
} |
52 |
return _6;
|
53 |
}; |
54 |
this.p=_4;
|
55 |
return this; |
56 |
},CatmullRom:function(_7,c){ |
57 |
this.getValue=function(_8){ |
58 |
var _9=_8*(this.p.length-1); |
59 |
var _a=Math.floor(_9);
|
60 |
var _b=_9-_a;
|
61 |
var i0=_a-1; |
62 |
if(i0<0){ |
63 |
i0=0;
|
64 |
} |
65 |
var i=_a;
|
66 |
var i1=_a+1; |
67 |
if(i1>=this.p.length){ |
68 |
i1=this.p.length-1; |
69 |
} |
70 |
var i2=_a+2; |
71 |
if(i2>=this.p.length){ |
72 |
i2=this.p.length-1; |
73 |
} |
74 |
var u=_b;
|
75 |
var u2=_b*_b;
|
76 |
var u3=_b*_b*_b;
|
77 |
var _c=new Array(this.p[0].length); |
78 |
for(var k=0;k<this.p[0].length;k++){ |
79 |
var x1=(-this.c*this.p[i0][k])+((2-this.c)*this.p[i][k])+((this.c-2)*this.p[i1][k])+(this.c*this.p[i2][k]); |
80 |
var x2=(2*this.c*this.p[i0][k])+((this.c-3)*this.p[i][k])+((3-2*this.c)*this.p[i1][k])+(-this.c*this.p[i2][k]); |
81 |
var x3=(-this.c*this.p[i0][k])+(this.c*this.p[i1][k]); |
82 |
var x4=this.p[i][k]; |
83 |
_c[k]=x1*u3+x2*u2+x3*u+x4; |
84 |
} |
85 |
return _c;
|
86 |
}; |
87 |
if(!c){
|
88 |
this.c=0.7; |
89 |
}else{
|
90 |
this.c=c;
|
91 |
} |
92 |
this.p=_7;
|
93 |
return this; |
94 |
},Arc:function(_d,_e,_f){ |
95 |
function _10(a,b){ |
96 |
var c=new Array(a.length); |
97 |
for(var i=0;i<a.length;i++){ |
98 |
c[i]=a[i]+b[i]; |
99 |
} |
100 |
return c;
|
101 |
}; |
102 |
function _11(a){ |
103 |
var b=new Array(a.length); |
104 |
for(var i=0;i<a.length;i++){ |
105 |
b[i]=-a[i]; |
106 |
} |
107 |
return b;
|
108 |
}; |
109 |
var _12=dojox.math.midpoint(_d,_e);
|
110 |
var _13=_10(_11(_12),_d);
|
111 |
var rad=Math.sqrt(Math.pow(_13[0],2)+Math.pow(_13[1],2)); |
112 |
var _14=dojox.math.radiansToDegrees(Math.atan(_13[1]/_13[0])); |
113 |
if(_13[0]<0){ |
114 |
_14-=90;
|
115 |
}else{
|
116 |
_14+=90;
|
117 |
} |
118 |
dojox.math.curves.CenteredArc.call(this,_12,rad,_14,_14+(_f?-180:180)); |
119 |
},CenteredArc:function(_15,_16,_17,end){ |
120 |
this.center=_15;
|
121 |
this.radius=_16;
|
122 |
this.start=_17||0; |
123 |
this.end=end;
|
124 |
this.getValue=function(n){ |
125 |
var _18=new Array(2); |
126 |
var _19=dojox.math.degreesToRadians(this.start+((this.end-this.start)*n)); |
127 |
_18[0]=this.center[0]+this.radius*Math.sin(_19); |
128 |
_18[1]=this.center[1]-this.radius*Math.cos(_19); |
129 |
return _18;
|
130 |
}; |
131 |
return this; |
132 |
},Circle:function(_1a,_1b){ |
133 |
dojox.math.curves.CenteredArc.call(this,_1a,_1b,0,360); |
134 |
return this; |
135 |
},Path:function(){ |
136 |
var _1c=[];
|
137 |
var _1d=[];
|
138 |
var _1e=[];
|
139 |
var _1f=0; |
140 |
this.add=function(_20,_21){ |
141 |
if(_21<0){ |
142 |
console.error("dojox.math.curves.Path.add: weight cannot be less than 0");
|
143 |
} |
144 |
_1c.push(_20); |
145 |
_1d.push(_21); |
146 |
_1f+=_21; |
147 |
_22(); |
148 |
}; |
149 |
this.remove=function(_23){ |
150 |
for(var i=0;i<_1c.length;i++){ |
151 |
if(_1c[i]==_23){
|
152 |
_1c.splice(i,1);
|
153 |
_1f-=_1d.splice(i,1)[0]; |
154 |
break;
|
155 |
} |
156 |
} |
157 |
_22(); |
158 |
}; |
159 |
this.removeAll=function(){ |
160 |
_1c=[]; |
161 |
_1d=[]; |
162 |
_1f=0;
|
163 |
}; |
164 |
this.getValue=function(n){ |
165 |
var _24=false,_25=0; |
166 |
for(var i=0;i<_1e.length;i++){ |
167 |
var r=_1e[i];
|
168 |
if(n>=r[0]&&n<r[1]){ |
169 |
var _26=(n-r[0])/r[2]; |
170 |
_25=_1c[i].getValue(_26); |
171 |
_24=true;
|
172 |
break;
|
173 |
} |
174 |
} |
175 |
if(!_24){
|
176 |
_25=_1c[_1c.length-1].getValue(1); |
177 |
} |
178 |
for(var j=0;j<i;j++){ |
179 |
_25=dojox.math.points.translate(_25,_1c[j].getValue(1));
|
180 |
} |
181 |
return _25;
|
182 |
}; |
183 |
function _22(){ |
184 |
var _27=0; |
185 |
for(var i=0;i<_1d.length;i++){ |
186 |
var end=_27+_1d[i]/_1f;
|
187 |
var len=end-_27;
|
188 |
_1e[i]=[_27,end,len]; |
189 |
_27=end; |
190 |
} |
191 |
}; |
192 |
return this; |
193 |
}}); |
194 |
} |