Program to draw a line using bresenham’s method

/* Program to draw a line using bresenham’s method */

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int x1,y1,x2,y2,x,y,xend,yend,dy,dx,d;
cout<<“\n\t\t\t\**Bresenham’s method to draw a line**\n”;
cout<<“\nEnter the x coordinates(x1,x2)\n”;
cin>>x1>>x2;
cout<<“\nEnter the y coordinates(y1,y2)\n”;
cin>>y1>>y2;
dx=x2-x1;
dy=y2-y1;
x=x1;
y=y1;
int gd=DETECT,gm;
initgraph(&gd,&gm,””);
setbkcolor(WHITE);
if(fabs(dx)>=fabs(dy))
{
d=(2*dy)-dx;
while(x<=x2)
{
putpixel(x,y,8);
x=x+1;
if(d>=0)
{
y=y+1;
d+=2*(dy-dx);
}
else
d+=2*dy;
}
}
else
{
d=(2*dx)-dy;
while(y<=y2)
{
putpixel(x,y,8);
y++;
if(d>=0)
{
x=x+1;
d+=2*(dx-dy);
}
else
d+=2*dx;
}
}
setcolor(8);
outtextxy(100,60,”Line drawing using Bresenham’s method”);
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *