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

Filled Area Primitives

Dsa notes

Uploaded by

markshaikh50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Filled Area Primitives

Dsa notes

Uploaded by

markshaikh50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Filled Area Primitives: 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.

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

2. Flood Filling Algorithm - region having same pixel value

Fig.1 Flood Fill Algorithm

Fig.2 Boundary Fill Algorithm


Introduction to Boundary Fill Algorithm
In computer graphics, Boundary Fill algorithm is used to fill a inside of closed
polygon having boundary of same color with a desired color. It is mainly used with
interactive-painting packages where an inside point can be easily chosen as its
approach requires a starting pixel also called seed, to start with.

Details on Implementation and Problem statement


It is mainly implemented using stack-based recursion. Functioning of the boundary-
fill requires an interior point (x,y) , fill color and boundary color to start with. The
algorithm starts by checking whether a pixel value equals to boundary color or fill
color . If not, pixel is filled with desired color and advances to check for neighboring
pixels. Else, not. The process continues until it hits all sides of the boundary region.

Further, Boundary-fill algorithm can be implemented using 4-connected pixels and


8-connected pixels.
4-connected pixels : Neighboring pixels are pixels on left,right,above and below of
current pixel.
8-connected pixels : Neighboring pixels are left,right ,above ,below and 4 diagonal
pixels as shown below.

Fig.3 Neighboring Pixels


4-connected vs 8-connected
In case of sharp boundaries, 4-connected method fails, while 8-connected method
filled the region efficiently. Following figure represents the situation.

Boundary Fill Algorithm


Steps in Boundary Fill Algorithm:

1. There are two defined colors: color of boundary (color_boundary) and color
that needs to be filled (color_fill)

2. Get color (say color1) of the current pixel

3. If color1 is equal to color_boundary or color_fill, nothing needs to be done as


correct color is already assigned.

4. If color1 is not equal to the two values:


4.1. Add color_fill to the current pixel.

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.

2. For all edges of a polygon, boundary color should be same.

3. It may fail to fill in case some interior pixels are already filled with color.

Flood Fill Algorithm:


In this method, a point or seed which is inside region is selected. This point is called a seed
point. Then four connected approaches or eight connected approaches is used to fill with
specified 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.

Floodfill algorithm is a technique used to fill a connected area in an


image or a matrix with a particular color or pattern. It starts from a
given point and traverses the adjacent pixels or cells, coloring them
as it goes, until it reaches the boundary of the area or encounters a
barrier that prevents further filling.

Is Floodfill a BFS or DFS Algorithm?

Floodfill algorithm can be implemented using both BFS (Breadth-


First Search) and DFS (Depth-First Search) approaches.

DFS is implemented recursively, and it uses a stack to keep track of


the visited pixels or cells. When a pixel or cell is visited, its
neighboring pixels or cells are recursively visited in a depth-first
manner until the entire area is filled.

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.

Advantages of Flood-fill algorithm


• It provides an easy way to fill color in graphics.
• The Flood-fill algorithm colors the whole area through interconnected pixels by
a single color.
• The algorithm fills the same color inside the boundary.

Disadvantages of Flood-fill Algorithm

• It is a more time-consuming algorithm.


• Sometimes it does not work on large polygons.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy