Distance sur c

amine -  
 amine -
Bonjour,

Je dispose de 100 points possédant chacun des coordonnées (x,y). Je souhaiterais créer une matrice de 100 * 100 représentant la distance euclidienne entre chacun de ces points.
Etant donné que je débute sur c (CodeBlocks), quelqu'un peut-il me donner un petit coup de pouce?
Merci beaucoup,
A bientôt

4 réponses

  1. Hadra38 Messages postés 109 Statut Membre 11
     
    je propose :

    #include <math.h>
    typedef struct Point Point;
    struct Point
    {
        int x;
        int y;
    };
    
    Point tableau_de_point[100];
    int Matrice_Distance[100][100];
    
    // initialisation du tableau
    for(int i=0; i < 100; i++) {
    	tableau_de_point[i].x = ?? ;
    	tableau_de_point[i].y = ?? ;
    }
    // -----------
    
    // creation de la matrice de distance
    for(int i=0; i < 100; i++) {
        for(int j=0; j<100 ; j++) {
    		Matrice_Distance[i][j] = sqrt( (tableau_de_point[i].x - tableau_de_point[j].x) * (tableau_de_point[i].x - tableau_de_point[j].x) + (tableau_de_point[i].y - tableau_de_point[j].y) * (tableau_de_point[i].y - tableau_de_point[j].y) );
    	}
    }
    // -----------
    


    qu'en penses tu ?
    0
    1. Hadra38 Messages postés 109 Statut Membre 11
       
      aprés reflexion ...

      la fonction sqrt() de Math.h renvoie un "double" donc corrige :

      "int Matrice_Distance[100][100];" en "double Matrice_Distance[100][100];"
      0
  2. Char Snipeur Messages postés 10112 Date d'inscription   Statut Contributeur Dernière intervention   1 331
     
    optimisation:
    for(int i=0; i < 100; i++) {
        for(int j=0; j<100 ; j++) {
    		if(j>i)Matrice_Distance[i][j] = sqrt( (tableau_de_point[i].x - tableau_de_point[j].x) * (tableau_de_point[i].x - tableau_de_point[j].x) + (tableau_de_point[i].y - tableau_de_point[j].y) * (tableau_de_point[i].y - tableau_de_point[j].y) );
                    else if(i==j)Matrice_Distance[i][j]=0;
                    else Matrice_Distance[i][j]=Matrice_Distance[j][i];
    	}
    
    0
  3. Hadra38 Messages postés 109 Statut Membre 11
     
    bien vu
    0
  4. amine
     
    Je vous remercie les gars, ça marche
    0