0

I'm an enginnering student, our lecturer has ask us to create an opengl app tat shows the bersenham

so far i have derive the algo and the pixels...but i don't know how to draw a line between those pixels..

this is what i have so far

```
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0); // set the background to white
glColor3f(0, 0, 0);
glMatrixMode(GL_PROJECTION); // set projection parameters
glLoadIdentity();
gluOrtho2D(0.0,400.0,0.0,400.0);
}
void setPixel(int px, int py)
{
glBegin(GL_POINTS);
glColor3f(1, 1, 1);
glVertex2i(px, py);
glEnd();
}
void swap(int i, int j) {
int t = i;
i = j;
j = t;
}
void lineBresenham(int p1x, int p1y, int p2x, int p2y)
{
int F, x, y;
if (p1x > p2x) // Swap points if p1 is on the right of p2
{
swap(p1x, p2x);
swap(p1y, p2y);
}
// Handle trivial cases separately for algorithm speed up.
// Trivial case 1: m = +/-INF (Vertical line)
if (p1x == p2x)
{
if (p1y > p2y) // Swap y-coordinates if p1 is above p2
{
swap(p1y, p2y);
}
x = p1x;
y = p1y;
while (y <= p2y)
{
setPixel(x, y);
y++;
}
return;
} // Trivial case 2: m = 0 (Horizontal line)
else if (p1y == p2y)
{
x = p1x;
y = p1y;
while (x <= p2x)
{
setPixel(x, y);
x++;
}
return;
}
int dy = p2y - p1y; // y-increment from p1 to p2int dx
int dx = p2x - p1x; // x-increment from p1 to p2
int dy2 = (dy << 1); // dy << 1 == 2*dy
int dx2 = (dx << 1);
int dy2_minus_dx2 = dy2 - dx2; // precompute constant for speed up
int dy2_plus_dx2 = dy2 + dx2;
if (dy >= 0) // m >= 0
{
// Case 1: 0 <= m <= 1 (Original case)
if (dy <= dx)
{
F = dy2 - dx; // initial F
x = p1x;
y = p1y;
while (x <= p2x)
{
setPixel(x, y);
if (F <= 0)
{
F += dy2;
}
else
{
y++;
F += dy2_minus_dx2;
}
x++;
}
}
// replace all dy by dx and dx by dy)
else
{
F = dx2 - dy; // initial F
y = p1y;
x = p1x;
while (y <= p2y)
{
setPixel(x, y);
if (F <= 0)
{
F += dx2;
}
else
{
x++;
F -= dy2_minus_dx2;
}
y++;
}
}
}
else // m < 0
{
// Case 3: -1 <= m < 0 (Mirror about x-axis, replace all dy by -dy)
if (dx >= -dy)
{
F = -dy2 - dx; // initial F
x = p1x;
y = p1y;
while (x <= p2x)
{
setPixel(x, y);
if (F <= 0)
{
F -= dy2;
}
else
{
F -= dy2_plus_dx2;
}
x++;
}
}
// Case 4: -INF < m < -1 (Mirror about x-axis and mirror
// about y=x line, replace all dx by -dy and dy by dx)
else
{
F = dx2 + dy; // initial F
y = p1y;
x = p1x;
while (y >= p2y)
{
setPixel(x, y);
if (F <= 0)
{
F += dx2;
}
else
{
x++;
F += dy2_plus_dx2;
}
y--;
}
}
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
// Draws a line from (100,100) to (400,300).
lineBresenham(100, 100, 400, 300);
glFlush();
}
int main( int argc, char **argv)
{
glutInit(&argc, argv); // Initialize GLUT
glutInitWindowPosition(50,100); // Set top-left display window position
glutInitWindowSize(400, 400); // Set display window width and height
glutCreateWindow("An Example OpenGL Program");// Create display window
init(); // Execute initialization procedure
glutDisplayFunc(display); // Send graphics to display window
glutMainLoop(); // Display everything and wait
}
```

can someone guide me onto the correct path..thank you