### Draw a line using line DDA algorithm

#define ROUND(x) ( (int) (x+0.5) )
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void lineDDA(int, int, int, int);

void main()
{
int gdriver = DETECT, gmode, x1,x2,y1,y2;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

cleardevice();

printf("Enter the values for Starting point (X1,Y1) : ");
scanf("%d %d",&x1,&y1);

printf("Enter the values for Ending   point (X2,Y2) : ");
scanf("%d %d",&x2,&y2);

clrscr();   cleardevice();
lineDDA(x1,y1,x2,y2);

getch();

closegraph();
}

void lineDDA(int x1, int y1, int x2, int y2)
{
int dx = x2-x1, dy = y2-y1, step, i;
float xIncr, yIncr, x=x1, y=y1;
int midx = getmaxx()/2, midy = getmaxy()/2;

/* Co-ordinate Axis */
line(midx,0,midx,2*midy);
line(0,midy,2*midx,midy);

outtextxy(midx+20,midy+20,"(0,0)");
outtextxy(midx+310,midy+20,"X");
outtextxy(midx+20,midy-230,"Y");

/* Start & End Points */
printf("Start = (%d,%d)",x1,y1);
printf("\nEnd   = (%d,%d)",x2,y2);

putpixel(midx+x1,midy-y1,15);
putpixel(midx+x2,midy-y2,15);

outtextxy(midx+x1+20,midy-y1,"Start");
outtextxy(midx+x2+20,midy-y2,"End");

getch();

if (abs(dx) > abs(dy))
step = abs(dx);
else
step = abs(dy);

xIncr = dx / (float) step;
yIncr = dy / (float) step;

putpixel(midx+ROUND(x),midy-ROUND(y),4);

for(i=0 ; i<step ; i++)
{
delay(50);
x += xIncr;
y += yIncr;
putpixel(midx+ROUND(x),midy-ROUND(y),4);
}
}