Génération des sphères en c
Bonjour à vous,
Je souhaite générer un ensemble de sphères de rayon identique (on va dire r=1) dont je connais parfaitement les centres.
En faisant quelques recherches sur internet, je suis arrivé à trouver un code source donné sur ce site. C'est vraiment intéressant et correspond assez bien à ce que je compte faire (en généralisant au cas de plusieurs sphères).
Voici le code source :
Code:
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
|
void CreateUnitSphere(int dtheta,int dphi)
{
int n;
int theta,phi;
XYZ p[4];
for (theta=-90;theta<=90-dtheta;theta+=dtheta) {
for (phi=0;phi<=360-dphi;phi+=dphi) {
n = 0;
p[n].x = cos(theta*DTOR) * cos(phi*DTOR);
p[n].y = cos(theta*DTOR) * sin(phi*DTOR);
p[n].z = sin(theta*DTOR);
n++;
p[n].x = cos((theta+dtheta)*DTOR) * cos(phi*DTOR);
p[n].y = cos((theta+dtheta)*DTOR) * sin(phi*DTOR);
p[n].z = sin((theta+dtheta)*DTOR);
n++;
p[n].x = cos((theta+dtheta)*DTOR) * cos((phi+dphi)*DTOR);
p[n].y = cos((theta+dtheta)*DTOR) * sin((phi+dphi)*DTOR);
p[n].z = sin((theta+dtheta)*DTOR);
n++;
if (theta > -90 && theta < 90) {
p[n].x = cos(theta*DTOR) * cos((phi+dphi)*DTOR);
p[n].y = cos(theta*DTOR) * sin((phi+dphi)*DTOR);
p[n].z = sin(theta*DTOR);
n++;
}
/* Do something with the n vertex facet p */
}
}
} |
Dans ce code, je ne comprends pas ce que représente DTOR, s'agit-il d'une fonction incluse dans les librairies c, d'une simple conversion? Je pense avoir bien regarder sur son site mais aucune explication...
Quelqu'un peut-il m'expliquer ce qu'il a compris de ce code?
La sphère est tracée à partir du système d'équations :
x=cos(theta).cos(phi) (1)
y=cos(theta).sin(phi) (2)
z=sin(theta) (3)
où -90<=theta<=+90 et 0<=phi<=360
Utilisé le code tel qu'il est donné ci-dessus génère bien évidemment des erreurs à la compilation, voici donc le code que j'arrive à compiler sur mon pc sous windows avec MinGW:
Code:
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 62
|
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
double x,y,z;
} XYZ;
void CreateUnitSphere(int dtheta,int dphi)
{
int n;
int theta,phi;
XYZ p[4]= {0,0,1, 0,0,-1, -1,-1,0, 1,-1,0};
double DTOR;
for (theta=-90;theta<=90-dtheta;theta+=dtheta) {
for (phi=0;phi<=360-dphi;phi+=dphi) {
n = 0;
p[n].x = cos(theta*DTOR) * cos(phi*DTOR);
p[n].y = cos(theta*DTOR) * sin(phi*DTOR);
p[n].z = sin(theta*DTOR);
n++;
p[n].x = cos((theta+dtheta)*DTOR) * cos(phi*DTOR);
p[n].y = cos((theta+dtheta)*DTOR) * sin(phi*DTOR);
p[n].z = sin((theta+dtheta)*DTOR);
n++;
p[n].x = cos((theta+dtheta)*DTOR) * cos((phi+dphi)*DTOR);
p[n].y = cos((theta+dtheta)*DTOR) * sin((phi+dphi)*DTOR);
p[n].z = sin((theta+dtheta)*DTOR);
n++;
if (theta > -90 && theta < 90) {
p[n].x = cos(theta*DTOR) * cos((phi+dphi)*DTOR);
p[n].y = cos(theta*DTOR) * sin((phi+dphi)*DTOR);
p[n].z = sin(theta*DTOR);
n++;
}
}
}
}
main()
{
int dtheta,dphi;
dtheta=1;
dphi=1;
CreateUnitSphere(dtheta,dphi);
return 0;
} |
Merci à vous pour votre contribution.