简体   繁体   中英

Calculating largest euclidean distance between two values in a 2d array

I am trying to calculate the euclidean distance between 2 values in a 2d array. I need the largest, so I have 2 for loops to travers the arrays, and I also have a maxDistance variable to store the greatest distance as I compare.

My code looks like this

  //Returns the largest Euclidean distance between any two cities 
 within the cities array
 public static double furthestDistance(int[][] x)
 {
int power;
double sum = 0.0;
int distance = 0;
int maxDistance = 0;
for (int i = 0; i < x.length; i++)
{
  for(int j = 0; j<x[0].length; j++) 
  {
     sum = (x[i][j] - x[i][i+1]) + (x[i][j] - x[i][j+1]);

    power = (int) Math.pow(sum, 2);
    distance = (int)Math.sqrt(power);                     

    if (distance > maxDistance) 
    {
      maxDistance = distance;
    }



  }
}
return Math.sqrt(sum);
}

I am having issues, getting an error that says my arrayIndex is out of bounds, but I am not sure what the ebst way to travers my array to find the largest distance between any two values in my 2d array of around 10 "x,y coordinates"

x is an array of cities which looks like this

int[][] cities0 = {{22,-45},{-20,-43},{-45,29},{41,35},{21,4}, 
{23,-37},{16,-19},{-44,-10},{26,15},{6,-30},{2,35},{6,-19}}; 

I am not sure if I am approaching the problem the right way of if I am even calculating the distance properly?

The Euclidean distance is

public static double calculateDistance(int[] array1, int[] array2)
    {
        double Sum = 0.0;
        for(int i=0;i<array1.length;i++) {
           Sum = Sum + Math.pow((array1[i]-array2[i]),2.0);
        }
        return Math.sqrt(Sum);
    }

Now, the thing is that you have an array of points, each point is represented by a two-element array. The index out of bounds error stems from the fact that you have the line of

sum = (x[i][j] - x[i][i+1]) + (x[i][j] - x[i][j+1]);

which assumes that there is a next i and a next j, also, it assumes that the i'th element of x has at least i + 2 elements, which, if i > 0 will crash. Using the method I described at the start of my answer, your solution would look like:

double maxDistance = -1;
int firstPoint = -1;
int secondPoint = -1;
//notice that the limit is x.length - 1, because we will compare the penultimate
//item with the last
for (int i = 0; i < x.length - 1; i++) {
    for (int j = i + 1; j < x.length; j ++) {
        double d = calculateDistance(x[i], x[j]);
        if (d > maxDistance) {
            maxDistance = d;
            firstPoint = i;
            secondPoint = j;
        }
    }
}

Your Euclidean distance calculation was wrong in the initial code.

Also, the statement x[i][i+1] is problematic because you have tuples as data points and each tuple is in a different row, which means that the elements in the array can be accessible by either x[i][0] or x[i][1] and any value greater than 1 in the second bracket will cause an IndexOutOfBounds Error. Modifying the inner loop of the code can help you :

for(int j = i + 1; j<x.length; j++) 
{
    sum = Math.pow((x[i][0] - x[j][0]), 2) + Math.pow((x[i][1] - x[j][1]), 2);
    distance = Math.sqrt(sum);                     
    System.out.println(distance);
    if (distance > maxDistance) 
    {
        maxDistance = distance;
    }
}

Since we have tuples as our coordinates, x[i][0] - x[j][0] denotes the difference between x-coordinates of the two points and x[i][1] - x[j][1] denotes the difference between y-coordinates. Using that notation, we calculate Euclidean distance. The rest is what you have wrote previously. Also change the variables to double rather than int for more precision in your calculations! Note : This example is hard-coded for the input you gave in your question, you might need to modify it for using with different arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM