Filled Area Primitives
Filled Area Primitives
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.
1. Flood-fill Algorithm
2. Boundary-fill Algorithm
Region Filling is the process of filling a region or an image. Further depending upon
filling, it can be categorized as:
1. Boundary Filling Algorithm - region inside the boundary of same pixel value
1. There are two defined colors: color of boundary (color_boundary) and color
that needs to be filled (color_fill)
4.2. Do the same process for 4 adjacent pixel points: (x, y-1), (x+1, y), (x,
y+1), (x-1, y)
4.3. If 8-connected fill is being done, do the same process for 4 diagonal pixel
points additionally: (x+1, y-1), (x+1, y+1), (x-1, y+1), (x-1, y-1).
5. Do the process for every pixel point.
Restrictions
1. Starting point should be inside closed polygon.
3. It may fail to fill in case some interior pixels are already filled with color.
The flood fill algorithm has many characters similar to boundary fill. But this method is more
suitable for filling multiple colors boundary. When boundary is of many colors and interior is
to be filled with one color we use this algorithm.
In fill algorithm, we start from a specified interior point (x, y) and reassign all pixel values are
currently set to a given interior color with the desired color. Using either a 4-connected or 8-
connected approaches, we then step through pixel positions until all interior points have been
repainted.
BFS, on the other hand, uses a queue to keep track of the visited pixels
or cells. When a pixel or cell is visited, its neighboring pixels or cells
are added to the queue, and they are visited in a breadth-first manner
until the entire area is filled.
Algorithm of Flood-fill
Procedure flood_fill (p, q, fill_color, Old_color: Integer)
Var
Current: Integer
Current = getpixel (p, q)
If
(Current = Old_color)
Then
Start
setpixel (p, q, fill_color);
flood_fill (p, q+1, fill_color, Old_color);
flood_fill (p, q-1, fill_color, Old_color);
flood_fill (p+1, q, fill_color, Old_color);
flood_fill (p-1, q, fill_color, Old_color);
End;
Note- Getpixel () defines the color of specified pixel.
Setpixel () set the pixel with the specified color.