/* Auteur: Nicolas JANEY */ /* Avril 2001 */ /* Progammation de l'algorithme */ /* de Bresenham pour le trace de segments */ #include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include <stdio.h> #include <stdlib.h> #include <math.h> static int aff = 0 ; static float angle = 0 ; static float rouge[] = { 1.0F,0.0F,0.0F,1.0F }; static float vert[] = { 0.0F,1.0F,0.0F,1.0F }; static float bleu[] = { 0.0F,0.0F,1.0F,1.0F } ; static int p1[] = { 18,15 } ; static int p2[] = { 27,15 } ; static int anim = 0; void line(int *p1,int *p2) { glColor3fv(bleu) ; glBegin(GL_LINES) ; glVertex2f(p1[0]*10,p1[1]*10) ; glVertex2f(p2[0]*10,p2[1]*10) ; glEnd() ; } void pixel(int x,int y,float *c) { glColor3fv(c) ; glBegin(GL_QUADS) ; glVertex2f(x*10-5,y*10-5) ; glVertex2f(x*10-5,y*10+5) ; glVertex2f(x*10+5,y*10+5) ; glVertex2f(x*10+5,y*10-5) ; glEnd() ; } void ligneQuatreConnexite(float *c,int *p1,int *p2) { int dx,dy,i,xinc,yinc,cumul,x,y ; x = p1[0] ; y = p1[1] ; dx = p2[0] - p1[0] ; dy = p2[1] - p1[1] ; xinc = ( dx > 0 ) ? 1 : -1 ; yinc = ( dy > 0 ) ? 1 : -1 ; dx = abs(dx) ; dy = abs(dy) ; pixel(x,y,c) ; if ( dx > dy ) { cumul = dx / 2 ; for ( i = 1 ; i <= dx ; i++ ) { x += xinc ; cumul += dy ; if (cumul >= dx) { cumul -= dx ; y += yinc ; } pixel(x,y,c) ; } } else { cumul = dy / 2 ; for ( i = 1 ; i <= dy ; i++ ) { y += yinc ; cumul += dx ; if ( cumul >= dy ) { cumul -= dy ; x += xinc ; } pixel(x,y,c) ; } } } void ligneHuitConnexite(float *c1,float *c2,int *p1,int *p2) { int dx,dy,i,xinc,yinc,cumul,x,y ; x = p1[0] ; y = p1[1] ; dx = p2[0] - p1[0] ; dy = p2[1] - p1[1] ; xinc = ( dx > 0 ) ? 1 : -1 ; yinc = ( dy > 0 ) ? 1 : -1 ; dx = abs(dx) ; dy = abs(dy) ; pixel(x,y,c1) ; if ( dx > dy ) { cumul = dx / 2 ; for ( i = 1 ; i <= dx ; i++ ) { x += xinc ; cumul += dy ; if (cumul >= dx) { cumul -= dx ; y += yinc ; if ( cumul < dx/2 ) pixel(x,y-yinc,c2); else pixel(x-xinc,y,c2); } pixel(x,y,c1) ; } } else { cumul = dy / 2 ; for ( i = 1 ; i <= dy ; i++ ) { y += yinc ; cumul += dx ; if ( cumul >= dy ) { cumul -= dy ; x += xinc ; if ( cumul > dy/2 ) pixel(x,y-yinc,c2); else pixel(x-xinc,y,c2); } pixel(x,y,c1) ; } } } void ligneAvecMotif(float *c,int *p1,int *p2,int pattern) { int dx,dy,i,xinc,yinc,cumul,x,y ; int cp = 0; x = p1[0] ; y = p1[1] ; dx = p2[0] - p1[0] ; dy = p2[1] - p1[1] ; xinc = ( dx > 0 ) ? 1 : -1 ; yinc = ( dy > 0 ) ? 1 : -1 ; dx = abs(dx) ; dy = abs(dy) ; int bt = (pattern >> cp) & 0x1; if ( bt ) pixel(x,y,c) ; cp = (cp+1)%32; if ( dx > dy ) { cumul = dx / 2 ; for ( i = 1 ; i <= dx ; i++ ) { x += xinc ; cumul += dy ; if (cumul >= dx) { cumul -= dx ; y += yinc ; } int bt = (pattern >> cp) & 0x1; if ( bt ) pixel(x,y,c) ; cp = (cp+1)%32; } } else { cumul = dy / 2 ; for ( i = 1 ; i <= dy ; i++ ) { y += yinc ; cumul += dx ; if ( cumul >= dy ) { cumul -= dy ; x += xinc ; } int bt = (pattern >> cp) & 0x1; if ( bt ) pixel(x,y,c) ; cp = (cp+1)%32; } } } void display() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); switch (aff) { case 0 : ligneQuatreConnexite(rouge,p1,p2) ; line(p1,p2) ; break ; case 1 : ligneAvecMotif(rouge,p1,p2,0xF3A2D75B) ; line(p1,p2) ; break ; case 2 : ligneHuitConnexite(rouge,vert,p1,p2) ; line(p1,p2) ; break ; } glPopMatrix(); glFlush(); glutSwapBuffers() ; } void idle() { if ( anim ) { angle += 0.003F; p1[0] =(int) (16 + 10*cos(angle)); p2[0] =(int) (16 + 14*cos(1.314*angle)); p1[1] =(int) (16 + 8*sin(angle)); p2[1] =(int) (16 + 14*sin(1.314*angle)); glutPostRedisplay() ; } } void reshape(int w,int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,w,0,h,-10.0,10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void keyboard(unsigned char key,int x,int y) { switch ( key ) { case 0x0D : anim = !anim; break; case 0x20 : aff = (aff+1)%3; break; case '\033' : exit(0); break ; } glutPostRedisplay(); } void init() { glShadeModel(GL_SMOOTH); glClearColor(0.8F,0.8F,0.8F,1.0F); } void main(void) { glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(300,300); glutCreateWindow("Algorithme de Bresenham"); init(); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutReshapeFunc(reshape); glutIdleFunc(idle); glutMainLoop(); }
RETOUR