Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

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