public static ClosestTwoPointsAndDistance FindClosestTwoPoints(Point[] points) { ClosestTwoPointsAndDistance closestTwoAndDistance = new ClosestTwoPointsAndDistance(); double minimalDistance = double.MaxValue; for (int first = 0; first < points.Length; first++) { for (int second = first + 1; second < points.Length; second++) { Point p1 = points[first]; Point p2 = points[second]; double currentDistance = CalcDistance(p1, p2); if (currentDistance < minimalDistance) { minimalDistance = currentDistance; closestTwoAndDistance = new ClosestTwoPointsAndDistance() { First = p1, Second = p2, Distance = currentDistance }; } } } return(closestTwoAndDistance); }
private static void ClosestTwoPoints() { Point[] points = ClosestTwoPointsAndDistance.ReadArrayOfPoints(); ClosestTwoPointsAndDistance closestTwoPointsAndDistance = ClosestTwoPointsAndDistance.FindClosestTwoPoints(points); Console.WriteLine($"{closestTwoPointsAndDistance.Distance:F3}"); Console.WriteLine(closestTwoPointsAndDistance.First); Console.WriteLine(closestTwoPointsAndDistance.Second); }