/* Calcul sur des tableaux de sommets 3D */ /* representant des nuages de points */ public class BarycentreNuageSommets3D { ///////////////////////////////////////////////// /* Type agrege de stockage d'un sommet 3D */ static class Sommet3D { double x = 0.0; double y = 0.0; double z = 0.0; }; /* Fonction de calcul et de retour */ /* du barycentre d'un nuage de points stocké */ /* dans un tableau de Sommet3D */ static Sommet3D barycentre(Sommet3D [] tp) { Sommet3D p = new Sommet3D(); int i; for ( i = 0 ; i < tp.length ; i = i+1 ) { p.x = p.x+tp[i].x; p.y = p.y+tp[i].y; p.z = p.z+tp[i].z; } p.x = p.x/tp.length; p.y = p.y/tp.length; p.z = p.z/tp.length; return p; } ///////////////////////////////////////////////// /* Fonction de creation et retour d'un tableau */ /* de Sommet3D initialise avec des sommets */ /* de position tiree au sort dans un cube */ /* de rayon 0.5 centre sur l'origine */ /* n : La taille du tableau ŕ générer */ static Sommet3D [] nuageDansCube(int n) { int i; Sommet3D [] t = new Sommet3D[n]; for ( i = 0 ; i < n ; i = i+1 ) { t[i] = new Sommet3D(); t[i].x = Math.random()-0.5; t[i].y = Math.random()-0.5; t[i].z = Math.random()-0.5; } return t; } /* Programme principal */ public static void main(String [] args) { Sommet3D [] nuage; Sommet3D barycentre; int n; double r; Ecran.afficher("SVP, nombre de sommets? "); n = Clavier.saisirInt(); nuage = nuageDansCube(n); barycentre = barycentre(nuage); Ecran.afficherln("Barycentre:"); Ecran.afficherln("x = ",barycentre.x); Ecran.afficherln("y = ",barycentre.y); Ecran.afficherln("z = ",barycentre.z); } }