Problem:
The GPS navigation system is becoming increasingly popular. The system uses the graph and geometric algorithms to calculate distances and map a route. This problem presents a geometric problem for finding a closest pair of points. Given a set of points, the closest-pair problem is to find the two points that are nearest to each other. Create a program that reads from the user pair of values, and prints the nearest two numbers. You might need to make use of two-dimensional arrays, and the famous ma thematic formula that calculates the distance between two points.
Output:
Enter how many numbers you want to check:
8
Fill in the points:
-1 3 -1 -1 1 1 2 0.5 2 -1 3 3 4 2 4 -0.5
The closest two points are (1.0 ,1.0)(2.0 ,0.5)
8
Fill in the points:
-1 3 -1 -1 1 1 2 0.5 2 -1 3 3 4 2 4 -0.5
The closest two points are (1.0 ,1.0)(2.0 ,0.5)
Solution:
import java.util.Scanner; public class twoDimentionalArrays { public static double calculateDistance(double x1, double x2, double y1, double y2) { return Math.sqrt( Math.pow(y1 - x1, 2) + Math.pow(y2 - x2, 2) ); } public static void main (String[] args) { Scanner scan = new Scanner (System.in); System.out.println("Enter how many numbers you want to check: "); int numbers = scan.nextInt(); System.out.println("Fill in the points: "); double[][] list = new double[numbers][2]; double n1=0,n2=0,n3=0,n4=0; list[0][0] = scan.nextDouble(); list[0][1]= scan.nextDouble(); list[1][0] = scan.nextDouble(); list[1][1]= scan.nextDouble(); double min = calculateDistance(list[0][0],list[0][1],list[1][0],list[1][1]); for (int i = 2;i<list.length;i++) { list[i][0] = scan.nextDouble(); list[i][1] = scan.nextDouble(); } for (int j=0;j<list.length;j++) { for(int k=j+1; k<list.length;k++) { if (calculateDistance(list[j][0],list[j][1],list[k][0],list[k][1]) < min) { min = calculateDistance(list[j][0],list[j][1],list[k][0],list[k][1]); n1 = list[j][0]; n2 = list[j][1]; n3 = list[k][0]; n4 = list[k][1]; } } } System.out.println("The closest two points are " + "("+n1+" ," + n2+")" +"("+n3+" ," + n4+ ")") ; } }
No comments :
Post a Comment