Frenchie Frenchie - 1 month ago 21
Java Question

How to find 3 closest coordinates from a point in an array Java

I have a homework assignment that is due tomorrow and I am completely stuck (level: beginner).

The assignment is:
Your municipality searches the best place for installing a public waste dump. They want the dump to be as far as possible from the residencies. The residencies are represented by their coordinates x and y in a two dimensional map. As the municipality has already a hint about the place where they want to place the dump, they want to verify that the distance between the desired position and the closest residency is reasonable and to place it in an optimal way close to the targeted place.


  • My 1st task was to create a method and calculate distances between 2 points, which I did.

  • My 2nd task was to create a method and find the closest distance from an user entry and a point in an array, which I did.

  • My 3rd task is to create a method and find 3 closest distances from an user entry and all the points in an array - and I am stuck here for 2 days now.

  • My final task is to find the best place for a waste dump.



The part of the code above the "Completez le programme a partir d'ici." and the "Ne rien modifier apres cette ligne." is predefined and I am not allowed to change it. I have to write my code between those lines.

Here's what I have so far:

import java.util.Scanner;
import java.util.Arrays;
import java.text.DecimalFormat;

public class Decharge {
private static DecimalFormat df = new DecimalFormat("#.000");

/*******************************************
* Completez le programme a partir d'ici.
*******************************************/
public static double calculerDistance (int x1, int y1, int x2, int y2) {
double distance;
distance = Math.sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));

return distance;
}


public static int plusProche (int x, int y, int[] coordonneesHabitations) {

double distanceA = 0.0;
int k = x;
int z = y;
int a = 0;
int b = 0;
int [] that = Arrays.copyOf(coordonneesHabitations, coordonneesHabitations.length);
int taille = that.length;
int i = 0;
double minDistance = Float.MAX_VALUE;
int position = 0;

for (i = 0; i < taille; i += 2) {
a = that[i];
b = that[i+1];
distanceA = calculerDistance(a, b, k, z);
if (distanceA < minDistance) {
minDistance = distanceA;
position = i/2;
System.out.println(i + " " + minDistance);
}
}
return position;
}

// This is where I'am stucked

public static int[] troisPlusProches (int x, int y, int[] coordonneesHabitations)
{
int [] that = Arrays.copyOf(coordonneesHabitations, coordonneesHabitations.length);
int table[] = new int[6];
double distanceA = 0.0;
double minDistance = Float.MAX_VALUE;
int a = 0;
int b = 0;
int i = 0;
int j = 0;

for (j = 0; j < table.length; j++)
{
for (i = 0; i < that.length; i += 2)
{
a = that[i];
b = that[i+1];
distanceA = calculerDistance(a, b, x, y);

if (distanceA < minDistance)
{
minDistance = distanceA;
table[j] = a;
table[j+1] = b;
System.out.println(j + " " + table[j] + " " + table[j+1] + " " + minDistance);
}
}
}

return table;
}


public static int[] meilleurePlace (int x, int y, int[] coordonneesHabitations) {
int nestoDrugo[] = {0,0};

return nestoDrugo;
}


/*******************************************
* Ne rien modifier apres cette ligne.
*******************************************/

public static void afficheTroisPlusProches(int x, int y, int[] coordonneesHabitations) {
int[] tpp = troisPlusProches(x, y, coordonneesHabitations);

System.out.println("(" + x + "," + y + ") est a :");
for (int i = 0 ; i < 3 ; i++) {
System.out.println(" " + df.format(calculerDistance(x, y, tpp[2*i], tpp[2*i+1])) + " de (" + tpp[2*i] + "," + tpp[2*i+1] + ")");
}
}



//PROGRAMME PRINCIPAL
public static void main(String args[]) {

int[] coordonneesHabitations = {
9, 30, 18, 8, 3, 18, 25, 36
};

// Lecture des donnees
Scanner clavier = new Scanner(System.in);
System.out.print("Entrez la coordonnee x de la decharge: ");
int x = clavier.nextInt();
System.out.print("Entrez le coordonnee y de la decharge: ");
int y = clavier.nextInt();

// trouve les coordonnees de l'habitation la plus proche
// (identifiees par l'indice correspondant dans le tableau
// de coordonnees)
int plusProche = plusProche(x, y, coordonneesHabitations);
System.out.println("--- Question 1 ---");
System.out.println("Coordonnees de l'habitation la plus proche de la decharge :");
System.out.println("(" + coordonneesHabitations[plusProche * 2] + "," + coordonneesHabitations[plusProche * 2 + 1] + ") ; distance = " +
df.format(calculerDistance(x, y, coordonneesHabitations[plusProche * 2], coordonneesHabitations[plusProche * 2 + 1])) + " metres");

// trouve les coordonnees des 3 habitations les plus proches et affiche les coordonnees

System.out.println("--- Question 2 ---");
System.out.println("Coordonnees des 3 habitations les plus proches de la decharge :");
afficheTroisPlusProches(x, y, coordonneesHabitations);

// affiche le centre de gravite des 3 plus proches (la meilleure place)
int[] grav = meilleurePlace(x, y, coordonneesHabitations);
System.out.println("--- Question 3 ---");
System.out.println("Coordonnees de la meilleure place pour la decharge :");
System.out.println("(" + grav[0] + "," + grav[1] + ")");
clavier.close();
}


}

Answer

I don't speak french so I find it hard to read your code. However, think about it like this:

Tou have a method which computes the closest point to a user entry. Now you need to create a copy of that method which allows you to compute the closest point to a user entry excluding the point you already found. That will let you find the first and second closest points. Then do the same thing to find the third point, this time by excluding the two points you already found.

You can make a copy of your existing method. It might look something like this:

public static int plusProche (int x, int y, int[] coordonneesHabitations, int ignoreIndex) {
    double distanceA = 0.0; 
    int k = x;
    int z = y;
    int a = 0;
    int b = 0;
    int [] that = Arrays.copyOf(coordonneesHabitations, coordonneesHabitations.length); 
    int taille = that.length;
    int i = 0;
    double minDistance = Float.MAX_VALUE;
    int position = 0;

       for (i = 0; i < taille; i += 2) {

           //here we add the ability to skip the passed index
           if ((i / 2) == ignoreIndex) {
               continue;
           }

           a = that[i];
           b = that[i+1]; 
           distanceA = calculerDistance(a, b, k, z);
           if (distanceA < minDistance) { 
               minDistance = distanceA;
               position = i/2;
               System.out.println(i + " " + minDistance);
           }
       }
       return position;
}

You can use the above to find the second closest point, by passing the index of the closest point as an argument. It will skip that index, therefore finding the next closest index. Do something similar to find the third closest point.

Comments