DDA Algorithm
The Digital Differential Analyzer DDA is a scan conversion line algorithm based on calculating either the difference of y or x ie ?y or ?x. The pixel positions are calculated initially- uses raster characteristics so that appropriate increments are applied in the x or y direction to step to pixel positions along the line path.
/* To draw a line using DDA(Digital Differential Analyzer) Algorithm */
#include
#include
#include
#include
#include
void main()
{
int gd=DETECT, gm; //initialize graphics driver and graphics module
initgraph(&gd, &gm, “C:\\tc\\bgi”);
int x1,x2,y1,y2,step;
cout<>x2>>y2;
int dx=(x2-x1);
int dy=(y2-y1);
if (abs(dx)>=abs(dy))
step= abs(dx);
else
step=abs(dy);
int xinc= dx/step;
int yinc= dy/step;
int x=x1;
int y=y1;
for(x=x1;x
{
putpixel(x,y,1);
x=x+xinc;
y=y+yinc;
}
getch();
closegraph();
}