/*program to diplay a line using BRESENHAM's algorithm*/
#include stdio.h
#include conio.h
#include graphics.h
#include math.h
void main()
{
float x1,y1,x2,y2,dx,dy,p;
int x,y;
int gd,gm=DETECT,i;
clrscr();
printf("Enter the co-ordinates of point 1\t");
scanf("%f %f",&x1,&y1);
printf("\nEnter the co-ordinates of point 2\t");
scanf("%f %f",&x2,&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
x=x1;
y=y1;
putpixel(x,y,15);
p=(2*dy)-dx;
i=1;
do
{
if(p<0)
{
x=x+1;
p=p+(2 * dy);
}
else
{
x=x+1;
y=y+1;
p=p+(2*dy)-(2*dx);
}
putpixel(x,y,15);
i=i+1;
}while(i<=dx);
getch();
closegraph();
}
No comments:
Post a Comment