Beruflich Dokumente
Kultur Dokumente
Output primitives
Topics
Creation of simple objects
Line
Circle
Ellipse
Filling areas
Objects are created on the display device by
bringing light to a collection of pixels. Assume
that setpixel (x, y, c) will light position (x,y)
with color c.
Line drawing
DDA
Uses floating point calculations
Bresenham’s method
Uses only integer arithmetic
Line drawing
Designed to take inputs of end points
With (x1,y1) and (x2,y2) as end points the
equation is y=mx+c where m=(y2-y1)/(x2-x1)
and c=y1-m*x1
When x increase (or decrease) is more, then x is
incremented in units of 1 (or -1) and y
computed in increments of m (dy /|dx|) and
rounded
This is DDA
DDA
Ex: line between (20,10) and (28, 15)
m=5/8
xinc=1, yinc=5/8=0.625
The points to be plotted on the device are
(20,10), (21, round(10+.625)) / (21, 11), (22,
round(10+2*0.625)) / (22,11), (23,12), (24,
13), (25, 13), (26, 14), (27, 14), (28, 15)
DDA
Ex: End points are (20, 20), (12, 25)
Here x decreases rapidly when compared to y
increase
dx=-8, dy=5
Number of points= max(|dx|, |dy|)=steps=8
xinc=dx/steps=-1, yinc=dy/steps=5/8
Points generated are (20, 20), (19, round(20+.625)) /
(19, 21), (18, 20+.625+.625)/(18,21), (17, 22), (16,
23), (15, 23), (14, 24), (13, 24), (12, 25)
DDA Line drawing
When y increase (or decrease) is more, then y is
incremented in units of 1 (or -1) and x computed
in increments of 1/m (dx/|dy|) and rounded
Note that x=(y-c)/m
Ex: end points be (10, 20), (15, 30)
dx=5, dy=10, |dx| > |dy|
xinc=1/m=5/10=0.5, yinc=1
Points plotted are (10, 20), (10.5, 21)/(11,21), (11,
22), (11.5, 23)/(12,23), …
DDA Line drawing
When y increase (or decrease) is more, then y is
incremented in units of 1 (or -1) and x computed
in increments of 1/m (dx/|dy|) and rounded
Note that x=(y-c)/m
Ex: end points be (10, 20), ( 5, 30)
dx=-5, dy=10, |dx| > |dy|
xinc= -5/10= -0.5, yinc=1
Points plotted are (10, 20), (9.5, 21)/(10,21), (9,
22), (8.5, 23)/(9,23), …
DDA
Procedure DDA(xa, ya, xb, yb, c)
dx=xb-xa, dy=yb-ya
If abs (dx) >=abs(dy) then steps=abs(dx)
else steps=abs(dy)
xinc=dx/steps, yinc=dy/steps
x=xa, y=ya
Setpixel(x, y, c)
For i=1 to steps
x=x+xinc, y=y+yinc
Setpixel (round(x), round(y),c)
End for
End DDA
Bresenham’s line drawing
Suppose dx, dy are positive and dx > dy
Let (x1, y1) be the first point of the line
Then call setpixel(x1, y1, c). First pixel is lit.
Suppose that equation to the line to be displayed is y=mx+c
As x increment is positive and greater xinc=1
Suppose that k pixels are plotted
xk+1= xk +1 and yk+1 = yk +1 or yk
Actual y coordinate at xk+1 is m* xk+1 +c = m* (xk +1) +c =y and this may
be a floating point number
We find the difference between the actual y and these two possible y’s
and go for that y which is closer to actual y
pk=(y- yk ) -(yk +1-y) is the decision parameter
pk =2*dy* xk – 2*dx* yk + 2*c*dx-dx (prove this!)
Sign of pk will determine the next y coordinate
Bresenham’s line drawing
Case 1
If pk is positive or zero then yk+1is closer to y
We set yk+1 = yk +1 and plot (xk+1,yk+1 )
We compute the next step decision parameter pk+1
xk+2= xk+1 +1 and yk+2 = yk+1 +1 or yk+1 (these are possible y’s)
Actual y coordinate at xk+2 is m* xk+2 +c = m* (xk+1 +1) +c
=y and this may be a floating point number
As in the previous step we calculate ‘p’ as difference
between actual y and the possible y’s
Bresenham’s line drawing
pk+1=(y- yk+1 ) -(yk+1 +1-y) is the decision
parameter
pk+1 = (y- yk +1) -(yk +1+1-y) = pk +2*dy-2*dx
Case 2
If pk is negative then yk is closer to y
We set yk+1 = yk and plot (xk+1,yk+1 )
‘p’ is updated as
pk+1 = (y- yk ) -(yk +1-y) = pk +2*dy
Bresenham’s line drawing
Using current ‘p’ at any step a point is plotted – integer arithmetic
‘p’ is updated – integer arithmetic
Line drawing this way uses only integer arithmetic so far
We find initial ‘p’ and if this also uses only integers then all
computations in the algorithm are all integers
Plot first point (x1, y1)
x2=x1+1, y2=y1 or y1+1 and p1=(y- y1 ) -(y1 +1-y)
Here y=m*x2 +c = m (x1+1)+c
p1 = 2*dy-dx (prove this!) also uses only integer operations
Thus the points to be plotted on the display device are found using
only integer operations
Bresenham’s algorithm
The algorithm for the special case of dx, dy are positive and dx >= dy is given here
Bresenham-line1 (xa, ya, xb, yb, c)
dx=xb-xa, dy=yb-ya
d1=2*dy, d2=2*dy-2*dx, p= 2*dy-dx
x=xa, y=ya
setpixel(x, y, c)
For i=1 to dx-1 in steps of 1
x=x+1
If p>=0 then y= y+1 and p=p+d2
else p=p+d1
setpixel(x, y, c)
End for
setpixel(xb, yb, c)
End Bresenham-line1
Bresenham’s line drawing
For the case of dx, dy positive and dy > dx swap the role of x, y and dx and
dy in the previous algorithm
Bresenham-line2 (xa, ya, xb, yb, c)
dx=xb-xa, dy=yb-ya
d1=2*dx, d2=2*dx-2*dy, p= 2*dx-dy
x=xa, y=ya
setpixel(x, y, c)
For i=1 to dy-1 in steps of 1
y=y+1
If p>=0 then x= x+1 and p=p+d2
else p=p+d1
setpixel(x, y, c)
End for
setpixel(xb, yb, c)
End Bresenham-line2
Bresenham’s line drawing
By adjusting x and y increments these two algorithms can be made to
work for all cases
Bres-line1 (xa, ya, xb, yb, c)
dx=abs(xb-xa), dy=abs(yb-ya)
d1=2*dy, d2=2*dy-2*dx, p= 2*dy-dx
If xa < xb, xinc=1 endif If xa=xb, xinc=0 endif If xa > xb, xinc=-1 endif
If ya < yb yinc=1 endif If ya=yb, yinc=0 endif If ya > yb, yinc=-1 endif
x=xa, y=ya, setpixel(x, y, c)
For i=1 to dx-1 in steps of 1
x=x+xinc
If p>=0 then y= y+yinc and p=p+d2
else p=p+d1 Endif
setpixel(x, y, c)
End for
setpixel(xb, yb, c)
End Bres-line1
Bresenham’s line drawing
For the case of more of y change than that of x
Bres-line2 (xa, ya, xb, yb, c)
dx=abs(xb-xa), dy=abs(yb-ya)
d1=2*dx, d2=2*dx-2*dy, p= 2*dx-dy
If xa < xb, xinc=1 endif If xa=xb, xinc=0 endif If xa > xb, xinc=-1 endif
If ya < yb yinc=1 endif If ya=yb, yinc=0 endif If ya < yb, yinc=-1 endif
x=xa, y=ya, setpixel(x, y, c)
For i=1 to dy-1 in steps of 1
y=y+yinc
If p>=0 then x= x+xinc and p=p+d2
else p=p+d1 Endif
setpixel(x, y, c)
End for
setpixel(xb, yb, c)
End Bres-line2
Bresenham’s line drawing
For any case the algorithm is given as follows
Bres-line (xa, ya, xb, yb, c)
dx=abs(xb-xa), dy=abs(yb-ya)
If dx >= dy call Bres-line1(xa, ya, xb, yb, c)
Else call Bres-line2 (xa, ya, xb, yb, c)
End Bres-line
Tracing Bresenham’s algorithm
End points be (12, 24), (22, 20)
dx=10, dy=-4 (|dx| > |dy| so execute Bres-line1)
xinc= 1, yinc=-1
p= 2*|dy|-|dx|= -2, d1= 2*|dy|= 8, d2=2*|dy|-2*|dx|= -12
p -2 6 -6 -2 6
Pixel (12, 24) (13, 24) (14, 23) (15, 23) (16, 23) (17, 22)
Next p p+d1 p+d2 p+d1 p+d1 p+d2
p -6 2 -10 -2 6
Pixel (18, 22) (19, 21) (20, 21) (21, 21) (22, 20)
Next p p+d1 p+d2 p+d1 p+d1
Tracing Bresenham’s algorithm
End points be (12, 24), (8, 30)
dx=-4, dy= 6 (|dx| < |dy| so execute Bres-line2)
xinc= -1, yinc= 1
p= 2*|dx|-|dy|= -4, d1= 2*|dx|= 8, d2=2*|dx|-2*|dy|= -4
p -4 4 0 -4 4
Pixel (12, 24) (12, 25) (11, 26) (10, 27) (10, 28) (9, 29)
Next p p+d1 p+d2 p+d2 p+d1 p+d2
p 0
Pixel (8, 30)
Next p
Circle drawing
Simple methods
Uses floating point calculations
Bresenham’s method
Uses only integer arithmetic
Midpoint circle method
Uses only integer arithmetic
Circle drawing
Symmetry in circle
4 quadrants are shown using white lines
If (a, b) is on the circle in I quadrant, then (-a, b),
(-a, -b), (a, -b) are points on the circle in II, III, IV quadrants
Blue and white lines show 8 way
symmetry
If (a, b) is a point on the circle in 8 1
2
region 1 then (b, a), (b, -a), (a, -b), 7
polarcircle(0, 0, r, c)
pi=22/7
for phi=pi/2 to pi/4 in steps of
x=round(r*cos(phi)), y=round(r*sin(phi))
setpixel(x, y, c), setpixel(y, x, c)
setpixel(y, -x, c), setpixel(x, -y, c)
setpixel(-x, -y, c), setpixel(-y, -x, c)
setpixel(-y, x, c), setpixel(-x, y, c)
endfor
end polarcircle
Polarcircle1
Circle with center (h, k)
polarcircle1(h, k, r, c)
pi=22/7
for phi=pi/2 to pi/4 in steps of
x=round(r*cos(phi)), y=round(r*sin(phi))
setpixel(x+h, y+k, c), setpixel(y+h, x+k, c)
setpixel(y+h, -x+k, c), setpixel(x+h, -y+k, c)
setpixel(-x+h, -y+k, c), setpixel(-y+h, -x+k, c)
setpixel(-y+h, x+k, c), setpixel(-x+h, y+k, c)
endfor
end polarcircle1
Bresenham’s circle
Circle drawn using only integer operations
As in the line algorithm a decision parameter is found and based on the sign of
this, y is updated (points close to portion of the circle in sector 1 is generated
and symmetrical points in the remaining sectors are plotted)
Suppose that k points are generated by the algorithm and the k th point be (xk,
yk)
xk+1 =xk + 1 and yk+1 =yk or yk -1
y is such that yk -1 ≤ y ≤ yk
D(T)= (xk +1)2 + (yk -1)2-r2 is ≤ 0
D(S)= (xk +1)2 + (yk )2-r2 is ≥ 0
pk=D(T)+D(S)
If pk ≥ 0, then D(T) is smaller and hence (xk +1, yk -1) is closer to the circle and
xk+1 =xk + 1 and yk+1 = yk -1
We can show that pk+1 = pk + 4 xk -4yk +10 = pk + 4 xk+1 -4yk+1 +2
Else, (xk +1, yk ) is closer to circle
We can show that pk+1 = pk + 4 xk +6 = pk + 4xk+1 +2
The first point on the circle is (0,r). P1=2(1)+(r-1)2+r2-2r2=3-2r
Bresenham’s circle
Bresenham-circle(h,k,c)
x=0, y=r, p=3-2r
setpixel(x+h, y+k, c), setpixel(x+h, -y+k, c), setpixel(y+h, x+k, c), setpixel(-y+h, x+k, c)
while (x<=y)
if p>=0 then p=p+4x-4y+10, y=y-1
else p=p+4x+6
endif
x=x+1
setpixel(x+h, y+k,c ), setpixel(y+h, x+k,c)
setpixel(x+h, -y+k,c), setpixel(y+h, -x+k, c)
setpixel(-x+h, -y+k,c), setpixel –y+h, -x+k, c)
setpixel(-x+h, y+k,c), setpixel (-y+h, x+k, c)
endwhile
end Bresenham-circle
Midpoint circle
Circle drawn using only integer operations
As in the line algorithm a decision parameter is found and based on the sign of
this, y is updated (points close to portion of the circle in sector 1 is generated
and symmetrical points in the remaining sectors are plotted)
Let f(x, y)= x2+y2-r2
If f(x, y) <, =, > 0 then (x, y) is inside, on, outside the circle
Suppose that k points are generated by the algorithm and the k th point be (xk,
yk)
xk+1 =xk + 1 and yk+1 =yk or yk -1
y is such that yk -1 ≤ y ≤ yk
Possible (k+1)th pixels are (xk + 1, yk ) and (xk + 1, yk -1)
Midpoint of these two pixels is (xk + 1, yk -1/2)
Sign of f(xk + 1, yk -1/2) decides the next pixel
f(xk + 1, yk -1/2) = (xk + 1)2+( yk -1/2)2-r2 =pk
Midpoint circle
Case 1
pk>=0
Then midpoint is outside. So the point inside is
close to circle.
xk+1=xk+1 and yk+1=yk-1
pk+1 = (xk+1 + 1)2+( yk+1 -1/2)2-r2 = (xk + 1+1)2+
( yk -1-1/2)2-r2 = pk+ 2xk -2 yk +5 =pk+ 2xk+1
-2yk+1 -1
Midpoint circle
Case 2
Pk<0
Then midpoint is inside. So the point outside is close to circle.
xk+1=xk+1 and yk+1=yk
pk+1 = (xk+1 + 1)2+( yk+1 -1/2)2-r2 = (xk + 1+1)2+( yk -1/2)2-r2 = pk+
2xk +3 = pk+ 2xk+1 +1
Circle is plotted from (0,r). The next pixel is (1, r) or (1, r-1).
Midpoint of these two pixels is (1, r-1/2) and p1= (0 + 1)2+( r
-1/2)2-r2 =p1= 5/4-r
If r is integer (usually it is) we can take p= 1-r. Thus starting p is
integer and all updations on p are integer and so the algorithm is
integer arithmetic algorithm
Midpoint circle algorithm
Midpointcircle(h,k,c)
x=0, y=r, p=1-r
setpixel(x+h, y+k, c), setpixel(x+h, -y+k, c), setpixel(y+h, x+k, c), setpixel(-y+h, x+k, c)
while (x<=y)
x=x+1
if p>=0 then y=y-1, p=p+2x-2y+1,
else p=p+2x+1
endif
setpixel(x+h, y+k,c ), setpixel(y+h, x+k,c)
setpixel(x+h, -y+k,c), setpixel(y+h, -x+k, c)
setpixel(-x+h, -y+k,c), setpixel –y+h, -x+k, c)
setpixel(-x+h, y+k,c), setpixel (-y+h, x+k, c)
endwhile
end Midpointcircle
Bresenham’s circle drawing
Circle centre(10, 15) and radius 5
If p >= 0 then p=p+4x-4y+10, x=x+1, y=y-1
Else p=p+4x+6, x=x+1
p=3-2r=-7, x=0, y=5
points (0+10, 5+15), (0+10, -5+15), (5+10, 0+15) and (-5+10, 0+15) are
plotted
current p -7
next p, x, y p=-7+6=-1, x=1, y=5
points plotted (1+10, 5+15), (5+10, 1+15),
(1+10, -5+15), (5+10, -1+15),
(-1+10, -5+15), (-5+10, -1+15),
(-1+10, 5+15), (-5+10, 1+15)
x<=y? true
Bresenham’s circle drawing
current p -1
next p, x, y p=-1+4+6=9, x=2, y=5
points plotted (2+10, 5+15), (5+10, 2+15),
(2+10, -5+15), (5+10, -2+15),
(-2+10, -5+15), (-5+10, -2+15),
(-2+10, 5+15), (-5+10, 2+15)
x<=y? True
current p 9
next p, x, y p=9+8-20+10=7, x=3, y=4
points plotted (3+10, 4+15), (4+10, 3+15),
(3+10, -4+15), (4+10, -3+15),
(-3+10, -4+15), (-4+10, -3+15),
(-3+10, 4+15), (-4+10, 3+15)
x<=y? true
Bresenham’s circle drawing
current p 7
next p, x, y p=7+12-16+10=13, x=4, y=3
points plotted (4+10, 3+15), (3+10, 4+15),
(4+10, -3+15), (3+10, -4+15),
(-4+10, -3+15), (-3+10, -4+15),
(-4+10, 3+15), (-3+10, 4+15)
x<=y? No
All pixels close to circle are plotted.
Midpoint circle drawing
Circle centre(10, 15) and radius 5
If p >= 0 then x=x+1, y=y-1, p= p+2x-2y+1
Else x=x+1, p=p+2x+1
p=1-r=-4, x=0, y=5
points (0+10, 5+15), (0+10, -5+15), (5+10, 0+15) and (-5+10, 0+15) are
plotted
current p -4
next x, y, p x=1, y= 5, p=-4+2+1=-1
points plotted (1+10, 5+15), (5+10, 1+15),
(1+10, -5+15), (5+10, -1+15),
(-1+10, -5+15), (-5+10, -1+15),
(-1+10, 5+15), (-5+10, 1+15)
x<=y? true
Midpoint circle drawing
current p -1
Next x, y, p x=2, y=5, p=-1+ 4+1=4
points plotted (2+10, 5+15), (5+10, 2+15),
(2+10, -5+15), (5+10, -2+15),
(-2+10, -5+15), (-5+10, -2+15),
(-2+10, 5+15), (-5+10, 2+15)
x<=y? True
current p 4
next x, y, p x=3, y=4, p=4+6-8+1=3
points plotted (3+10, 4+15), (4+10, 3+15),
(3+10, -4+15), (4+10, -3+15),
(-3+10, -4+15), (-4+10, -3+15),
(-3+10, 4+15), (-4+10, 3+15)
x<=y? true
Midpoint circle drawing
current p 4
next x, y, p x=4, y=3, p=4+8-6+1=7
points plotted (4+10, 3+15), (3+10, 4+15),
(4+10, -3+15), (3+10, -4+15),
(-4+10, -3+15), (-3+10, -4+15),
(-4+10, 3+15), (-3+10, 4+15)
x<=y? No
All pixels close to circle are plotted.
Note that both algorithms plot the same set of points in the same
order
Ellipse
Ellipse with centre origin axes parallel to co- ordinate axes
with half major and minor axes as a, b is x 2/a2+y2/b2=1
dy/dx=-1
dy/dx=-1
(x,y)
translation
translation
midptell (h,k,a,b,c)
x=0, y=b, p= b2 +(1/4)a2 -a2b
setpixel (x, y, c), setpixel (x, -y, c)
while (b2x/a2y <1)
x=x+1
if p>=0 then y=y-1, p=p+2b2x-2a2y+b2
else p=p+2b2x+ b2 , endif
setpixel(x+h, y+k, c), setpixel(-x+h, y+k, c)
setpixel(-x+h, -y+k, c), setpixel(x+h, -y+k, c)
end while
q= b2(x + 1/2)2+a2(y -1)2 -a2b2
for i=y to 1
y=y-1
if q >=0 then q=q-2a2y +a2
else x=x+1, q=q+2b2x-2a2y+a2 , endif
setpixel(x+h, y+k, c), setpixel(-x+h, y+k, c)
Drawing ellipse using midpoint ellipse
Suppose centre is (10, 5), a= 4, b=3
Plot (0+10, 4+5) and (0+10, -4+5)
p= 9+(1/4)16 – 16*3=-35
b2x/a2y = 0 <1
x=1, current p=-35, p=-35+2*9*1+9=-8
Points plotted are (1+10, 4+5), (-1+10, 4+5), (-1+10, -4+5), (1+10,
-4+5)
b2x/a2y = 9/64 <1
x=2, current p=-8, p=-8+2*9*2+9=37
Points plotted are (2+10, 4+5), (-2+10, 4+5), (-2+10, -4+5), (2+10,
-4+5)
b2x/a2y = 18/64 <1
x=3, current p=37, y=3, p=37+2*9*3-2*16*3=-5
Points plotted are (3+10, 3+5), (-3+10, 3+5), (-3+10, -3+5), (3+10,
-3+5)
Drawing ellipse using midpoint ellipse
b2x/a2y = 27/48 <1
x=4, current p=-5, p=-5+2*9*4+9=76
Points plotted are (4+10, 3+5), (-4+10, 3+5),
(-4+10, -3+5), (4+10, -3+5)
b2x/a2y = 36/48 <1
x=5, current p=76, y=2, p=76+2*9*5-2*16*2+9=111
Points plotted are (5+10, 2+5), (-5+10, 2+5),
(-5+10, -2+5), (5+10, -2+5)
b2x/a2y = 45/32 is not <1
q= 9*(5.5*5.5)+16(1)-16*9=144.25
Drawing ellipse using midpoint ellipse
i=2
y=1
Current q=144.25 > 0, q=144.25-2*16+16=128.25
Points plotted are ( 5+10, 1+5), (-5+10, 1+5), (-5+10,
-1+5), (5+10, -1+5)
i=1
y=0
Current q=128.5 > 0, q=128.25-2*16*0+16=144.25
Points plotted are ( 5+10, 0+5), (-5+10, 0+5), (-5+10,
-0+5), (5+10, -0+5)
Other curves
Curve functions are useful for
Modeling objects, setting paths for animation, data graphing etc
Common curves are
Conics, polynomials, trigonometric functions, probability distributions, spline
functions etc
Methods to draw curves
Use equations, find efficient methods such as the ones for circles and ellipse,
approximate using straight line segments-intermediary points are needed
and can be found using parametric equations, etc
Second degree conic sections are described by
F(x, y)= Ax2+Bxy+Cy2+Dx+Ey+F=0
We get ellipse, parabola and hyperbolas if B2-4AC (discriminant) is <, =, > 0
The equation becomes a circle when A=C, B=0, D=-2x1, E=-2y1, F=x12+y12-r2
The equation becomes a line when A=B=C=0
Filled area primitives
User may want to fill a closed region
Three algorithms discussed
Boundary fill, Flood fill, Scan line fill
Boundary and flood fill take a seed point
inside the region to be painted
Boundary fill requires a uniform color
boundary such as not
Boundary fill
bdryfill(x, y, fc, bc)
cc=getpixel(x, y)
if cc fc and cc bc
setpixel (x, y, fc)
bdryfill(x+1, y, fc, bc)
bdryfill(x-1, y, fc, bc)
bdry(x, y+1, fc, bc)
bdryfill(x, y-1, fc, bc)
endif
end bdryfill
Recursive calls can be made with eight neighbors of (x, y) instead of 4
neighbors as shown in the algorithm
Simulation of boundary fill
****
**** ****
**** ****
****
****
**** ****
**** ****
****
**** **** ****
The boundary is shown in blue and fill color be red.
Pixels in yellow are to filled with red and the seed is shown in magenta
Suppose that the top left pixel is (10, 10) then bottom right pixel is (13,
14)
Seed (12,11) is neither fc nor bc so it is colored with fc=red
The function is called with (13, 11), (11, 11), (12, 12), (12, 10)
Stack of calls (13, 11), (11, 11), (12, 12), (12, 10)
The top of stack is call to function with (12, 10). This is bc. No more
function calls are added to stack
Stack of calls (13, 11), (11, 11), (12, 12)
Function is called with (12, 12). This is neither fc nor bc. So colored red.
Function calls with (13, 12), (11, 12), (12, 13), (12, 11) generated and
added to stack
Simulation of boundary fill
****
**** ****
**** ****
****
****
**** ****
**** ****
****
**** **** ****
Stack of calls (13, 11), (11, 11), (13, 12), (11, 12), (12, 13), (12, 11)
Function called with (12, 11). This is cored with fc. No more calls to
function.
Stack of calls (13, 11), (11, 11), (13, 12), (11, 12), (12, 13)
Function called with (12, 13). This is neither bc nor fc. So painted red
and more calls added to stack.
Stack has (13, 11), (11, 11), (13, 12), (11, 12), (13, 13), (11, 13), (12,
14), (12, 11)
(12, 11) is fc, (12, 14) is bc. (11, 13) is neither fc nor bc so painted
red and the stack has
(13, 11), (11, 11), (13, 12), (11, 12), (13, 13), (12, 13), (10, 13), (11,
14), (11, 12)
****
****
Simulation
****
****
****
****
of boundary fill
**** **** ****
****
**** ****
**** ****
****
Function called with (11, 12) and this pixel is colored and
more calls added
Stack has (13, 11), (11, 11), (13, 12), (11, 12), (13, 13),
(12, 13), (10, 13), (11, 14), (12, 12), (10, 12), (11, 13),
(11, 11)
(11, 11) is painted red and more calls are added. By this
time all pixels in the stack are either fc or bc. So all calls
from stack removed in order and found to be of fc or bc
and stack becomes empty and function terminates
Flood fill
floodfill(x, y, fc, oc)
cc=getpixel (x, y)
if cc = oc
setpixel (x, y, fc)
floodfill(x+1, y, fc, oc)
floodfill(x-1, y, fc, oc)
floodfill (x, y+1, fc, oc)
floodfill (x, y-1, fc, oc)
endif
end floodfill
oc is the background color and fc is the fill color
Scan line fill
Suitable for filling polygons (boundaries are
lines)
Principle is to select all scan lines (horizontal
lines) and find points of intersections with
polygon and identify which part of the line is
to be painted
X=0 X=XMAX
Scan line fill
SOME CONCAVE POLYGONS