/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Avril 2004 */ /* Illustration du trace de */ /* cercle par l'algorithme */ /* de Bresenham (Midpoint) */ #include #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleMenus.h" #include "ModuleReshape.h" #include "ModuleManipulateur.h" static int aff = 0; static int r = 1; static int cpt; void circle(int xi,int yi,float r) { glColor4fv(couleurBleu()); glBegin(GL_LINE_LOOP); for ( int i = 0 ; i < 360 ; i++ ) { float angle = i * 3.14159F / 180; float x = (float) (xi + r*cos(angle)); float y = (float) (yi + r*sin(angle)); glVertex2f(x,y); } glEnd(); } void pixel(int x,int y,float *c) { cpt++; glColor4fv(c); glPushMatrix(); glTranslatef((float) x,(float) y,0.0F); glutWireCube(1.0); glPopMatrix(); } void pixelsSymetriques8(int x,int y,float *c) { pixel(x,y,c); pixel(-x,y,c); pixel(x,-y,c); pixel(-x,-y,c); pixel(y,x,c); pixel(-y,x,c); pixel(y,-x,c); pixel(-y,-x,c); } void pixelsSymetriques4(int x,int y,float *c) { pixel(x,y,c); pixel(x,-y,c); pixel(y,x,c); pixel(-y,x,c); } void pixelsDiagonale(int x,float *c) { pixel(x,x,c); pixel(x,-x,c); pixel(-x,x,c); pixel(-x,-x,c); } void cercle(float *c,int r) { int x,y,d; x = 0; y = r; d = 1 - r; pixelsSymetriques4(x,y,c); while ( y > x ) { if ( d < 0 ) d += 2 * x + 3; else { d += 2 * (x - y) + 5; y--; } x++; if ( x == y ) pixelsDiagonale(x,c); if ( y > x ) pixelsSymetriques8(x,y,c); } } void trame(int xi,int y,float *c) { for ( int x = -xi ; x <= xi ; x++ ) pixel(x,y,c); } void disque1(float *c,int r) { int x,y,d; x = 0; y = r; d = 1 - r; trame(y,x,c); while ( y > x ) { if ( d < 0 ) d += 2 * x + 3; else { d += 2 * (x - y) + 5; trame(x,y,c); trame(x,-y,c); y--; } x++; trame(y,x,c); trame(y,-x,c); } } void disque2(float *c,int r) { int *xt =(int *) calloc((r+1),sizeof(int)); int x,y,d; x = 0; y = r; d = 1 - r; xt[y] = x; xt[x] = y; while ( y > x ) { if ( d < 0 ) d += 2 * x + 3; else { d += 2 * (x - y) + 5; y--; } x++; if ( x > xt[y] ) xt[y] = x; if ( y > xt[x] ) xt[x] = y; } for ( x = -xt[0] ; x <= xt[0] ; x++ ) pixel(x,0,c); for ( y = 1 ; y <= r ; y++ ) for ( x = -xt[y] ; x <= xt[y] ; x++ ) { pixel(x,y,c); pixel(x,-y,c); } free(xt); } void display() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); switch (aff) { case 0 : circle(0,0,r); cpt = 0; cercle(couleurRouge(),r); printf("%d\n",cpt); break; case 1 : circle(0,0,r); cpt = 0; disque1(couleurRouge(),r); printf("%d\n",cpt); break; case 2 : circle(0,0,r); cpt = 0; disque2(couleurRouge(),r); printf("%d\n",cpt); break; } glPopMatrix(); glFlush(); glutSwapBuffers(); } void myinit() { glShadeModel(GL_SMOOTH); glClearColor(0.8F,0.8F,0.8F,1.0F); } void key(unsigned char key,int x,int y) { if ( keyManipulateur(key,x,y) ) glutPostRedisplay(); else switch ( key ) { case 45 : r--; if ( r < 1 ) r = 1; glutPostRedisplay(); break; case 43 : r++; glutPostRedisplay(); break; case 0x0D : aff = (aff+1)%3; glutPostRedisplay(); break; case 0x1B : exit(0); break; } } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(350,350); glutInitWindowPosition(50,50); glutCreateWindow("Cercles par l'algorithme du midpoint"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-20.0,20.0,-20.0,20.0,-300.0,300.0); glutReshapeFunc(reshapeOrthoBasique); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); setManipulateurDistance(1.0F); glutKeyboardFunc(key); glutDisplayFunc(display); glutMainLoop(); return(0); }