Week 4 Filled Area Primitives
Week 4 Filled Area Primitives
In case of
odd
number of
intersections
count the
point which
is a vertex
Scan Line Polygon Fill
● To resolve, shorten some polygon
edges to split those vertices that
should be counted as one intersection
● Process non-horizontal edges around the
polygon boundary in the order specified,
clockwise or counter-clockwise
● As we process each edge, check to
determine whether that edge and the
next non-horizontal edge have either
monotonically increasing or decreasing
endpoint y values
●
Scan Line Polygon Fill
out
in
out
in
The scan fill algorithm works as follows
i. Intersect each scanline with all edges
ii. Sort intersections in x
iii. Calculate parity of intersections to determine in/out
iv. Fill the “in” pixels
Special cases to be handled:
v. Horizontal edges should be excluded
vi. For vertices lying on scanlines,
i. count twice for a change in slope.
ii. Shorten edge by one scanline for no change in slope
• Coherence between scanlines tells us that
- Edges that intersect scanline y are likely to intersect y + 1
- x changes predictably from scanline y to y + 1
(Xk , Yk )
Scan Line yk
Non Zero Winding Rule
• The method of the non-zero winding number rule is to
count the number of times edges wind around a point P in
a counter-clockwise direction.
– Initialize the winding number to zero.
– Draw a line from P to the exterior of the polygon without
passing through a vertex.
– Add 1 to the winding number for every edge that crosses the
line from P in one direction (clockwise or counter-clockwise)
and subtract 1 from the winding number for every crossing
edge in the opposite direction.
– If the winding number is not equal to zero, the point P is
within the polygon.
Non Zero Winding Rule
Inside Outside Tests
Boundary Fill Algorithm
● Scan line algorithm for regions with
curved boundaries require more work
because intersection calculations
involve non-linear boundaries
● Another approach to area filling is to
start at a point inside a region and
paint the interior outward toward the
boundary
● If the boundary is specified in a
single colour, the fill algorithm
proceeds outward pixel by pixel
until the boundary colour is
Boundary Fill Algorithm
● It accepts as input the coordinates of
an interior point (x, y), a fill colour,
and a boundary colour
● Starting from (x, y), the procedure
tests neighbouring positions to
determine whether they are of the
boundary colour.
● If not, they are painted with the
fill colour, and their neighbours
are tested.
● This process continues until all pixels
Boundary Fill Algorithm
●2 methods for processing
neighbouring pixels from a
current test position
● four neighbouring points - are
right, left, above, and below –
4 connected
●eight neighbouring points – right,
left, above, below and four diagonal
positions – 8 connected
Algorithm of Boundary-fill
Procedure boundary fill (p, q, fill color, boundary)
Step 1: Initialize the boundary of the region.
Step 2: Get the interior pixel (p, q). Now, define an Integer called current pixel and
assign it to (p, q).
Current_pixel= getpixel (p, q)
Step 3: If
(current_pixel != boundary) and (current_pixel != fill)
Then
Setpixel(p, q, fill);
Boundary fill (p+1, q, fill, boundary);
Boundary fill (p-1, q, fill, boundary);
Boundary fill (p, q+1, fill, boundary);
Boundary fill (p, q-1, fill, boundary);
Step 4: End;
Problems with Boundary-fill algorithm
● Recursive boundary-fill algorithms may not
fill regions correctly if some interior pixels
are already displayed in the fill colour.
● Encountering a pixel with the fill colour can
cause a recursive branch to terminate,
leaving other interior pixels unfilled.
● To avoid this, first change the colour of any
interior pixels that are initially set to the fill
colour before applying the boundary-fill
procedure.
Flood Fill Algorithm
● To fill in an area that is not defined within a
single colour boundary
● Paint areas by replacing a specified interior
colour instead of searching for a particular
boundary colour – flood-fill algorithm
● Start from a specified interior point (x, y) and
reassign all pixel values that are currently set
to a given interior colour with the desired fill
colour.
● If the area that we want to paint has more than
one interior colour, we can first reassign pixel
values so that all interior points have the
same colour.
● Using either a 4-connected or 8-connected
approach, we then step through pixel
positions until all interior points have been
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);
}
}
Flood-fill Algorithm Boundary-fill Algorithm
It can process the image containing more It can only process the image containing
than one boundary colours. single boundary colour.