/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Avril 2001 */ /* Illustration du trace */ /* d'ellipse 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 ellipse(int *p1,float a,float b) { glColor4fv(couleurBleu()) ; glBegin(GL_LINE_LOOP) ; for ( int i = 0 ; i < 360 ; i++ ) { float angle = i * 3.14159F / 180 ; float x = (float) (p1[0] + a*cos(angle)) ; float y = (float) (p1[1] + b*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 ellipse(float *c,long a,long b) { int x,y ; double d1,d2 ; x = 0 ; y = b ; d1 = b*b - a*a*b + a*a/4 ; pixel(x,y,c) ; pixel(x,-y,c) ; pixel(-x,-y,c) ; pixel(-x,y,c) ; while ( a*a*(y-.5) > b*b*(x+1) ) { if ( d1 < 0 ) { d1 += b*b*(2*x+3) ; x++ ; } else { d1 += b*b*(2*x+3) + a*a*(-2*y+2) ; x++ ; y-- ; } pixel(x,y,c) ; pixel(x,-y,c) ; pixel(-x,-y,c) ; pixel(-x,y,c) ; } d2 = b*b*(x+.5)*(x+.5) + a*a*(y-1)*(y-1) - a*a*b*b ; while ( y > 0 ) { if ( d2 < 0 ) { d2 += b*b*(2*x+2) + a*a*(-2*y+3) ; y-- ; x++ ; } else { d2 += a*a*(-2*y+3) ; y-- ; } pixel(x,y,c) ; pixel(x,-y,c) ; pixel(-x,-y,c) ; pixel(-x,y,c) ; } } void ellipseIncomplete(float *c,long a,long b,int max,int aff) { int x,y ; long d1,d2 ; x = 0 ; y = b ; d1 = b*b - a*a*b + a*a/4 ; pixel(x,y,c) ; pixel(x,-y,c) ; pixel(-x,-y,c) ; pixel(-x,y,c) ; int cpt = 0; if ( ( cpt == max ) && aff ) printf("%3d %3d\n",x,y) ; while ( ( a*a*(y-.5) > b*b*(x+1) ) && ( cpt < max ) ) { cpt++; if ( d1 < 0 ) { d1 += b*b*(2*x+3) ; x++ ; } else { d1 += b*b*(2*x+3) + a*a*(-2*y+2) ; x++ ; y-- ; } if ( ( cpt == max ) && aff ) printf("%3d %3d %ld\n",x,y,d1) ; pixel(x,y,c) ; pixel(x,-y,c) ; pixel(-x,-y,c) ; pixel(-x,y,c) ; } d2 =(long) (b*b*(x+.5)*(x+.5) + a*a*(y-1)*(y-1) - a*a*b*b) ; while ( ( y > 0 ) && ( cpt < max ) ) { cpt++; if ( d2 < 0 ) { d2 += b*b*(2*x+2) + a*a*(-2*y+3) ; y-- ; x++ ; } else { d2 += a*a*(-2*y+3) ; y-- ; } if ( ( cpt == max ) && aff ) printf("%3d %3d %ld\n",x,y,d2) ; 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 : ellipse(couleurCyan(),7,5) ; ellipse(couleurRouge(),14,10) ; ellipse(couleurVert(),21,15) ; ellipse(couleurJaune(),28,20) ; ellipse(p,7,5) ; ellipse(p,14,10) ; ellipse(p,21,15) ; ellipse(p,28,20) ; break ; case 1 : ellipseIncomplete(couleurCyan(),7,5,yy,0) ; ellipseIncomplete(couleurRouge(),14,10,yy,0) ; ellipseIncomplete(couleurVert(),21,15,yy,0) ; ellipseIncomplete(couleurJaune(),28,20,yy,0) ; ellipse(p,7,5) ; ellipse(p,14,10) ; ellipse(p,21,15) ; ellipse(p,28,20) ; break ; case 2 : ellipseIncomplete(couleurJaune(),28,20,yy,1) ; ellipse(p,28,20) ; break ; case 3 : ellipseIncomplete(couleurVert(),21,15,yy,1) ; ellipse(p,21,15) ; break ; case 4 : ellipseIncomplete(couleurRouge(),14,10,yy,1) ; ellipse(p,14,10) ; break ; case 5 : ellipseIncomplete(couleurJaune(),28,20,yy,1) ; ellipse(p,28,20) ; 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,200); glutInitWindowPosition(50,50); glutCreateWindow("Ellipse par l'algorithme de Bresenham"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-22.0,22.0,-22.0,22.0,-4.0,4.0); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutDisplayFunc(display); glutMainLoop(); return(0); }