You are on page 1of 5

Experiment: Line Drawing using Bresenham’s line

Algorithm

Student Name: Mohit Singh                                    UID:19BCA1160


Branch:BCA                                                                  Section/Group:2/A
Semester: 5th sem                                                     Date of Performance:16/09/2021
Subject Name:Computer Graphics Lab                   Subject Code:CAP-306

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

Task to be done:
Write a program to draw a line using Bresenham line Algorithm with x and y.
Problem-01:
 
Write a program to draw a line with starting coordinates (9, 18) and ending coordinates (14, 22).
Problem-02:
 
Write a program to draw a line with starting coordinates (20, 10) and ending coordinates (30,
18).

3) Code for experiment/practical:


Code1:
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
void main(){
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
float x1,x2,y1,y2,x,y,di,t,s,dx,dy,m;
x1=9;
y1=18;
x2=14;
y2=22;

dx=x2-x1;
dy=y2-y1;
m=dy/dx;

x=x1;
y=y1;

t=2*(dy-dx);
s=2*dy;
di=2*dy-dx;
while(x<x2)
{
x++;

if(di<0)
{
di+=s;
putpixel(x,y,RED);

}
else
{
di+=t;
putpixel(x,y,GREEN);
cout<<x<<" "<<y;

y++;
}

}
getch();
}

Code2:
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
void main(){
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
float x1,x2,y1,y2,x,y,di,t,s,dx,dy,m;
x1=20;
y1=10;
x2=30;
y2=18;

dx=x2-x1;
dy=y2-y1;
m=dy/dx;
x=x1;
y=y1;

t=2*(dy-dx);
s=2*dy;
di=2*dy-dx;
while(x<x2)
{
x++;

if(di<0)
{
di+=s;
putpixel(x,y,RED);

}
else
{
di+=t;
putpixel(x,y,GREEN);
cout<<x<<" "<<y;
y++;
}

}
getch();
}

4) Output
Output 1

Output 2
5) Learning outcomes (What I have learnt):

1.Learn about Bresenham line algorithm.

2.Bresenham line algorithm is used for scan converting a line.

3. Learn about graphics

4.Learn about some methmaticals function

5: Graphics header Files

Evaluation Grid:

Sr. No. Parameters Marks Obtained Maximum Marks


1. Demonstration and 5
Performance (Pre Lab Quiz)
2. Worksheet 10
3. Post Lab Quiz 5

You might also like