Area Filling
Area Filling
Area filling is a method or process that helps us to fill an object, area, or image. We can easily fill the
polygon. The polygon filling is defined as filling or highlighting all the pixels. The pixels appear inside
the polygon shape with any color other than the background color.
There are two algorithms or methods used to fill the polygon.
Seed Fill Algorithm
Scan Line Algorithm
Seed Fill Algorithm
In this method, we will select a seed or starting point inside the boundary. We can further
divide the seed fill into two parts.
Boundary-fill Algorithm
Flood-fill Algorithm
Boundary-fill Algorithm
It is also known as the “Edge-fill algorithm.” A boundary-fill procedure accepts as input the
coordinates of an interior point (x, y), a fill color, and a boundary color. Starting from (x, y), the
procedure tests neighbouring positions to determine whether they are of the boundary color. If not, they
are painted with the fill color, and their neighbours are tested. This process continues until all pixels up
to the boundary color for the area have been tested.
The boundary fill algorithm is used for area filling. If the object has a particular boundary in a single
color, then the algorithm travels each pixel until it reaches the boundary.
In the Boundary-fill algorithm, we use the 4-connected and 8-connected methods. By the use of a 4-
connected or 8-connected method, we can set the new position of the pixels until all the interior points
have been filled.
Algorithm of Boundary-fill: 4-connected Approach
void boundaryFill4 (int x, int y, int fill, int boundary)
{
int current;
current = getpixel (x, y);
if ((current != boundary) && (current != fill))
{
setcolor (fill);
setpixel (x, y);
boundaryfill4 (x+l, y, fill, boundary);
boundaryFill4 (x-1, y, fill, boundary) :
boundaryFill4 (x, y+l, fill, boundary);
boundaryFill4 (x, y-1, fill, boundary) ;
}
}
Problems with Boundary-fill algorithm
Sometimes it may not fill all the regions.
In the 4-connected method, it does not fill corner pixels. Because it only checks the adjacent
position of the pixel.
An 8 connected boundary-fill algorithm would correctly fill the interior of the area defined in
Fig. above but a 4-connected boundary-fill algorithm produces the partial fill shown.
Flood-fill Algorithm
Sometimes we want to fill in (or recolor) an area that is not defined within a single color boundary.
Figure shows an area bordered by several different color regions.
Flood-fill algorithm helps to define a region in the boundary, attached to a point in the multi-
dimensional array. It is similar to the bucket tool used in the paint program. The stack-based recursive
function is used to implement the algorithm. In flood -fill algorithm, we replace all the associated pixels
of the selected color with a fill color. We also check the pixels for a particular interior color, not for
boundary color.
We start from a specified interior point (x, y) and reassign all pixel values that are currently set to a
given interior color with the desired fill color. If the area we want to paint has more than one interior
color, we can first reassign pixel values so that all interior points have the same color. Using either a 4-
connected or 8-connected approach, we then step through pixel positions until all interior points have
been repainted.
Scan-Line Algorithm
The Scan-Line Algorithm is an area filling algorithm. In this algorithm, we can fill the polygons through
horizontal lines or scan lines. The scan-line intersects the edges of the polygon, and the polygon is filled
between pairs of the intersection. The main purpose of this algorithm is to fill color in the interior pixels
of the polygon.
For each scan line crossing a polygon, the area-fill algorithm locates the intersection points of the scan
line with the polygon edges. These intersection points are then sorted from left to right, and the
corresponding frame-buffer positions between each intersection pair are set to the specified fill color.
In the example of Fig., the four pixel intersection positions with the polygon boundaries define two
stretches of interior pixels from x = 10 to x = 14 and from x = 18 to x = 24.
Some scan-line intersections at polygon vertices requires special handling. A scan line passing through
a vertex intersects two edges at that position, adding two points to the list of intersections for the scan
line. Figure shows two scan lines at positions y and y' that intersect edge endpoints. Scan line y intersects
five polygon edges. Scan line y', however, intersects an even number of edges although it also passes
through a vertex. Intersection points along scan line
y' correctly identify the interior pixel spans. But with scan line y, we need to do some additional
processing to determine the correct interior points. The topological difference between scan line y and
scan line y' in Figure is identified by noting the position of the intersecting edges relative to the scan
line. For scan line y, the two intersecting edges sharing a vertex are on opposite sides of the scan line.
But for scan line y', the two intersecting edges are both above the scan line. Thus, the vertices that
require additional processing are those that have connecting edges on opposite sides of the scan line.
We can identify these vertices by tracing around the polygon boundary either in clockwise or counter
clockwise order and observing the relative changes in vertex y coordinates as we move from one edge
to the next. If the endpoint y values of two consecutive edges monotonically increase or decrease, we
need to count the middle vertex as a single intersection point for any scan line passing through that
vertex. Otherwise, the shared vertex represents a local extremum (minimum or maximum) on the
polygon boundary, and the two edge intersections with the scan line passing through that vertex can be
added to the intersection list.
Another Solution:
One way to resolve the question as to whether we should count a vertex as one intersection or two is to
shorten some polygon edges to split those vertices that should be counted as one intersection. We can
process nonhorizontal edges around the polygon boundary in the order specified, either clockwise or
counter clockwise. As we process each edge, we can check to determine whether that edge and the next
nonhorizontal edge have either monotonically increasing or decreasing endpoint y values. If so, the
lower edge can be shortened to ensure that only one intersection point is generated for the scan line
going through the common vertex joining the two edges. Figure illustrates shortening of an edge. When
the endpoint y coordinates of the two edges are increasing, the y value of the upper endpoint for the
current edge 1s decreased by 1, as in Fig. (a). When the endpoint y values are monotonically decreasing,
as in Fig. (b), we decrease they coordinate of the upper endpoint of the edge following the current edge.