along function

void along(
  1. List<Point2> pts,
  2. double len,
  3. void fn(
    1. double x0,
    2. double y0,
    3. double x1,
    4. double y1,
    5. double d,
    )
)

Part of a polyline: the first len px of it.

Implementation

void along(
  List<Point2> pts,
  double len,
  void Function(double x0, double y0, double x1, double y1, double d) fn,
) {
  var d = 0.0;
  for (var i = 1; i < pts.length && d < len; i++) {
    final (x0, y0) = pts[i - 1];
    final (x1, y1) = pts[i];
    final l = hypot(x1 - x0, y1 - y0);
    final f = math.min(1.0, (len - d) / l);
    fn(x0, y0, x0 + (x1 - x0) * f, y0 + (y1 - y0) * f, d);
    d += l;
  }
}