distanceToPolyline method

double distanceToPolyline(
  1. List<Point> polyline
)

Calculates the shortest distance from this point to a polyline, which is a list of points forming a broken line.

Example:

var point = Point(1, 1);
var polyline = [Point(0, 0), Point(0, 2), Point(2, 2), Point(2, 0)];
print(point.distanceToPolyline(polyline)); // Output: 1.0

Returns the distance as a double.

Implementation

double distanceToPolyline(List<Point> polyline) {
  var minDistance = double.infinity;
  for (var i = 0; i < polyline.length - 1; i++) {
    var distance = distanceToLine(polyline[i], polyline[i + 1]);
    minDistance = min(minDistance, distance);
  }
  return minDistance;
}