1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
public class DechargeEssai {
public static void main(String[] args) {
int [] coordonneesHabitations = {9, 30 ,18, 8, 3, 18, 25 , 36 };
System.out.println("la position dans le tableau du point le plus proche est : "+plusProche(10, 15, coordonneesHabitations));
System.out.println((troisPlusProches(10, 15, coordonneesHabitations)));
}
//calcule de la distance de 2 points avec pythagore
public static double calculerDistance(int x1, int y1, int x2, int y2){
return Math.sqrt((x1 - x2)*(x1 - x2)+(y1 - y2)*(y1 - y2));
}
//calcul du point le plus proche du point (x,y)
public static int plusProche(int x ,int y ,int[]coordonneesHabitations) {
double min = Integer.MAX_VALUE;
int position = 0;
// on cree une boucle avec pour index un couple de coordonnées => couple(x = index,y =index+1) donc on parcours la boucle au pas de 2
for (int index = 0; index < coordonneesHabitations.length-1; index+=2) {
int xcoor = coordonneesHabitations[index];
int ycoor = coordonneesHabitations[index+1];
//on cree une variable distanceMini avec la méthode calculerDistance
double distanceMini = calculerDistance(x, y, coordonneesHabitations[index],coordonneesHabitations[index+1]);
System.out.println(xcoor+" "+ycoor);
System.out.println(distanceMini);
// condition pour trouver la plus petite distance
if(distanceMini < min) {
min = distanceMini;
position = index;
}
}
return position ;
}
//la méthode doit renvoyer un tableau des 3 habitations les plus proches des points(x, y)
public static int[] troisPlusProches(int x, int y, int[]coordonneesHabitations ) {
int[] temp = new int[coordonneesHabitations.length];
System.arraycopy(coordonneesHabitations, 0,temp, 0, coordonneesHabitations.length);
int distanceMIni = plusProche(x, y, temp);
System.out.println(distanceMIni);
return temp;
}
} |
Partager