Pages

Thursday, February 17, 2011

BRESENHAM'S LINE ALGORITHM

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void main()
{
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi ");
errorcode=graphresult();
if(errorcode!=grOk)
{
cout<<"graphics error"<<grapherrormsg(errorcode)<<endl;
cout<<"press any key to start";
getch();
exit(1);
}
clrscr();
int x1,y1,x2,y2,dx,dy,d,loc1,loc2;
cout<<"enter x and y (starting point)";
cin>>x1>>y1;
cout<<"enter x and y (end point)";
cin>>x2>>y2;
dy=y2-y1;
dx=x2-x1;
d=2*dy-dx;
loc1=2*dy;
loc2=2*(dy-dx);
while(x1<=x2)
{
if(d<0)
{
putpixel(x1,y1,BLUE);
d=d+loc1;
}
else
{
putpixel(x1,y1,RED);
d=d+loc2;
y1++;

}
x1++;
}
getch();
closegraph();
}

No comments:

Post a Comment