I have a 2d array with x and y coordinates. I need to figure out the north most, south most, east most, and west most points and print them out. I figure the best way to do this is to find the minimum x value and the corresponding point will be the west most point, the maximum x value and the corresponding point will be the east most, the minimum y value and the corresponding point will be the south most, and the maximum y value and the corresponding point will be the north most. My code below goes through the 2d array of xy points and it finds the correct maximum and minimum x and y values, but I am not able to keep track of the correct index to print out the corresponding point. When I try and do the print statements I am not getting the correct points to be printed out. Any help is appreciated.
public static void MaxMin(int[][] a, int size){
int index=0,min_x = 0,max_x=0,min_y=0,max_y=0,max_x_index=0, max_y_index = 0, min_x_index=0, min_y_index=0;
if ( size%2 != 0 ){// if size is odd
min_x = a[0][0];
min_y = a[0][1];
max_x = a[0][0];
max_y = a[0][1];
index++;
}
else{// size is even
if ( a[0][0] < a[1][0] ){
min_x = a[0][0];
max_x = a[1][0];
}
if ( a[0][1] < a[1][1] ){
min_y = a[0][1];
max_y = a[1][1];
}
index = index + 2;
}
int big_x, small_x, small_y, big_y;
for ( int i = index; i < size; i = i+2 ){
if ( a[i][0] < a[i+1][0] ){ //one comparison
small_x = a[i][0];
big_x = a[i+1][0];
min_x_index = i;
max_x_index = i+1;
}
else{
small_x = a[i+1][0];
big_x = a[i][0];
min_x_index = i+1;
max_x_index = i;
}
if ( a[i][1] < a[i+1][1] ){ //one comparison
small_y = a[i][1];
big_y = a[i+1][1];
min_y_index = i;
max_y_index = i+1;
}
else{
small_y = a[i+1][1];
big_y = a[i][1];
min_y_index = i+1;
max_y_index = i;
}
if ( min_x > small_x ){ //one comparison
min_x = small_x;
}
if ( max_x < big_x ){ //one comparison
max_x = big_x;
}
if ( min_y > small_y ){ //one comparison
min_y = small_y;
}
if ( max_y < big_y ){ //one comparison
max_y = big_y;
}
}
System.out.println("Northmost Point: " + a[max_y_index][0] + "," + a[max_y_index][1]);
System.out.println("Southmost Point: " + a[min_y_index][0] + "," + a[min_y_index][1]);
System.out.println("Westmost Point: " + a[min_x_index][0] + "," + a[min_x_index][1]);
System.out.println("Eastmost Point: " + a[max_x_index][0] + "," + a[max_x_index][1]);
}