 
    
     
 
    
Le source: BresenhamSegment.cpp
/* Auteur: Nicolas JANEY         */
    /* nicolas.janey@univ-fcomte.fr  */
    /* Avril 2001                    */
    /* Illustration du trace         */
    /* de segments par l'algorithme  */
    /* de Bresenham                  */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #include <GL/glut.h>
    #include <GL/gl.h>
    #include <GL/glu.h>
    
    #include "ModuleCouleurs.h"
    #include "ModuleMenus.h"
    #include "ModuleReshape.h"
    
    static int aff = 0 ;
    static int yy = 0 ;
    static float bleu[] = { 0.0F,0.0F,1.0F,1.0F } ;
    
    void line(int *p1,int *p2) {
      glColor4fv(couleurBleu()) ;
      glBegin(GL_LINES) ;
      glVertex2f((float) p1[0],(float) p1[1]) ;
      glVertex2f((float) p2[0],(float) p2[1]) ;
      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 ligne(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 ligneIncomplete(float *c,int *p1,int *p2,int max,int aff) {
      int dx,dy,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 ;
        if ( aff && ( max == 0 ) ) {
          printf("dx = %d, dy = %d\n",dx,dy) ;
          printf("  x   c   y\n") ;
          printf("%3d %3d %3d\n",x,cumul,y) ; }
        for ( int i = 1,cp = 0 ; ( i <= dx ) && ( cp < max ) ; i++,cp++ ) {
          x += xinc ;
          cumul += dy ;
          if (cumul >= dx) {
            cumul -= dx ;
            y += yinc ; }
          if ( aff && ( cp == max - 1 ) )
            printf("%3d %3d %3d\n",x,cumul,y) ;
          pixel(x,y,c) ; } }
        else {
        cumul = dy / 2 ;
        if ( aff && ( max == 0 ) ) {
          printf("dx = %d, dy = %d\n",dx,dy) ;
          printf("  y   c   x\n") ;
          printf("%3d %3d %3d\n",y,cumul,x) ; }
        for ( int i = 1,cp = 0 ; ( i <= dy ) && ( cp < max ) ; i++,cp++ ) {
          y += yinc ;
          cumul += dx ;
          if ( cumul >= dy ) {
            cumul -= dy ;
            x += xinc ; }
          if ( aff && ( cp == max - 1 ) )
            printf("%3d %3d %3d\n",y,cumul,x) ;
          pixel(x,y,c) ; } }
    }
    
    void display() {
      int p1[] = { -25,-5 } ;
      int p2[] = { 20,27 } ;
      int p3[] = { 25,-28 } ;
      int p4[] = { 7,11 } ;
      int p5[] = { -15,-28 } ;
      int p6[] = { -7,3 } ;
      int p7[] = { -22,27 } ;
      int p8[] = { 7,21 } ;
      glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
      glPushMatrix();
      switch (aff) {
        case 0 : ligne(couleurRouge(),p1,p2) ;
                 ligne(couleurVert(),p3,p4) ;
                 ligne(couleurJaune(),p5,p6) ;
                 ligne(couleurCyan(),p7,p8) ;
                 line(p1,p2) ;
                 line(p3,p4) ;
                 line(p5,p6) ;
                 line(p7,p8) ;
                 break ;
        case 1 : ligneIncomplete(couleurRouge(),p1,p2,yy,0) ;
                 ligneIncomplete(couleurVert(),p3,p4,yy,0) ;
                 ligneIncomplete(couleurJaune(),p5,p6,yy,0) ;
                 ligneIncomplete(couleurCyan(),p7,p8,yy,0) ;
                 line(p1,p2) ;
                 line(p3,p4) ;
                 line(p5,p6) ;
                 line(p7,p8) ;
                 break ;
        case 2 : ligne(couleurRouge(),p2,p1) ;
                 ligne(couleurVert(),p4,p3) ;
                 ligne(couleurJaune(),p6,p5) ;
                 ligne(couleurCyan(),p8,p7) ;
                 line(p2,p1) ;
                 line(p4,p3) ;
                 line(p6,p5) ;
                 line(p8,p7) ;
                 break ;
        case 3 : ligneIncomplete(couleurRouge(),p2,p1,yy,0) ;
                 ligneIncomplete(couleurVert(),p4,p3,yy,0) ;
                 ligneIncomplete(couleurJaune(),p6,p5,yy,0) ;
                 ligneIncomplete(couleurCyan(),p8,p7,yy,0) ;
                 line(p2,p1) ;
                 line(p4,p3) ;
                 line(p6,p5) ;
                 line(p8,p7) ;
                 break ;
        case 4 : ligneIncomplete(couleurRouge(),p2,p1,yy,1) ;
                 line(p2,p1) ;
                 break ;
        case 5 : ligneIncomplete(couleurVert(),p4,p3,yy,1) ;
                 line(p4,p3) ;
                 break ;
        case 6 : ligneIncomplete(couleurJaune(),p6,p5,yy,1) ;
                 line(p6,p5) ;
                 break ;
        case 7 : ligneIncomplete(couleurCyan(),p8,p7,yy,1) ;
                 line(p8,p7) ;
                 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 == 8 )
                      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("Segment 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);
    }