public static double ShortestSquaredDistanceToOtherPolygon(this Polygon2D polygon, Polygon2D otherPolygon, out LineSegment2D shortestDistanceSegment) { var shortestSquaredDistance = double.MaxValue; shortestDistanceSegment = new LineSegment2D(); LineSegment2D candidateSegment; double candidateSquaredDistance; foreach (var lineSegment0 in polygon.GetLineSegments()) { foreach (var lineSegment1 in otherPolygon.GetLineSegments()) { candidateSquaredDistance = lineSegment0.ShortestSquaredDistanceToOtherLineSegment(lineSegment1, out candidateSegment); if (candidateSquaredDistance < shortestSquaredDistance) { shortestSquaredDistance = candidateSquaredDistance; shortestDistanceSegment = candidateSegment; } } } return(shortestSquaredDistance); }
public static double ShortestSquaredDistanceToPoint(this Polygon2D polygon, Point2D point, out Point2D pointOnPolygon) { var minSquaredDistance = double.MaxValue; pointOnPolygon = new Point2D(); foreach (var lineSegment in polygon.GetLineSegments()) { Point2D candidateForPointOnPolygon; var squaredDis = lineSegment.ShortestSquaredDistanceToPoint(point, out candidateForPointOnPolygon); if (squaredDis < minSquaredDistance) { minSquaredDistance = squaredDis; pointOnPolygon = candidateForPointOnPolygon; } } return(minSquaredDistance); }