getDrawPercentage function

double getDrawPercentage(
  1. Path path,
  2. Offset intersection, {
  3. bool first = true,
})

Calculates the draw percentage of the path up to the given intersection point.

The path parameter represents the path to calculate the draw percentage from. The intersection parameter is the intersection point. The first parameter specifies whether to calculate the draw percentage up to the first or last intersection.

Returns the draw percentage as a double value between 0 and 100.

Implementation

double getDrawPercentage(Path path, Offset intersection, {bool first = true}) {
  List<double> lengthsUpToIntersections =
      getPathLengthsUpToIntersection(path, intersection);
  if (lengthsUpToIntersections.isEmpty) return 100.0; // No intersection

  PathMetric pathMetric = path.computeMetrics().first;
  double totalLength = pathMetric.length;

  double nearestIntersectionLength = first
      ? lengthsUpToIntersections.reduce(min)
      : lengthsUpToIntersections.reduce(max);
  return (nearestIntersectionLength / totalLength) * 100;
}