-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathcurves.js
228 lines (206 loc) · 6.52 KB
/
curves.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// @ts-check
kaplay();
function addPoint(c, ...args) {
return add([
"point",
rect(8, 8),
anchor("center"),
pos(...args),
area(),
color(c),
]);
}
function addBezier(...objects) {
const points = [...objects];
let t = 0;
return add([
pos(0, 0),
{
draw() {
const coords = points.map(p => p.pos);
const c = normalizedCurve(t => evaluateBezier(...coords, t));
drawCurve(t => evaluateBezier(...coords, t), {
segments: 25,
width: 4,
});
drawLine({
p1: points[0].pos,
p2: points[1].pos,
width: 2,
color: rgb(0, 0, 255),
});
drawLine({
p1: points[3].pos,
p2: points[2].pos,
width: 2,
color: rgb(0, 0, 255),
});
for (let i = 0; i <= 10; i++) {
const p = evaluateBezier(...coords, i / 10);
drawCircle({
pos: p,
radius: 4,
color: YELLOW,
});
}
for (let i = 0; i <= 10; i++) {
const p = c(i / 10);
drawCircle({
pos: p,
radius: 8,
color: RED,
opacity: 0.5,
});
}
},
update() {
},
},
]);
}
function drawCatmullRom(a, b, c, d) {
drawCurve(t => evaluateCatmullRom(a, b, c, d, t), {
segments: 25,
width: 4,
});
}
function normalizedFirstDerivative(curve, curveFirstDerivative) {
const curveLength = curveLengthApproximation(curve);
const length = curveLength(1);
return s => {
const l = s * length;
const t = curveLength(l, true);
return curveFirstDerivative(t);
};
}
function addCatmullRom(...objects) {
const points = [...objects];
let t = 0;
return add([
pos(0, 0),
{
draw() {
const coords = points.map(p => p.pos);
const first = coords[0].add(coords[0].sub(coords[1]));
const last = coords[coords.length - 1].add(
coords[coords.length - 1].sub(coords[coords.length - 2]),
);
let curve;
let ct;
const curveCoords = [
[first, ...coords.slice(0, 3)],
coords,
[...coords.slice(1), last],
];
const curveLengths = curveCoords.map(cc =>
curveLengthApproximation(t => evaluateCatmullRom(...cc, t))(
1,
)
);
const length = curveLengths.reduce((sum, l) => sum + l, 0);
const p0 = curveLengths[0] / length;
const p1 = curveLengths[1] / length;
const p2 = curveLengths[2] / length;
if (t <= p0) {
curve = curveCoords[0];
ct = t * (1 / p0);
}
else if (t <= p0 + p1) {
curve = curveCoords[1];
ct = (t - p0) * (1 / p1);
}
else {
curve = curveCoords[2];
ct = (t - p0 - p1) * (1 / p2);
}
const c = normalizedCurve(t => evaluateCatmullRom(...curve, t));
const cd = normalizedFirstDerivative(
t => evaluateCatmullRom(...curve, t),
t => evaluateCatmullRomFirstDerivative(...curve, t),
);
drawCatmullRom(first, ...coords.slice(0, 3), {
segments: 10,
width: 4,
});
drawCatmullRom(...coords, { segments: 10, width: 4 });
drawCatmullRom(...coords.slice(1), last, {
segments: 10,
width: 4,
});
const cartPos1 = evaluateCatmullRom(...curve, ct);
const tangent1 = evaluateCatmullRomFirstDerivative(
...curve,
ct,
);
pushTransform();
pushTranslate(cartPos1);
pushRotate(tangent1.angle(1, 0));
drawRect({
width: 16,
height: 8,
pos: vec2(-8, -4),
color: YELLOW,
outline: { color: BLUE, width: 4 },
});
popTransform();
const cartPos2 = c(ct);
const tangent2 = cd(ct);
pushTransform();
pushTranslate(cartPos2);
pushRotate(tangent2.angle(1, 0));
drawRect({
width: 16,
height: 8,
pos: vec2(-8, -4),
color: RED,
opacity: 0.5,
outline: { color: BLACK, width: 4 },
});
popTransform();
},
update() {
t += dt() / 10;
t = t % 1;
},
},
]);
}
// Interraction
let obj = null;
onClick("point", (point) => {
obj = point;
});
onMouseMove((pos) => {
if (obj) {
obj.moveTo(pos);
}
});
onMouseRelease((pos) => {
obj = null;
});
// Scene creation
const p0 = addPoint(RED, 100, 40);
const p1 = addPoint(BLUE, 80, 120);
const p2 = addPoint(BLUE, 300, 60);
const p3 = addPoint(RED, 250, 200);
addBezier(p0, p1, p2, p3);
add([
pos(20, 300),
text("yellow: default spacing\nred: constant spacing", { size: 20 }),
]);
const c0 = addPoint(RED, 400, 40);
const c1 = addPoint(RED, 380, 120);
const c2 = addPoint(RED, 500, 60);
const c3 = addPoint(RED, 450, 200);
addCatmullRom(c0, c1, c2, c3);
add([
pos(400, 300),
text("yellow: default speed\nred: constant speed", { size: 20 }),
]);
add([
pos(20, 350),
text(
"curves are non-linear in t. This means that for a given t,\nthe distance traveled from the start doesn't grow at constant speed.\nTo fix this, turn the curve into a normalized curve first.\nUse derivatives to find the direction of the curve at a certain t.",
{ size: 20 },
),
]);