Guest

fourth

By: a guest on Oct 7th, 2008  |  syntax: C  |  size: 1.36 KB  |  hits: 91  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. /*
  2. ** File:        drawLine.c
  3. ** Author:      Nicholas Forysinski
  4. */
  5.  
  6. #include <GL/glut.h>
  7. #include <drawLine.h>
  8.  
  9. /*
  10. ** drawLine
  11. **
  12. ** Draw a line from vertex (x0,y0) to vertex (x1,y1) using
  13. ** the midpoint line algorithm (as discussed in class).
  14. */
  15.  
  16. void drawLine( GLint x0, GLint y0, GLint x1, GLint y1 ) {
  17.  
  18.   GLint x, y;
  19.  
  20.   //check the orientation of the line
  21.   if( x0 > x1 ) {
  22.     drawLine( x1, y1, x0, y0 );
  23.     return;
  24.   }
  25.  
  26.   GLint d, dE, dNE;
  27.  
  28.   int dy = y1 - y0;
  29.   int dx = x1 - x0;
  30.   int stepx = 1;
  31.   int stepy = 1;
  32.  
  33.   if( dy < 0 ) {
  34.     dy = dy * -1;
  35.     stepy = -1;
  36.   }
  37.  
  38.   if( dx > dy ) {
  39.    
  40.     dE = 2 * dy;
  41.     dNE = 2 * (dy - dx);
  42.     d = dE - dx;
  43.     y = y0;
  44.    
  45.     for ( x = x0; x != x1; x += stepx ) {
  46.      
  47.       //draw a pixel
  48.       glBegin( GL_POINTS );
  49.         glVertex2i( x, y );
  50.       glEnd();
  51.      
  52.       if( d <= 0 ) {
  53.         d += dE;
  54.       } else {
  55.         y += stepy;
  56.         d += dNE;
  57.       }
  58.      
  59.     }
  60.    
  61.   } else {
  62.    
  63.     dNE = 2 * (dx - dy);
  64.     dE = 2 * dx;
  65.     d = dE - dy;
  66.     x = x0;
  67.  
  68.     for( y = y0; y != y1; y += stepy ) {
  69.      
  70.       //draw a pixel
  71.       glBegin( GL_POINTS );
  72.         glVertex2i( x, y );
  73.       glEnd();
  74.      
  75.       if( d <= 0 ) {
  76.         d += dE;
  77.       } else {
  78.         x += stepx;
  79.         d += dNE;
  80.       }
  81.      
  82.     }
  83.    
  84.   }
  85.  
  86. }