forked from springmeyer/arc.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bezier.js
35 lines (29 loc) · 932 Bytes
/
bezier.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
function bezier(pts) {
function curve(points) {
var c = [];
var steps = 40;
for (var i = 0; i <= steps; i++) {
var t = i / steps;
var pt = [
Math.pow(1 - t, 3) * points[0][0]
+ 3 * t * Math.pow(1 - t, 2) * points[1][0]
+ 3 * (1 - t) * Math.pow(t, 2) * points[2][0]
+ Math.pow(t, 3) * points[3][0],
Math.pow(1 - t, 3) * points[0][1]
+ 3 * t * Math.pow(1-t,2) * points[1][1]
+ 3 * (1-t) * Math.pow(t,2) * points[2][1]
+ Math.pow(t, 3) * points[3][1]
];
c.push(pt);
}
return c;
}
var c = [];
if (pts.length < 4) return pts;
for (var i = 0; i < pts.length; i += 3) {
if (i + 4 <= pts.length) {
c = c.concat(curve(pts.slice(i, i + 4)));
}
}
return c;
}