Algorithm
#define ROUND(a) ((int)(a+0.5))
voidlineDDA (int xa, int ya, int
xb, int yb)
{
int dx = xb - xa, dy = yb - ya,
steps, k;
floatxIncrement, yIncrement, x =
xa, y = ya;
if (abs (dx) > abs (dy) steps
= abs (dx) ;
else steps = abs dy);
xIncrement = dx / (float) steps;
yIncrement = dy / (float) steps
setpixel (ROUND(x), ROUND(y) ) :
for (k=0; k<steps; k++)
{
x += xIncrement;
y += yIncrement;
setpixel (ROUND(x), ROUND(y));
}
}
Algorithm
Description:
Step 1 :Accept Input as two endpoint pixel positions
Step 2: Horizontal and vertical differences between the endpoint
positions are assigned to parameters dx and dy (Calculate dx=xb-xa and
dy=yb-ya).
Step 3:The difference with the greater magnitude determines the value of
parameter steps.
Step 4 :Starting with pixel position (xa, ya), determine the offset needed
at each step to generate the next pixel position along the line path.
Step 5: loop the following process for steps number of times
a. Use a unit of increment or decrement
in the x and y direction
b. if xa is less than xb the values of
increment in the x and y directions are 1 and m
c. if xa is greater
than xb then the decrements -1 and – m are used.
Example: Consider the
line from (0,0) to (4,6)
1. xa=0, ya =0 and
xb=4 yb=6
2. dx=xb-xa = 4-0 = 4 and
dy=yb-ya=6-0= 6
3. x=0 and y=0
4 > 6 (false) so,
steps=6
5. Calculate xIncrement = dx/steps = 4 /
6 = 0.66 and yIncrement = dy/steps =6/6=1
6. Setpixel(x,y) =
Setpixel(0,0) (Starting Pixel Position)
7. Iterate the calculation for
xIncrement and yIncrement for steps(6) number of times
Advantages of DDA
Algorithm
1. It is the simplest
algorithm
2. It is
a is a faster method for calculating pixel positions
Disadvantages of DDA
Algorithm
1. Floating point arithmetic in DDA
algorithm is still time-consuming
2. End point accuracy
is poor
No comments:
Post a Comment