Skip to content

Instantly share code, notes, and snippets.

@rutvik110
Created December 22, 2022 17:01
Show Gist options
  • Save rutvik110/74f5882a7f09782887300e213252bcf5 to your computer and use it in GitHub Desktop.
Save rutvik110/74f5882a7f09782887300e213252bcf5 to your computer and use it in GitHub Desktop.
Implementation of De Casteljau's algorithm in dart
// Implementation of De Casteljau's algorithm in dart
// ref - https://pomax.github.io/bezierinfo/#splitting, https://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm,
List<List<math.Point<num>>> drawCurvePoint(List<math.Point> points, double t) {
final List<math.Point<num>> left = <math.Point>[];
final List<math.Point<num>> right = <math.Point>[];
if (points.length == 1) {
left.add(points[0]);
right.add(points[0]);
return [left, right];
} else {
final List<math.Point<num>> newpoints =
List<math.Point>.filled(points.length - 1, const math.Point(0, 0));
for (int i = 0; i < newpoints.length; i++) {
if (i == 0) {
left.add(points[i]);
}
if (i == newpoints.length - 1) {
right.add(points[i + 1]);
}
newpoints[i] = math.Point(points[i].x, points[i].y) * (1 - t) +
math.Point(points[i + 1].x, points[i + 1].y) * t;
}
final List<List<math.Point<num>>> newPoints = drawCurvePoint(newpoints, t);
left.addAll(newPoints[0]);
right.addAll(newPoints[1]);
return [left, right];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment