Digital Differential Analyzer (DDA) is a scan-conversion line drawing algorithm based on calculating either dx or dy using equation dy=mdx. Sampling line at unit intervals in one coordinate and determining corresponding integer values nearest the line path is done for other coordinate.
For a line of positive slope value less than or equal to 1, sampling is done at unit x intervals (dx=1) and corresponding y is calculated as yk+1=yk+m. Similarly, in case of slope greater than 1, dy=1 is kept sampling at unity intervals whereas each succeeding x value is calculated as: xk+1 = xk + 1/m.
Algorithm:
steps=dx
else
steps=dy
xinc=dx/steps
yinc=dy/steps
Plot(round(x),round(y))
x=x+xinc
y=y+yinc
k=k+1
while(k<=|steps|)
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include<conio.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,steps,xinc,yinc;
int k,gd,gm,xmax,ymax;
int gdriver=DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode,”c:\TC\BGI\”);
printf(“Enter the value of x1, y1, x2 and y2 : “);
scanf(“%f %f %f %f”,&x1, &y1, &x2, &y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,””);
dx=(x2-x1);
dy=(y2-y1);
if(fabs(dx)>=fabs(dy)){
steps=dx;
}
else {
steps=dy;
}
xinc=(dx/steps);
yinc=(dy/steps);
x=x1;
y=y1;
do
{
putpixel(x,y,1);
x=x+xinc;
y=y+yinc;
k=k+1;
}while(k<=fabs(steps));
getch();
}