0% found this document useful (0 votes)
21 views

The DDA Algorithm

The document describes several algorithms related to computer graphics and image processing including Bresenham's line drawing algorithm, the midpoint circle algorithm, boundary fill, and flood fill. It provides pseudocode to implement each algorithm.

Uploaded by

Dheeraj Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

The DDA Algorithm

The document describes several algorithms related to computer graphics and image processing including Bresenham's line drawing algorithm, the midpoint circle algorithm, boundary fill, and flood fill. It provides pseudocode to implement each algorithm.

Uploaded by

Dheeraj Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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

Bresenham’s Circle Algorithm


Algorithm:
For generating all the pixel coordinates in the
90º to 45º octant that are needed when scan-
converting a circle of radius r.
(i) int x = 0, y = r, d = 3-2r
(ii) while (x<= y)
{
setpixel(x,y)
if (d < 0)
d = d + 4x + 6
else
{
d = d + 4(x - y) + 10
y--
}
x++
}
Mid Point Circle Algorithm:
The following is a description of this midpoint circle algorithm that generates
the pixel coordinates in the 90˚ to 45 ˚ octant:
int x = 0, y = r, p = 1-r
while (x <=y)
{ setpixel(x,y);
if (p < 0)
p = p + 2x + 3
else {
p = p + 2(x – y) + 5
y- -
}
x++
}

Boundary Fill Algorithm


Void boundaryfill(x, y, fillcolor, boundary)
{
Int current;
current=getpixel(x,y);
if((current! = boundary) && (current ! = fillcolor))
{
putpixel (x, y, fillcolor)
boundaryfill(x + 1, y, fillcolor, boundary);
boundaryfill(x , y+1, fillcolor, boundary);
boundaryfill(x -1, y, fillcolor, boundary);
boundaryfill(x , y-1, fillcolor, boundary);
}}
Flood Fill Algorithm:
Void floodfill(x, y, fillcolor, oldcolor)
{
Int current;
current=getpixel(x,y);
if(current==oldcolor)
{
putpixel (x, y, fillcolor)
floodfill (x + 1, y, fillcolor, oldcolor);
floodfill (x , y+1, fillcolor, oldcolor);
floodfill (x -1, y, fillcolor, oldcolor);
floodfill (x , y-1, fillcolor, oldcolor);
}
}

You might also like