1 import java.util.Scanner;
2
3 public class FindNearestPoints {
4 public static void main(String[] args) {
5 Scanner input = new Scanner(System.in);
6 System.out.print("Enter the number of points: ");
7 int numberOfPoints = input.nextInt();
8
9
10 double[][] points = new double[numberOfPoints][2];
11 System.out.print("Enter " + numberOfPoints + " points: ");
12 for (int i = 0; i < points.length; i++) {
13 points[i][0] = input.nextDouble();
14 points[i][1] = input.nextDouble();
15 }
16
17
18 int p1 = 0, p2 = 1;
19 double shortestDistance = distance(points[p1][0], points[p1][1],
20 points[p2][0], points[p2][1]);
21
22
23 for (int i = 0; i < points.length; i++) {
24 for (int j = i + 1; j < points.length; j++) {
25 double distance = distance(points[i][0], points[i][1],
26 points[j][0], points[j][1]);
27
28 if (shortestDistance > distance) {
29 p1 = i;
30 p2 = j;
31 shortestDistance = distance;
32 }
33 }
34 }
35
36
37 System.out.println("The closest two points are " +
38 "(" + points[p1][0] + ", " + points[p1][1] + ") and (" +
39 points[p2][0] + ", " + points[p2][1] + ")");
40 }
41
42
43 public static double distance(
44 double x1, double y1, double x2, double y2) {
45 return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
46 }
47 }