The DDA Algorithm
The DDA Algorithm
Algorithm:
(x1,y1) (x2,y2) are the end points and dx, dy are the float variables.
(i) If abs(x2-x1) > abs(y2-y1) then
length = abs(x2-x1)
else
length = abs(y2-y1)
endif
(ii) dx = (x2-x1)/length
dy = (y2-y1)/length
(iii) x = x1 + 0.5, y = y1 + 0.5
(iv) i=0
(v) Plot (trunc(x), trunc(y))
(vi) x = x + dx, y = y + dy
(vii) i = i + 1
(viii) If i < length then go to step (v)
(ix) Stop
Bresenham's Line Rasterization algorithm for the first octant
(i) The line end points are (x1,y1) and (x2,y2), assumed not equal.
x, y, ∆x, ∆y are assumed integer; e is real
(ii) initialize variables
x=x1, y=y1, ∆x=x2-x1, ∆y=y2-y1, m=∆y/∆x
(iii) initialize e
e=m-½
(begin the main loop)
(iv) for i=1 to ∆x
setpixel (x,y)
while (e>0)
y=y+1
e=e-1
end while
x=x+1
e=e+m
(v) next i
(vi) finish
Bresenham’s Integer Line Drawing Algorithm for the first octant
(i) The line end points are (x1,y1) and (x2,y2), assumed not equal.
all variables are assumed integer
(ii) initialize variables
x=x1, y=y1, ∆x=x2-x1, ∆y=y2-y1
(iii) initialize e
ebar = 2∆y-∆x
(begin the main loop)
(iv) for i=1 to ∆x
setpixel (x,y)
while (ebar>0)
y=y+1
ebar=ebar-2∆x
end while
x=x+1
ebar=ebar+2∆y
(v) next i
(vi) finish
General Bresenham’s Line Drawing Algorithm for all quadrants
(i) The line end points are (x1,y1) and (x2,y2), assumed not equal.
all variables are assumed integer ;
(the Sign function returns -1,0,1 as its argument is <0,=0 or >0)
(ii) initialize variables
x=x1, y=y1, ∆x=abs(x2-x1), ∆y=abs(y2-y1), s1=sign(x2-x1), s2=sign(y2-
y1)
(iii) Interchange ∆x and ∆y depending on the slope of the line
if ∆y > ∆x then
Temp = ∆x , ∆x = ∆y, ∆y = Temp
Interchange = 1
else
Interchange = 0
end if
ebar = 2 * ∆y - ∆x
(main loop)
(iv) for i = 1 to ∆x
setpixel(x,y)
while(ebar > 0 )
if Interchnage = 1 then
x = x + s1
else
y = y+ s2
end if
ebar = ebar – 2* ∆x
end while
if Interchange = 1 then
y = y+s2
else
x= x+s1
endif
ebar = ebar +2* ∆y
(v) next i
(vi) finish