A C++ program to draw a line using Bresenhams line algorithm


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT,gm,i,x1,y1,x2,y2,p,dx,dy;
initgraph(&gd,&gm,””);
cout<<“Enter point of x1:”;
cin>>x1;
cout<<“Enter point of y1:”;
cin>>y1;
cout<<“Enter point of x2:”;
cin>>x2;
cout<<“Enter point of y2:”;
cin>>y2;
dx=x2-x1;
dy=y2-y1;
// initial decision parameter
p=2*dy-dx;
i=dx;
while(i>0)
{
putpixel(x1,y1,WHITE);
if(p<0)
{
x1=x1+1;
p=p+2*dy;
}
else
{
x1=x1+1;
y1=y1+1;
p=p+2*dy-2*dx;
}
i–;}
getch();
closegraph();
}