public class ClosestPoints { public static double distanceSquare(double[] point1, double[] point2) { return (point1[0] - point2[0]) * (point1[0] - point2[0]) + (point1[1] - point2[1]) * (point1[1] - point2[1]); } public static void main(String[] args) { double[][] points = { { -1, 3 }, { -1, -1 }, { 1, 1 }, { 2, 0.5 }, { 2, -1 }, { 3, 3 }, { 4, 2 }, { 4, -0.5 } }; double minDistance = distanceSquare(points[0], points[1]); int p1 = 0, p2 = 1; for (int i = 0; i < points.length; ++i) for (int j = i + 1; j < points.length; ++j) { double distance = distanceSquare(points[i], points[j]); if (distance < minDistance) { p1 = i; p2 = j; minDistance = distance; } } System.out.printf("The closest points are Point %d and Point %d with distance %f\n", p1, p2, Math.sqrt(minDistance)); } }