Last active
September 2, 2021 02:46
-
-
Save CodyJasonBennett/240998bdcfda4b45fce9eb9504885efb to your computer and use it in GitHub Desktop.
2D de Casteljau algorithm
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
function deCasteljauAlgorithm(points, t) { | |
if (t === 1) return points[points.length - 1]; | |
if (t === 0 || points.length === 1) return points[0]; | |
const calculatedPoints = []; | |
for (let i = 1; i < points.length; i++) { | |
const [p1X, p1Y] = points[i - 1]; | |
const [p2X, p2Y] = points[i]; | |
calculatedPoints.push([ | |
p1X + (p2X - p1X) * t, | |
p1Y + (p2Y - p1Y) * t | |
]); | |
} | |
return deCasteljauAlgorithm(calculatedPoints, t); | |
} | |
// Usage | |
console.log(deCasteljauAlgorithm([[0, 0], [0.5, 0.5], [1, 0]], 0.5)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment