Skip to content

Instantly share code, notes, and snippets.

@melbourne2991
Last active September 5, 2015 08:02
Show Gist options
  • Save melbourne2991/37930c149f2cf67a4af4 to your computer and use it in GitHub Desktop.
Save melbourne2991/37930c149f2cf67a4af4 to your computer and use it in GitHub Desktop.
// takes [x, y] array objects for the points
function bezier(t, p0, p1, p2) {
const ay = Math.pow(1 - t, 2) * p0[1];
const by = 2 * (1 - t) * t * p1[1];
const cy = Math.pow(t, 2) * p2[1];
const ax = Math.pow(1 - t, 2) * p0[0];
const bx = 2 * (1 - t) * t * p1[0];
const cx = Math.pow(t, 2) * p2[0];
const y = ay + by + cy;
const x = ax + bx + cx;
return [x, y];
};
function bezierCurve(p0, p1, p2, intervalSize) {
const curve = [];
for(let i = 0; i < 1; i = i + intervalSize) {
curve.push(bezier(i, p0, p1, p2));
}
return curve;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment