/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Avril 2001 */ /* 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" static int aff = 0 ; static int yy = 0 ; void circle(int *p1,float r) { glColor4fv(couleurBleu()) ; glBegin(GL_LINE_LOOP) ; for ( int i = 0 ; i < 360 ; i++ ) { float angle = i * 3.14159F / 180 ; float x = (float) (p1[0] + r*cos(angle)) ; float y = (float) (p1[1] + r*sin(angle)) ; glVertex2f(x,y) ; } glEnd() ; } void pixel(int x,int y,float *c) { glColor4fv(c) ; glBegin(GL_QUADS) ; glVertex2f(x-0.5F,y-0.5F) ; glVertex2f(x-0.5F,y+0.5F) ; glVertex2f(x+0.5F,y+0.5F) ; glVertex2f(x+0.5F,y-0.5F) ; glEnd() ; } void cercle(float *c,int r) { int x,y,d ; x = 0 ; y = r ; d = 1 - r ; 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) ; while ( y > x ) { if ( d < 0 ) d += 2 * x + 3 ; else { d += 2 * (x - y) + 5 ; y-- ; } x++ ; pixel(y,x,c) ; pixel(-y,x,c) ; pixel(y,-x,c) ; pixel(-y,-x,c) ; pixel(x,y,c) ; pixel(x,-y,c) ; pixel(-x,y,c) ; pixel(-x,-y,c) ; } } void cercleIncomplet(float *c,int r,int max,int aff) { int x,y,d ; x = 0 ; y = r ; d = 1 - r ; if ( aff && ( max == 0 ) ) { printf(" x c y\n") ; printf("%3d %3d %3d\n",x,d,y) ; } 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) ; int cp = 0 ; while ( ( y > x ) && ( cp < max ) ) { cp++ ; if ( d < 0 ) d += 2 * x + 3 ; else { d += 2 * (x - y) + 5 ; y-- ; } x++ ; if ( aff && ( cp == max ) ) printf("%3d %3d %3d\n",x,d,y) ; pixel(y,x,c) ; pixel(-y,x,c) ; pixel(y,-x,c) ; pixel(-y,-x,c) ; pixel(x,y,c) ; pixel(x,-y,c) ; pixel(-x,y,c) ; pixel(-x,-y,c) ; } } void display() { int p[] = { 0,0 } ; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); switch (aff) { case 0 : cercle(couleurRouge(),10) ; cercle(couleurVert(),15) ; cercle(couleurJaune(),20) ; cercle(couleurCyan(),25) ; circle(p,10) ; circle(p,15) ; circle(p,20) ; circle(p,25) ; break ; case 1 : cercleIncomplet(couleurRouge(),10,yy,0) ; cercleIncomplet(couleurVert(),15,yy,0) ; cercleIncomplet(couleurJaune(),20,yy,0) ; cercleIncomplet(couleurCyan(),25,yy,0) ; circle(p,10) ; circle(p,15) ; circle(p,20) ; circle(p,25) ; break ; case 2 : cercleIncomplet(couleurRouge(),10,yy,1) ; circle(p,10) ; break ; case 3 : cercleIncomplet(couleurVert(),15,yy,1) ; circle(p,15) ; break ; case 4 : cercleIncomplet(couleurJaune(),20,yy,1) ; circle(p,20) ; break ; case 5 : cercleIncomplet(couleurCyan(),25,yy,1) ; circle(p,25) ; 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) { switch ( key ) { case 32 : yy++ ; if ( yy == 60 ) yy = 0 ; glutPostRedisplay(); break; case 0x0D : yy = 0 ; aff++ ; if ( aff == 6 ) aff = 0 ; glutPostRedisplay(); break; case 0x1B : exit(0) ; break; } } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Cercle par l'algorithme de Bresenham"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-30.0,30.0,-30.0,30.0,-40.0,40.0); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutDisplayFunc(display); glutMainLoop(); return(0); }