Pages

Thursday, February 17, 2011

SIMPLE LINE DRAWING 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,x,y; float m,c;
cout<<"enter valve of x and y(starting point)";
cin>>x1>>y1;
cout<<"enter value of x and y (end point)";
cin>>x2>>y2;
x=x1; y=y1;
m=float (y2-y1)/(x2-x1);
c=y1-m*x1;
cout<<"m="<<m;
cout<<"c="<<c;
if(m==0)
{
while(x<=x2)
{putpixel(x,y,RED);
x++;
}
}
if(abs(m)<=1)
{
while(x<=x2)
{
putpixel(x,y,RED);
x++;
y=m*x+c;
}
}
else
{
while (y<=y2)
{
putpixel(x,y,CYAN);
y++;
x=(y-c)/m;
}
}
getch();
closegraph();
}

No comments:

Post a Comment