| 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.math.curves");
|
|
|
14 |
dojo.require("dojo.math");
|
|
|
15 |
dojo.math.curves = {Line:function (start, end) {
|
|
|
16 |
this.start = start;
|
|
|
17 |
this.end = end;
|
|
|
18 |
this.dimensions = start.length;
|
|
|
19 |
for (var i = 0; i < start.length; i++) {
|
|
|
20 |
start[i] = Number(start[i]);
|
|
|
21 |
}
|
|
|
22 |
for (var i = 0; i < end.length; i++) {
|
|
|
23 |
end[i] = Number(end[i]);
|
|
|
24 |
}
|
|
|
25 |
this.getValue = function (n) {
|
|
|
26 |
var retVal = new Array(this.dimensions);
|
|
|
27 |
for (var i = 0; i < this.dimensions; i++) {
|
|
|
28 |
retVal[i] = ((this.end[i] - this.start[i]) * n) + this.start[i];
|
|
|
29 |
}
|
|
|
30 |
return retVal;
|
|
|
31 |
};
|
|
|
32 |
return this;
|
|
|
33 |
}, Bezier:function (pnts) {
|
|
|
34 |
this.getValue = function (step) {
|
|
|
35 |
if (step >= 1) {
|
|
|
36 |
return this.p[this.p.length - 1];
|
|
|
37 |
}
|
|
|
38 |
if (step <= 0) {
|
|
|
39 |
return this.p[0];
|
|
|
40 |
}
|
|
|
41 |
var retVal = new Array(this.p[0].length);
|
|
|
42 |
for (var k = 0; j < this.p[0].length; k++) {
|
|
|
43 |
retVal[k] = 0;
|
|
|
44 |
}
|
|
|
45 |
for (var j = 0; j < this.p[0].length; j++) {
|
|
|
46 |
var C = 0;
|
|
|
47 |
var D = 0;
|
|
|
48 |
for (var i = 0; i < this.p.length; i++) {
|
|
|
49 |
C += this.p[i][j] * this.p[this.p.length - 1][0] * dojo.math.bernstein(step, this.p.length, i);
|
|
|
50 |
}
|
|
|
51 |
for (var l = 0; l < this.p.length; l++) {
|
|
|
52 |
D += this.p[this.p.length - 1][0] * dojo.math.bernstein(step, this.p.length, l);
|
|
|
53 |
}
|
|
|
54 |
retVal[j] = C / D;
|
|
|
55 |
}
|
|
|
56 |
return retVal;
|
|
|
57 |
};
|
|
|
58 |
this.p = pnts;
|
|
|
59 |
return this;
|
|
|
60 |
}, CatmullRom:function (pnts, c) {
|
|
|
61 |
this.getValue = function (step) {
|
|
|
62 |
var percent = step * (this.p.length - 1);
|
|
|
63 |
var node = Math.floor(percent);
|
|
|
64 |
var progress = percent - node;
|
|
|
65 |
var i0 = node - 1;
|
|
|
66 |
if (i0 < 0) {
|
|
|
67 |
i0 = 0;
|
|
|
68 |
}
|
|
|
69 |
var i = node;
|
|
|
70 |
var i1 = node + 1;
|
|
|
71 |
if (i1 >= this.p.length) {
|
|
|
72 |
i1 = this.p.length - 1;
|
|
|
73 |
}
|
|
|
74 |
var i2 = node + 2;
|
|
|
75 |
if (i2 >= this.p.length) {
|
|
|
76 |
i2 = this.p.length - 1;
|
|
|
77 |
}
|
|
|
78 |
var u = progress;
|
|
|
79 |
var u2 = progress * progress;
|
|
|
80 |
var u3 = progress * progress * progress;
|
|
|
81 |
var retVal = new Array(this.p[0].length);
|
|
|
82 |
for (var k = 0; k < this.p[0].length; k++) {
|
|
|
83 |
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]);
|
|
|
84 |
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]);
|
|
|
85 |
var x3 = (-this.c * this.p[i0][k]) + (this.c * this.p[i1][k]);
|
|
|
86 |
var x4 = this.p[i][k];
|
|
|
87 |
retVal[k] = x1 * u3 + x2 * u2 + x3 * u + x4;
|
|
|
88 |
}
|
|
|
89 |
return retVal;
|
|
|
90 |
};
|
|
|
91 |
if (!c) {
|
|
|
92 |
this.c = 0.7;
|
|
|
93 |
} else {
|
|
|
94 |
this.c = c;
|
|
|
95 |
}
|
|
|
96 |
this.p = pnts;
|
|
|
97 |
return this;
|
|
|
98 |
}, Arc:function (start, end, ccw) {
|
|
|
99 |
var center = dojo.math.points.midpoint(start, end);
|
|
|
100 |
var sides = dojo.math.points.translate(dojo.math.points.invert(center), start);
|
|
|
101 |
var rad = Math.sqrt(Math.pow(sides[0], 2) + Math.pow(sides[1], 2));
|
|
|
102 |
var theta = dojo.math.radToDeg(Math.atan(sides[1] / sides[0]));
|
|
|
103 |
if (sides[0] < 0) {
|
|
|
104 |
theta -= 90;
|
|
|
105 |
} else {
|
|
|
106 |
theta += 90;
|
|
|
107 |
}
|
|
|
108 |
dojo.math.curves.CenteredArc.call(this, center, rad, theta, theta + (ccw ? -180 : 180));
|
|
|
109 |
}, CenteredArc:function (center, radius, start, end) {
|
|
|
110 |
this.center = center;
|
|
|
111 |
this.radius = radius;
|
|
|
112 |
this.start = start || 0;
|
|
|
113 |
this.end = end;
|
|
|
114 |
this.getValue = function (n) {
|
|
|
115 |
var retVal = new Array(2);
|
|
|
116 |
var theta = dojo.math.degToRad(this.start + ((this.end - this.start) * n));
|
|
|
117 |
retVal[0] = this.center[0] + this.radius * Math.sin(theta);
|
|
|
118 |
retVal[1] = this.center[1] - this.radius * Math.cos(theta);
|
|
|
119 |
return retVal;
|
|
|
120 |
};
|
|
|
121 |
return this;
|
|
|
122 |
}, Circle:function (center, radius) {
|
|
|
123 |
dojo.math.curves.CenteredArc.call(this, center, radius, 0, 360);
|
|
|
124 |
return this;
|
|
|
125 |
}, Path:function () {
|
|
|
126 |
var curves = [];
|
|
|
127 |
var weights = [];
|
|
|
128 |
var ranges = [];
|
|
|
129 |
var totalWeight = 0;
|
|
|
130 |
this.add = function (curve, weight) {
|
|
|
131 |
if (weight < 0) {
|
|
|
132 |
dojo.raise("dojo.math.curves.Path.add: weight cannot be less than 0");
|
|
|
133 |
}
|
|
|
134 |
curves.push(curve);
|
|
|
135 |
weights.push(weight);
|
|
|
136 |
totalWeight += weight;
|
|
|
137 |
computeRanges();
|
|
|
138 |
};
|
|
|
139 |
this.remove = function (curve) {
|
|
|
140 |
for (var i = 0; i < curves.length; i++) {
|
|
|
141 |
if (curves[i] == curve) {
|
|
|
142 |
curves.splice(i, 1);
|
|
|
143 |
totalWeight -= weights.splice(i, 1)[0];
|
|
|
144 |
break;
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
computeRanges();
|
|
|
148 |
};
|
|
|
149 |
this.removeAll = function () {
|
|
|
150 |
curves = [];
|
|
|
151 |
weights = [];
|
|
|
152 |
totalWeight = 0;
|
|
|
153 |
};
|
|
|
154 |
this.getValue = function (n) {
|
|
|
155 |
var found = false, value = 0;
|
|
|
156 |
for (var i = 0; i < ranges.length; i++) {
|
|
|
157 |
var r = ranges[i];
|
|
|
158 |
if (n >= r[0] && n < r[1]) {
|
|
|
159 |
var subN = (n - r[0]) / r[2];
|
|
|
160 |
value = curves[i].getValue(subN);
|
|
|
161 |
found = true;
|
|
|
162 |
break;
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
if (!found) {
|
|
|
166 |
value = curves[curves.length - 1].getValue(1);
|
|
|
167 |
}
|
|
|
168 |
for (var j = 0; j < i; j++) {
|
|
|
169 |
value = dojo.math.points.translate(value, curves[j].getValue(1));
|
|
|
170 |
}
|
|
|
171 |
return value;
|
|
|
172 |
};
|
|
|
173 |
function computeRanges() {
|
|
|
174 |
var start = 0;
|
|
|
175 |
for (var i = 0; i < weights.length; i++) {
|
|
|
176 |
var end = start + weights[i] / totalWeight;
|
|
|
177 |
var len = end - start;
|
|
|
178 |
ranges[i] = [start, end, len];
|
|
|
179 |
start = end;
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
return this;
|
|
|
183 |
}};
|
|
|
184 |
|