#include<graphics.h>
using namespace std;
void drawline(int,int,int,int);
int main(){
int gdriver=DETECT, gmode, x1, y1, x2, y2;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
cout<<"Enter co-ordinates of start point: ";
cin>>x1>>y1;
cout<<"Enter co-ordinates of end point: ";
cin>>x2>>y2;
drawline(x1, y1, x2, y2);
getch();
closegraph();
return 0;
}
void drawline(int x1,int y1,int x2,int y2 )
{
int dx,dy,xinc,yinc,x,y,k,pk;
dx=x2-x1;
dy=y2-y1;
if(x2>x1)
xinc=1;
else
xinc=-1;
if(y2>y1)
yinc=1;
else
yinc=-1;
if(abs(dx)>abs(dy)){
x=x1;
y=y1;
pk=2*dy-dx;
for(k=0;k<=dx;k++)
{
if(pk<0)
{
putpixel(x,y,RED);
x=x+xinc;
y=y+0;
pk=pk+2*dy;
}
else{
putpixel(x,y,RED),
x=x+xinc;
y=y+yinc;
pk=pk+2*(dy-dx);
}
delay(100);
}
}
else {
x=x1;
y=y1;
pk=2*dx-dy;
for(k=0;k<=dy;k++)
{
if(pk<0)
{
putpixel(x,y,RED),
x=x+0;
y=y+yinc;
pk=pk+2*dx;
}
else{
putpixel(x,y,RED),
x=x+xinc;
y=y+yinc;
pk=pk+2*(dx-dy);
}
delay(100);
}
}
}
0 comments:
Post a Comment