My code for implementation of DDA (Digital Differential Analyzer) algorithm is working good for drawing lines with slope less than 45o but fails in lines slope more than 45o.
It is behaving like a broken line for angles > 45o
Code
void dda(int x1,int y1,int x2,int y2)
{
float x,y,xinc,yinc,dx,dy;
int k,step;
dx = x2 - x1;
dy = y2 - y1;
step = abs(dx);
xinc = dx / step;
yinc = dy / step;
x = x1;
y = y1;
putpixel(x,y,63);
for(k=1;k<=step;k++)
{
x = x + xinc;
y = y + yinc;
putpixel(x,y,63);
}
}
Is this the demerit of DDA algorithm or there is any fault in my code, please help me to find out the cause that is making my program inefficient.
Thanks
No, There is no such demerit in DDA algorithm, you should use the following code, It will definitely work.
It will be better if you use Bresenham's line Drawing Algorithm.
You are only using dx to calculate unit step for your algorithm, thus it draws lines with angle < 45o.
You are not using dy in your program code for slope > 45o that is why your lines becomes broken. Append this code, and your code will work fine.
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);