
fourth
By: a guest on Oct 7th, 2008 | syntax:
C | size: 1.36 KB | hits: 91 | expires: Never
/*
** 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;
}
}
}
}