Last active
September 14, 2021 22:18
-
-
Save piman51277/a6093bdba84244289677e2ab94326ca2 to your computer and use it in GitHub Desktop.
Small class to generate points on a bezier curve
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BezierCurve { | |
static quadratic(x0, y0, x1, y1, x2, y2, steps) { | |
const points = []; | |
for (let step = 0; step <= steps; step++) { | |
const t = step / steps; | |
const [lx0, ly0] = interp(x0, y0, x1, y1, t); | |
const [lx1, ly1] = interp(x1, y1, x2, y2, t); | |
const [qx0, qy0] = interp(lx0, ly0, lx1, ly1, t); | |
points.push([qx0, qy0]); | |
} | |
return points; | |
} | |
static q_quadratic(x0, y0, x1, y1, x2, y2, steps) { | |
const points = []; | |
for (let step = 0; step <= steps; step++) { | |
const t = step / steps; | |
const t_sq = t * t; | |
points.push([x0 * (1 - (2 * t) + t_sq) + x1 * (2 * (t - t_sq)) + x2 * t_sq, y0 * (1 - (2 * t) + t_sq) + y1 * (2 * (t - t_sq)) + y2 * t_sq]); | |
} | |
return points; | |
} | |
static q_cubic(x0, y0, x1, y1, x2, y2, x3, y3, steps) { | |
const points = []; | |
for (let step = 0; step <= steps; step++) { | |
const t = step / steps; | |
const t_sq = t * t; | |
const t_cb = t_sq * t; | |
points.push([x0 * (-t_cb + 3 * t_sq - 3 * t + 1) + x1 * (3 * t_cb - 6 * t_sq + 3 * t) + x2 * (-3 * t_cb + 3 * t_sq) + x3 * t_cb, y0 * (-t_cb + 3 * t_sq - 3 * t + 1) + y1 * (3 * t_cb - 6 * t_sq + 3 * t) + y2 * (-3 * t_cb + 3 * t_sq) + y3 * t_cb]); | |
} | |
return points | |
} | |
static cubic(x0, y0, x1, y1, x2, y2, x3, y3, steps) { | |
const points = []; | |
for (let step = 0; step <= steps; step++) { | |
const t = step / steps; | |
const [lx0, ly0] = interp(x0, y0, x1, y1, t); | |
const [lx1, ly1] = interp(x1, y1, x2, y2, t); | |
const [lx2, ly2] = interp(x2, y2, x3, y3, t); | |
const [qx0, qy0] = interp(lx0, ly0, lx1, ly1, t); | |
const [qx1, qy1] = interp(lx1, ly1, lx2, ly2, t); | |
const [zx0, zy0] = interp(qx0, qy0, qx1, qy1, t); | |
points.push([zx0, zy0]); | |
} | |
return points | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
q_cubic is around 2.7x the speed of cubic
q_quadratic is around 1.7x the speed of quadratic