Skip to content

Instantly share code, notes, and snippets.

@CodyJasonBennett
Last active September 2, 2021 02:46
Show Gist options
  • Save CodyJasonBennett/240998bdcfda4b45fce9eb9504885efb to your computer and use it in GitHub Desktop.
Save CodyJasonBennett/240998bdcfda4b45fce9eb9504885efb to your computer and use it in GitHub Desktop.
2D de Casteljau algorithm
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