Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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