/* Trace d'un segment de droite en 2D */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2012 */ #include #include #include #include #include #include #include "Position2D.h" /* Dessin OpenGL */ void trace(int xi,int yi,int xf,int yf,int zoom) { glPointSize(zoom-2.0F); glBegin(GL_POINTS); int dx,dy,i,xinc,yinc,cumul,x,y; x = xi; y = yi; dx = xf - xi; dy = yf - yi; xinc = ( dx > 0 ) ? 1 : -1; yinc = ( dy > 0 ) ? 1 : -1; dx = abs(dx); dy = abs(dy); glVertex2i(x*zoom+zoom/2-1,y*zoom+zoom/2-1); if ( dx > dy ) { cumul = dx / 2; for ( i = 1 ; i <= dx ; i++ ) { x += xinc; cumul += dy; if ( cumul >= dx ) { cumul -= dx; y += yinc; } glVertex2i(x*zoom+zoom/2-1,y*zoom+zoom/2-1); } } else { cumul = dy / 2; for ( i = 1 ; i <= dy ; i++ ) { y += yinc; cumul += dx; if ( cumul >= dy ) { cumul -= dy; x += xinc; } glVertex2i(x*zoom+zoom/2-1,y*zoom+zoom/2-1); } } glEnd(); glPointSize(1.0F); } void trace(int xi,int yi,int xf,int yf) { glPointSize(9.0F); glBegin(GL_POINTS); int dx,dy,i,xinc,yinc,cumul,x,y; x = xi; y = yi; dx = xf - xi; dy = yf - yi; xinc = ( dx > 0 ) ? 1 : -1; yinc = ( dy > 0 ) ? 1 : -1; dx = abs(dx); dy = abs(dy); glVertex2i(x*11+4,y*11+4); if ( dx > dy ) { cumul = dx / 2; for ( i = 1 ; i <= dx ; i++ ) { x += xinc; cumul += dy; if ( cumul >= dx ) { cumul -= dx; y += yinc; } glVertex2i(x*11+4,y*11+4); } } else { cumul = dy / 2; for ( i = 1 ; i <= dy ; i++ ) { y += yinc; cumul += dx; if ( cumul >= dy ) { cumul -= dy; x += xinc; } glVertex2i(x*11+4,y*11+4); } } glEnd(); glPointSize(1.0F); } void trace(double xi,double yi,double xf,double yf) { trace((int) (xi+0.49),(int) (yi+0.49),(int) (xf+0.49),(int) (yf+0.49)); } void trace(Position2D *pi,Position2D *pf) { trace(pi->c[0],pi->c[1],pf->c[0],pf->c[1]); } void trace(double xi,double yi,double xf,double yf,int zoom) { trace((int) (xi+0.49),(int) (yi+0.49),(int) (xf+0.49),(int) (yf+0.49),zoom); } void trace(Position2D *pi,Position2D *pf,int zoom) { trace(pi->c[0],pi->c[1],pf->c[0],pf->c[1],zoom); }