Program for DDA line algorithm.

/*program to diplay a line using DDA algorithm*/

#include stdio.h
#include conio.h
#include graphics.h
#include math.h

void main()
{
int x1,y1,x2,y2,length,i;
float x,y;
int dx,dy,gd,gm=DETECT;
clrscr();
printf("Enter the co-ordinates of point 1\t");
scanf("%d %d",&x1,&y1);
printf("\nEnter the co-ordinates of point 2\t");
scanf("%d %d",&x2,&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
{
length=dx;
}
else
{
length=dy;
}
dx=(x2-x1)/length;
dy=(y2-y1)/length;
i=0;
x = x1+(0.5*dx);
y = y1+(0.5*dy);
while(i<=length)
{
putpixel(x,y,15);
x = x + dx;
y = y + dy;
i++;
}
getch();
closegraph();
}

No comments:

Post a Comment