#include #include #include struct position { double x; double y; double z; }; struct cube { double cote; struct position centre; }; int myRand(void) { return rand() * (RAND_MAX + 1) + rand(); } int randDansIntervalle(int bi, int bf) { return bi + myRand() % (bf - bi + 1); } struct cube* creerTableauCubes(int n) { return (struct cube*) calloc(n, sizeof(struct cube)); } void initialiserTableauCubes(struct cube* tc, int n) { for (int i = 0; i < n; i++) { tc[i].centre.x = randDansIntervalle(-5000, 5000) / 100.0; tc[i].centre.y = randDansIntervalle(-5000, 5000) / 100.0; tc[i].centre.z = randDansIntervalle(-5000, 5000) / 100.0; tc[i].cote = randDansIntervalle(500, 1500) / 100.0; } } bool etreInclusePositionDansCube(struct position p, struct cube cb) { double cs2 = cb.cote / 2.0; return ((p.x >= cb.centre.x - cs2) && (p.x <= cb.centre.x + cs2) && (p.y >= cb.centre.y - cs2) && (p.y <= cb.centre.y + cs2) && (p.z >= cb.centre.z - cs2) && (p.z <= cb.centre.z + cs2)); } void afficherIndicesCubes(struct cube* tc, int n, struct position p) { for (int i = 0; i < n; i++) { if (etreInclusePositionDansCube(p, tc[i]) == true) { printf("%5d\n", i); } } } void afficherCubes(struct cube* tc, int n) { for (int i = 0; i < n; i++) { printf("%5d : %7.2lf %7.2lf %7.2lf : %7.3lf\n", i, tc[i].centre.x, tc[i].centre.y, tc[i].centre.z, tc[i].cote); } } int main(void) { const int N = 1000; struct cube* tc = creerTableauCubes(N); if (tc == NULL) { printf("Tableau de cubes non cree\n"); return 1; } printf("Tableau de %d cubes cree\n", N); initialiserTableauCubes(tc, N); printf("Tableau de %d cubes initialise\n", N); struct position p = { -30.0,30.0,-20.0 }; //afficherCubes(tc, N); afficherIndicesCubes(tc, N, p); free(tc); tc = NULL; printf("Tableau libere\n"); return 0; }