Created
March 7, 2021 14:35
-
-
Save steveruizok/4603d75bba5a0b1e7ef2e6bd104ab379 to your computer and use it in GitHub Desktop.
Interpolate a point along a cubic bezier curve.
This file contains hidden or 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
export function interpolateCubicBezier( | |
p0: { x: number; y: number }, | |
c0: { x: number; y: number }, | |
c1: { x: number; y: number }, | |
p1: { x: number; y: number } | |
) { | |
// 0 <= t <= 1 | |
return function interpolator(t: number) { | |
return [ | |
pow(1 - t, 3) * p0.x + | |
3 * pow(1 - t, 2) * t * c0.x + | |
3 * (1 - t) * pow(t, 2) * c1.x + | |
pow(t, 3) * p1.x, | |
pow(1 - t, 3) * p0.y + | |
3 * pow(1 - t, 2) * t * c0.y + | |
3 * (1 - t) * pow(t, 2) * c1.y + | |
pow(t, 3) * p1.y, | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment