Skip to content

Instantly share code, notes, and snippets.

@osaxma
Last active April 6, 2021 13:48
Show Gist options
  • Save osaxma/d65ed10046cf86d16cd27366c5d6639e to your computer and use it in GitHub Desktop.
Save osaxma/d65ed10046cf86d16cd27366c5d6639e to your computer and use it in GitHub Desktop.
Find the coordinates of a point along a quadratic Bezier curve in dart.
// credit: https://stackoverflow.com/a/9195706/10976714
// The formulas to calculate the coordinates of a point at any given position (from 0 to 1) on the Bezier curve are:
// x(t) = (1-t)^2 * x1 + 2 * (1-t) * t * x2 + t^2 * x3
// y(t) = (1-t)^2 * y1 + 2 * (1-t) * t * y2 + t^2 * y3
// where (x1, y1) is the starting point, (x2, y2) is the control point and (x3, y3) is the end point.
// get a point along the Bezier curve line
// If you pass the start, end & control points, along with 0.5 for the halfway position, you get the offset to that point
Offset getQuadraticCurvePoint(Offset start, Offset control, Offset end, double position) {
return Offset(
_getQBezierValue(position, start.dx, control.dx, end.dx),
_getQBezierValue(position, start.dy, control.dy, end.dy),
);
}
double _getQBezierValue(double t, double p1, double p2, double p3) {
final iT = 1 - t;
return iT * iT * p1 + 2 * iT * t * p2 + t * t * p3;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment