You are on page 1of 7

Computer Graphics

CSE 421 (Section-E)

Exp Name : Bresenham Line Drawing Algorithm


Implementation.
Lab Session- May 7, 2022, 2:30 - 5:00PM

1
Lab Session Problem – 01

➢ Experiment Name : Bresenham Line Drawing Algorithm


Implement In OpenGL Using C.

➢ Required Tools :
❖ Code Blocks
❖ OpenGL and GLUT using CodeBlocks.
❖ Tdm-gcc-10.3.0

➢ Expected Outcome :
❖ We will be introduced to OpenGL.We will be able
to use OpenGL with CodeBlocks.

❖ We will be able to draw a line using Bresenham’s


algorithm in OpenGL with two endpoints.

❖ Able to appreciate the knowledge along axis


(X,Y).

❖ We will be able to implement Bresenham’s


algorithm.

❖ Able to demonstrate effective OpenGL programs to


solve graphics programming issues including
other algorithm also.

2
➢ Session Details :

❖ Bresenham’s Line drawing Algorithm :

1) Input the line Start and End-points, Starting Point (𝑋0 , 𝑌0 ) Ending point(𝑋𝑛 , 𝑌𝑛 )

2) Plot the point (𝑋0 , 𝑌0 )

3) Calculate the constants ∆X = 𝑋𝑛 − 𝑋0, ∆Y = 𝑌𝑛 − 𝑌0 , 2∆X, 2∆Y and 2∆Y − 2∆X

4) And get the First value for the Decision Parameter as :

𝑃 = 2∆Y − ∆X

5) At each 𝑋𝐾 along the line, starting at 𝑘 = 0, perform the following test.

If 𝑃𝑘 < 0, the next point to plot is (𝑋𝐾 + 1, 𝑌𝑘 ) and :

𝑃𝐾+1 = 𝑃𝑘 + 2∆Y

If 𝑃𝑘 ≥ 0, the next point to plot is (𝑋𝐾 + 1,𝑌𝑘 + 1) and :

𝑃𝐾+1 = 𝑃𝑘 + 2∆Y − 2∆X

6) Repeat step 5 (Δx – 1) times.

3
Source Code :

After getting Bresenham’s algorithm’s theoretical concept, We will write


a program to implement Bresenham’s algorithm.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int x,y,x1,y1,x2,y2,dx,dy,p,i;
int gd=DETECT,gm;

printf("MD RIFAT BHUIYAN\n");


printf("191-15-2375\n");

printf("Enter Your Starting point : ");


scanf("%d%d",&x1,&y1);
printf("Enter Your Ending Point point : ");
scanf("%d%d",&x2,&y2);
initgraph(&gd,&gm,(char*)"");
dx=x2-x1;
dy=y2-y1;
p=2*dy-dx;
x=x1;
y=y1;
i=0;
while(i<=dx)
{

4
putpixel(x,y,WHITE);
if(p<0)
{
x=x+1;
p=p+2*dy;
}
else
{
x=x+1;
y=y+1;
p=p+2*dy-2*dx;
}

i++;

}
getch();
closegraph();
return 0;

5
➢ Output :

6
Here, Our expected outcome is our Starting point (50,80) and Ending Point
(200,300) and output will show one street line.

➢ Conclusion :

Mainly Bresenham's Line Algorithm use fixed point, i.e Integer Arithmetic.
Bresenham's Line Algorithm uses only subtraction and addition its
operation. Bresenham's Algorithm is faster than DDA Algorithm in line
because it involves only addition & subtraction in its calculation and uses
only integer arithmetic. Bresenham's Line Algorithm is more accurate and
efficient at DDA Algorithm.That’s why we learn and implement
Bresenham’s Line Drawing Algorithm.

You might also like