getPathLengthsUpToIntersection function

List<double> getPathLengthsUpToIntersection(
  1. Path path,
  2. Offset intersection
)

Calculates the lengths of the path up to the given intersection point.

The path parameter represents the path to calculate the lengths from. The intersection parameter is the intersection point.

Returns a list of lengths representing the distances along the path up to the intersection point.

Implementation

List<double> getPathLengthsUpToIntersection(Path path, Offset intersection) {
  PathMetric pathMetric = path.computeMetrics().first;
  double totalLength = pathMetric.length;
  List<double> lengths = [];

  for (double d = 0.0; d <= totalLength; d += 1) {
    Tangent? tangent = pathMetric.getTangentForOffset(d);
    if (tangent == null) continue;
    if ((tangent.position - intersection).distance < 1) {
      lengths.add(d);
    }
  }

  return lengths;
}