Posted by fourth on Wed 8 Oct 03:33
report abuse | View followups from Vector XX | download | new post
- /*
- ** File: drawLine.c
- ** Author: Nicholas Forysinski
- */
- #include <GL/glut.h>
- #include <drawLine.h>
- /*
- ** drawLine
- **
- ** Draw a line from vertex (x0,y0) to vertex (x1,y1) using
- ** the midpoint line algorithm (as discussed in class).
- */
- void drawLine( GLint x0, GLint y0, GLint x1, GLint y1 ) {
- GLint x, y;
- //check the orientation of the line
- if( x0 > x1 ) {
- drawLine( x1, y1, x0, y0 );
- return;
- }
- GLint d, dE, dNE;
- int dy = y1 - y0;
- int dx = x1 - x0;
- int stepx = 1;
- int stepy = 1;
- if( dy < 0 ) {
- dy = dy * -1;
- stepy = -1;
- }
- if( dx > dy ) {
- dE = 2 * dy;
- dNE = 2 * (dy - dx);
- d = dE - dx;
- y = y0;
- for ( x = x0; x != x1; x += stepx ) {
- //draw a pixel
- glBegin( GL_POINTS );
- glVertex2i( x, y );
- glEnd();
- if( d <= 0 ) {
- d += dE;
- } else {
- y += stepy;
- d += dNE;
- }
- }
- } else {
- dNE = 2 * (dx - dy);
- dE = 2 * dx;
- d = dE - dy;
- x = x0;
- for( y = y0; y != y1; y += stepy ) {
- //draw a pixel
- glBegin( GL_POINTS );
- glVertex2i( x, y );
- glEnd();
- if( d <= 0 ) {
- d += dE;
- } else {
- x += stepx;
- d += dNE;
- }
- }
- }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.