Java Array multidimensional Arrays find the closest pair of points

Question

We would like to find the closest pair of points using multidimensional array.

Given a set of points, the closest-pair problem is to find the two points that are nearest to each other.

We can compute the distances between all pairs of points and find the one with the minimum distance.

 AnswerCode:
                                             
 public class Main {
   public static void main(String[] args) {
     int numberOfPoints = 5;
                                             
     // Create an array to store points
     double[][] points = new double[numberOfPoints][2];
     for (int i = 0; i < points.length; i++) {
       points[i][0] = Math.random()*(Math.random()*20);
       points[i][1] = Math.random()*(Math.random()*300);
     }/*  w ww  .j  a  va 2  s.c  o  m*/
                                             
     //your code 
   }
                                             
   /** Compute the distance between two points (x1, y1) and (x2, y2) */
   public static double distance(double x1, double y1, double x2, double y2) {
     return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
   }
 }



PreviousNext

Related