Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Advantage & Disadvantage of DDA Algo

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

Advantage & Disadvantage of DDA algo

Adva
It is a faster method for calculating pixel positions than the direct use.

Disadv The accumulation of round off error in successive


additions of the floating point increment, can cause the calculated pixel positions to drift away from the true line
path for long line segments. Rounding operations and floating point procedure in lineDDA are still time consuming.

Bresenhams Algorithm Theory


First consider scan conversion process for lines with +ve slopes <1 , (m<1).
Pixel position along the line path are determined by sampling at unit x intevals.

Starting from the left end point(x0,y0) of a given line, we step to each successive column (x position) and plot the pixel whose scan line y value is closest to the line path.

Bresenham's line algorithm


y = mx + b

y = m(x+1) + b y

d2
d1

x+1

Assume the given endpoints are (xa,ya) and (xb,yb), that xa < xb. Define x = xb - xa; y = yb - ya;
As in the DDA algorithm we will iterate through the relevant columns and set one pixel in each column.

Assume that in the k-th iteration, the pixel (xk,yk) is set.

Then in the next iteration xk+1,,, ,We will select either (xk+1,yk) or (xk+1,yk+1)

The y coordinate on the mathematical line at pixel position xk+1 is calculated as Y = m(Xk+1) + b

d1=y yk
= m(xk+1)+b - yk

d2=(yk+1)-y
=yk+1 m(xk+1)-b

But xk+1=xk+1

Pk+1 PK = 2 yxk + 2 y2 x(yk+1) + c

-[ 2 yxk
2

-2

xyk

+c

y -2

x(yk+1 yk)

Bresenham's line algorithm (slope 1)


input line endpoints, (x0,y0) and (xn, yn) calculate x = xn - x0 and y = yn - y0 calculate parameter p0 = 2 y - x set pixel at position (x0,y0) repeat the following steps until (xn, yn) is reached: if pi < 0 set the next pixel at position (xi +1, yi ) calculate new pi+1 = pi + 2 y if pi 0 set the next pixel at position (xi +1, yi + 1 ) calculate new pi+1 = pi + 2(y - x)

Compute using Bresenhams


End points (20,10) and (30,18) Slope= 0.8 x=10 y=8 Initial decision parameter has the value P0=2 y - x =6 Successive decision parameters increments r 2 y = 16 , 2 y 2 x = - 4

We plot initial point (20 , 10) and determine successive pixel position along the line path from the decision parameters as

K 0 1 2 3 4 5

Pk 6 2 -2 14 10 6

(xk+1,yk+1) K (21,11) (22,12) (23,12) (24,13) (25,14) (26,15) 6 7 8 9

Pk 2 -2 14 10

(xk+1,yk+1) (27,16) (28,16) (29,17) (30,18)

DDA works with floating point arithmetic Rounding to integers necessary

DDA versus Bresenhams Algorithm

Bresenhams algorithm uses integer arithmetic Constants need to be computed only once
Bresenhams algorithm generally faster than DDA

Bresenhams Algorithm Code


void lineBres(int xa, int ya, int xb, int yb) { int dx = abs(xb xa); int dy =abs( yb ya); Int p=2*dy dx; int twoDy = 2*dy; int twoDyDx=2*(dy dx); Int x, y,xend; If(xa>xb) { x = xb; y = yb; xEnd=xa; }

else{ x = xa; y = ya; xEnd=xb; } setPixel(x,y); While (x < xEnd){ x++; If (p<0) p += twoDy; Else{ y++; p += twoDyDx;} Setpixel(x,y); } }

You might also like