-
Notifications
You must be signed in to change notification settings - Fork 14
/
IKRS.CubicBezierCurve.js~
154 lines (103 loc) · 4.21 KB
/
IKRS.CubicBezierCurve.js~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* @author Ikaros Kappler
* @date 2013-08-15
* @version 1.0.0
**/
IKRS.CubicBezierCurve = function ( p_startPoint, // THREE.Vector2
p_endPoint, // THREE.Vector2
p_startControlPoint, // THREE.Vector2
p_endControlPoint // THREE.Vector2
) {
//window.alert( "[IKRS.CubicBezierCurve] constructor called." );
// Call super constructor
IKRS.Object.call( this );
// p0: start of the curve
// p3: end of the curve
// p1: help-point of p0
// p2: help-point of p3
this.startPoint = p_startPoint;
this.startControlPoint = p_startControlPoint;
this.endPoint = p_endPoint;
this.endControlPoint = p_endControlPoint;
this.curveIntervals = 30;
this.segmentCache = [];
this.segmentLengths = [];
this.arcLength = null;
this.updateArcLengths();
};
IKRS.CubicBezierCurve.prototype = new IKRS.Object();
IKRS.CubicBezierCurve.prototype.constructor = IKRS.CubicBezierCurve;
IKRS.CubicBezierCurve.prototype.updateArcLengths = function() {
var
//x1 = this.startPoint.x,
//y1 = this.startPoint.y,
//x2, y2,
pointA = new THREE.Vector2( this.startPoint.x,
this.startPoint.y
),
pointB = new THREE.Vector2( 0, 0 ),
curveStep = 1.0/this.curveIntervals;
var u = curveStep;
// Clear segment cache
this.segmentCache = [];
// Push start point into buffer
this.segmentCache.push( this.startPoint );
this.segmentLengths = [];
this.arcLength = 0.0;
//var point;
for( var i = 0; i < this.curveIntervals; i++) {
pointB = this.getPointAt( (i+1) * curveStep ); // parameter is 'u' (not 't')
// Store point into cache
this.segmentCache.push( pointB ); // new THREE.Vector2(x2,y2) );
// Calculate segment length
//var tmpLength = Math.sqrt( Math.pow(x1-x2,2) + Math.pow(y1-y2,2) );
var tmpLength = Math.sqrt( Math.pow(pointA.x-pointB.x,2) + Math.pow(pointA.y-pointB.y,2) );
this.segmentLengths.push( tmpLength );
this.arcLength += tmpLength;
//x1 = point.x; // x2;
//y1 = point.y; // y2;
pointA = pointB;
u += curveStep;
} // END for
// Check if there are enough segments so the max segment length is not bigger than 20px.
// Each time the curce gets too long add more segments
/* There's something going wrong
if( this.arcLength/this.curveIntervals > 20 ) {
//window.alert( "rescaling ..." );
this.curveIntervals = this.arcLength / 20;
// recalculate (will only happen once)
this.updateArcLengths();
} else if( this.arcLength/this.curveIntervals < 10 ) {
// But there should not be too many segments if the curve gets shorter
this.curveIntervals = this.arcLength / 9;
// recalculate (will only happen once)
this.updateArcLengths();
}
*/
//window.alert( "segmentCache=" + this.segmentCache + ", segmentLengths=" + this.segmentLengths + ", arcLength=" + this.arcLength );
}; // END function
IKRS.CubicBezierCurve.prototype.getStartPoint = function() {
return this.startPoint;
};
IKRS.CubicBezierCurve.prototype.getEndPoint = function() {
return this.endPoint;
};
IKRS.CubicBezierCurve.prototype.getStartControlPoint = function() {
return this.startControlPoint;
};
IKRS.CubicBezierCurve.prototype.getEndControlPoint = function() {
return this.endControlPoint;
};
IKRS.CubicBezierCurve.prototype.getPointAt = function( u ) {
// Perform some powerful math magic
var x = this.startPoint.x * Math.pow(1.0-u,3) + this.startControlPoint.x*3*u*Math.pow(1.0-u,2)
+ this.endControlPoint.x*3*Math.pow(u,2)*(1.0-u)+this.endPoint.x*Math.pow(u,3);
var y = this.startPoint.y*Math.pow(1.0-u,3)+this.startControlPoint.y*3*u*Math.pow(1.0-u,2)
+ this.endControlPoint.y*3*Math.pow(u,2)*(1.0-u)+this.endPoint.y*Math.pow(u,3);
return new THREE.Vector2( x, y );
};
IKRS.CubicBezierCurve.prototype.getPoint = function( t ) {
return this.getPointAt( t * this.arcLength );
};
//window.alert( "IKRS.CubicBezierCurve=" +IKRS.CubicBezierCurve );
//window.alert( "IKRS.CubicBezierCurve.prototype=" + IKRS.CubicBezierCurve.prototype );