Professional Documents
Culture Documents
Algorithm
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).
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):
Evaluation Grid: