Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

3.Experiment Name: Bresenham's Line Drawing Algorithm.

Objective:
To implement Bresenham’s line drawing algorithm for drawing a line segment between two given endpoints A (x1, y2) and
B(x2, y2).

Program Developing Environment: Code Blocks (IDE).

Algorithm:

1. Input the two line end-points, storing the left end-point in (x0, y0).
2. Plot the point (x0, y0).
3. Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx) and get the first value for the
decision parameter as:
P0=2Δy-Δx.
4. At each xk along the line, starting at k = 0, perform the following test. If pk < 0,
the next point to plot is (xk+1, yk) and: pk+1=pk+2Δy
Otherwise, the next point to plot is (xk+1, yk+1) and:
pk+1=pk+2Δy-2Δx

Code:
#include<GL/glut.h>
#include<stdio.h>
#include<stdlib.h>
int x1,x2,y1,y2;
void display (void)
{
int dx=x2-x1;
int dy=y2-y1;
int p0 = 2*dy - dx;
float x=x1,y=y1;
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 1.0, 0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
int p =p0;
int k;
for(k=0;k<dx;k++)
{
if(p<0)
{
x = x+1;
glVertex2i(x,y);
}
else
{
x = x+1; y = y+1;
glVertex2i(x,y);
}
}
glEnd();
glFlush();
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0);
}
int main(int argc, char** argv)
{
printf("Enter the points\n(X1,Y1,X2,Y2):-\n");
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Breshanman Line Algorithm ");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Output:

Figure: output of the Bresenham's line drawing program


Discussion:

From the sample output we can see that the Bresenham’s Line Drawing is successful. For the following
above Bresenham’s Line-Drawing code the output picture is generated. This algorithm only helps to
draw the basic line.The resulted draw line is not smooth.

You might also like