DDA Line Drawing Algorithm (C Program)
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
float x, y, x1, y1, x2, y2, dx, dy, step, xinc, yinc;
void main()
{
int gd = DETECT, gm, i;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("Enter the coordinate of x1 and y1: ");
scanf("%f %f", &x1, &y1);
printf("Enter the coordinate of x2 and y2: ");
scanf("%f %f", &x2, &y2);
dx = x2 - x1;
dy = y2 - y1;
if (abs(dx) > abs(dy))
step = abs(dx);
else
step = abs(dy);
xinc = dx / step;
yinc = dy / step;
x = x1;
y = y1;
putpixel(x, y, WHITE);
for (i = 0; i < step; i++)
{
x = x + xinc;
y = y + yinc;
putpixel(x, y, WHITE);
}
getch();
closegraph();
}