-
Notifications
You must be signed in to change notification settings - Fork 0
/
DRCurve.as
159 lines (123 loc) · 4.59 KB
/
DRCurve.as
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
155
156
157
158
159
class DRCurve {
static var EDGE = 0;
static var CENTER = 1;
static var STEPS = 5;
static var nextId = 0;
var id:Number;
var controlPoints:Array;
var clip:MovieClip;
var isLoop:Boolean;
var style:Number;
var hidden:Boolean;
function DRCurve() {
this.id = nextId++;
controlPoints = new Array();
isLoop = false;
}
function display(parent:MovieClip, style:Number) {
clip = parent.createEmptyMovieClip("Curve_" + id, id);
this.style = style;
draw();
}
function loop() {
isLoop = true;
draw();
}
function addPoint(point:DRControlPoint) {
controlPoints.push( point );
if (!isLoop && controlPoints.length >= 2) draw();
if ( isLoop && controlPoints.length >= 3) draw();
}
function draw() {
clip.clear();
if (style == EDGE)
clip.lineStyle(2.0, 0x000000, 100);
else
clip.lineStyle(1.0, 0x666666, 100);
var startOfCurve = controlPoints[0]
clip.moveTo( startOfCurve.x, startOfCurve.y );
var segments = isLoop ? controlPoints.length : (controlPoints.length - 1);
for (var i = 0; i < segments; i++) {
var startOfSegment = controlPoints[i];
var endOfSegment = (i == controlPoints.length - 1) ? controlPoints[0] : controlPoints[i+1];
renderSegment( leftControlPoint(i), startOfSegment, endOfSegment, rightControlPoint(i) );
}
}
function leftControlPoint(segment:Number) {
if (segment == 0) {
return isLoop ? controlPoints[ controlPoints.length - 1 ] : controlPoints[0];
} else {
return controlPoints[ segment - 1 ];
}
}
function rightControlPoint(segment:Number) {
if (segment == controlPoints.length - 2) {
return isLoop ? controlPoints[0] : controlPoints[ controlPoints.length - 1 ];
} else if (segment == controlPoints.length - 1) {
// isLoop only
return controlPoints[1];
} else {
return controlPoints[ segment + 2 ];
}
}
function replaceControlPoint(point:DRControlPoint, withPoint:DRControlPoint) {
var newControlPoints = [];
var justAddedWith = false;
for (var i = 0; i < controlPoints.length; i++) {
if (controlPoints[i] == point || controlPoints[i] == withPoint) {
if (! justAddedWith) newControlPoints.push(withPoint);
justAddedWith = true;
} else {
newControlPoints.push(controlPoints[i]);
justAddedWith = false;
}
}
controlPoints = newControlPoints;
draw();
}
function doubleControlPoint(point:DRControlPoint) {
for (var i = 0; i < controlPoints.length; i++) {
if (point == controlPoints[i]) {
controlPoints.splice(i, 0, point);
return;
}
}
}
function renderSegment(p1, p2, p3, p4) {
for (var i = 0; i <= STEPS; i++) {
var t = i * 1.0 / STEPS;
renderSegmentFragmentCatmullRom(t, p1, p2, p3, p4);
}
}
function renderSegmentFragmentLinear(t, p1, p2, p3, p4) {
var x = (1 - t) * p2.x + t * p3.x;
var y = (1 - t) * p2.y + t * p3.y;
clip.lineTo(x, y);
}
function catmullRom(t, p1, p2, p3, p4) {
var p = ( -p1 + 3*p2 - 3*p3 + p4) * t * t * t +
(2*p1 - 5*p2 + 4*p3 - p4) * t * t +
( -p1 + p3 ) * t +
( 2*p2 );
return 0.5 * p;
}
function renderSegmentFragmentCatmullRom(t, p1, p2, p3, p4) {
var x = catmullRom(t, p1.x, p2.x, p3.x, p4.x);
var y = catmullRom(t, p1.y, p2.y, p3.y, p4.y);
clip.lineTo(x, y);
}
function curveLength() {
return controlPoints.length;
}
function parameterize(t:Number) {
var segment = Math.floor(t);
var frac = t - segment;
var lcp = leftControlPoint(segment);
var startOfSegment = controlPoints[segment];
var endOfSegment = (segment == controlPoints.length - 1) ? controlPoints[0] : controlPoints[segment+1];
var rcp = rightControlPoint(segment)
var x = catmullRom(frac, lcp.x, startOfSegment.x, endOfSegment.x, rcp.x);
var y = catmullRom(frac, lcp.y, startOfSegment.y, endOfSegment.y, rcp.y);
return { x: x, y: y };
}
}