C program to implement DDA algorithm

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int sign(int);
void main()
{
int gd=DETECT,gm;
int x1,x2,y1,y2,dx,dy,len,k;
float xi,yi,x,y;
clrscr();
initgraph (&gd,&gm,"C:\\TC\\BGI");
printf("Enter the co-ordinates of the first point \n");
printf("x1= ");
scanf("%d/n",&x1);
printf("y1= ");
scanf("%d/n",&y1);
printf("Enter the co-ordinates of the second point \n");
printf("x2= ");
scanf("%d/n",&x2);
printf("y2= ");
scanf("%d/n",&y2);
clrscr();
dx= x2-x1;
dy= y2-y1;
if (abs(dx) > abs(dy))
len = abs(dx);
else
len = abs(dy);
xi=(float)dx/len;
yi=(float)dy/len;
x=x1+0.5*sign(xi);
y=y1+0.5*sign(yi);
while(k<len)
{
putpixel (x,y,RED);
x=x+xi;
y=y+yi;
k++;
}
getch();
closegraph();
}
int sign(int a)
{if (a>0)
return (1);
elseif (a<0)
return (-1);
else return(0);
}
Previous
Next Post »