Table of contents
1.
Introduction
2.
Boundary Fill Algorithm is Recursive in Nature
3.
8-Connected Pixels in Boundary Fill Algorithm
4.
4-Connected Pixels vs 8-Connected Pixels
5.
Flood Fill vs. Boundary Fill
6.
Frequently Asked Questions
6.1.
What happens if the boundary color changes within the region in Boundary Fill?
6.2.
Can Flood Fill be used for textures or patterns instead of solid colors?
6.3.
What is the main advantage of using 8-connected over 4-connected in Boundary Fill?
7.
Conclusion
Last Updated: May 7, 2024
Easy

Boundary Fill Algorithm

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The boundary fill algorithm is a method used in computer graphics to fill a closed region with a specific color. It works by starting at a point inside the region and then recursively filling all connected points that have the same color as the boundary. This algorithm is commonly used in various graphics applications, such as digital painting programs and image editing software. 

Boundary Fill Algorithm

In this article, we'll learn about the boundary fill algorithm in detail, including its recursive nature, implementation, and the differences between 4-connected and 8-connected pixels. We'll also compare the boundary fill algorithm with the flood fill algorithm.

Boundary Fill Algorithm is Recursive in Nature

The Boundary Fill Algorithm uses recursion, a programming technique where a function calls itself to solve smaller instances of the same problem. This makes the algorithm efficient for filling enclosed areas in graphics because it can repeatedly apply the same filling process to adjacent pixels until it reaches the boundary of the area.

Recursion starts at a point inside the region & colors it if it's not already part of the boundary or filled. Then, the algorithm recursively calls itself for neighboring pixels: north, south, east, & west. This step is repeated until all pixels within the boundary are filled, ensuring no spots are missed.

Using recursion allows the algorithm to navigate complex shapes easily since it can adapt to any irregular boundaries without needing complex loops or conditional statements. It simplifies the code but requires careful handling to avoid excessive memory use, which can lead to a stack overflow if the area to be filled is too large.

To manage this, developers must ensure that the recursion depth is not too deep by limiting the size of the area or optimizing the recursion steps. For example, the algorithm can be modified to check if a pixel is already filled before proceeding, which reduces unnecessary function calls.

Here's a basic example of how the Boundary Fill Algorithm can be implemented using recursion in Python:

def boundary_fill(x, y, fill_color, boundary_color, screen):
    if screen[x][y] != boundary_color and screen[x][y] != fill_color:
        screen[x][y] = fill_color
        boundary_fill(x + 1, y, fill_color, boundary_color, screen)
        boundary_fill(x - 1, y, fill_color, boundary_color, screen)
        boundary_fill(x, y + 1, fill_color, boundary_color, screen)
        boundary_fill(x, y - 1, fill_color, boundary_color, screen)
# Example to use the function assuming screen is a 2D pixel array
# screen dimensions and colors must be defined earlier


This code snippet shows how the algorithm works step-by-step. The function takes a position and the color to fill, then fills the pixel if it’s not the boundary color or already filled, and recurses into adjacent pixels.

This recursive nature allows for efficiently filling connected areas in graphics, making it ideal for various applications in computer graphics design and development.

8-Connected Pixels in Boundary Fill Algorithm

In computer graphics, when we use the Boundary Fill Algorithm, deciding which pixels are considered neighbors is crucial. The concept of 8-connected pixels allows the algorithm to connect and fill more comprehensively across the graphics canvas. In 8-connected pixel consideration, a pixel has eight neighbors—up, down, left, right, and the four diagonals. This broader approach ensures that the fill is more uniform, even across diagonal gaps that might otherwise be skipped in simpler methods.

Here’s how it looks conceptually:

  • Central Pixel: This is the pixel currently being filled.
     
  • Neighbors: Includes all adjacent pixels, not just those directly to the north, south, east, and west, but also those at northeast, northwest, southeast, and southwest positions.

This method is especially useful for complex shapes where diagonal lines or edges are common. It helps in filling those small gaps that a 4-connected approach might leave out, giving a smoother and more visually cohesive fill.

To implement an 8-connected Boundary Fill Algorithm, the recursion adjusts to include checks in eight directions. Here's a simple Python implementation to demonstrate this:

def boundary_fill_eight(x, y, fill_color, boundary_color, screen):
    if screen[x][y] != boundary_color and screen[x][y] != fill_color:
        screen[x][y] = fill_color
        # Recurse for all 8 directions
        boundary_fill_eight(x + 1, y, fill_color, boundary_color, screen)
        boundary_fill_eight(x - 1, y, fill_color, boundary_color, screen)
        boundary_fill_eight(x, y + 1, fill_color, boundary_color, screen)
        boundary_fill_eight(x, y - 1, fill_color, boundary_color, screen)
        boundary_fill_eight(x + 1, y + 1, fill_color, boundary_color, screen)
        boundary_fill_eight(x - 1, y - 1, fill_color, boundary_color, screen)
        boundary_fill_eight(x + 1, y - 1, fill_color, boundary_color, screen)
        boundary_fill_eight(x - 1, y + 1, fill_color, boundary_color, screen)
# Usage example assuming screen is predefined


In this implementation, the function checks & fills every neighboring pixel around the current one, ensuring that no adjacent pixel is left unfilled if it’s not part of the boundary. This makes the fill more complete & thorough, covering more space efficiently & effectively.

4-Connected Pixels vs 8-Connected Pixels

Feature 4-Connected 8-Connected
Neighbor Count 4 neighbors 8 neighbors
Directions Considered North, South, East, West Includes diagonals: NE, NW, SE, SW
Filling Thoroughness Less thorough; may leave gaps in diagonal gaps More thorough; fills diagonal gaps
Computational Load Lower; fewer checks per pixel Higher; more checks per pixel
Memory Usage Generally lower due to fewer recursive calls Higher due to more recursive calls
Ideal Use Best for simpler, orthogonal shapes Suitable for complex shapes with diagonal elements
Risk of Overfill Lower; strict boundary adherence Higher; can fill across thin boundaries
Visual Output Can appear blocky or pixelated in diagonals Smoother and more consistent filling

This table explains the fundamental differences between 4-connected and 8-connected pixels in terms of their implementation in the Boundary Fill Algorithm. The choice between them hinges on the specific requirements of the application, particularly in how graphics are intended to appear and perform. The 4-connected approach might be preferable for simpler applications where performance and memory usage are critical, whereas the 8-connected method might be better for complex graphics where a smooth and comprehensive fill is necessary.

Flood Fill vs. Boundary Fill

Feature Flood Fill Boundary Fill
Basic Principle Fills an area until it reaches pixels of a color different from the one at the start point. Fills an area up to boundaries defined by a specific color.
Algorithm Type Non-recursive and recursive versions available. Typically implemented recursively.
Use Case Used when the boundary is of varying colors or when the boundary color is not defined. Used when a clear boundary color defines the area to be filled.
Color Sensitivity Starts filling until a different color is encountered. Does not require a specific boundary color. Requires a specific color to stop filling, sensitive to the boundary color.
Efficiency Can be more efficient in non-recursive implementations using a queue. Recursive nature can lead to high stack usage if not managed properly.
Memory Usage Queue-based implementations use more memory but manage stack overflow risk. Recursive calls increase memory use in the stack, risking overflow in large areas.
Applications Suitable for areas where boundaries are less defined or vary in color. Preferred for well-defined, uniformly colored boundaries.
Implementation Complexity Simpler in scenarios without a clear boundary color; requires managing a queue in non-recursive forms. More complex due to the need for recursion and handling of boundary color checks.

Flood Fill is generally more versatile and can handle more varied graphic scenarios, whereas Boundary Fill is more specialized, requiring a clear boundary color to function effectively.

Frequently Asked Questions

What happens if the boundary color changes within the region in Boundary Fill?

If the boundary color is not consistent within the region you are trying to fill, the Boundary Fill algorithm might not correctly stop at the boundary. It could either stop prematurely or exceed the intended boundary area.

Can Flood Fill be used for textures or patterns instead of solid colors?

Yes, Flood Fill can be adapted to fill areas with textures or patterns by modifying the filling condition to apply a texture or pattern instead of a single color, making it versatile for various graphic design applications.

What is the main advantage of using 8-connected over 4-connected in Boundary Fill?

The main advantage of using an 8-connected approach is its ability to fill more completely, especially around diagonal lines where 4-connected might leave gaps. This ensures a smoother and more visually appealing fill in complex shapes.

Conclusion

In this article, we have learned about the Boundary Fill Algorithm, its recursive nature, and the differences between 4-connected and 8-connected pixel methods. We also compared Boundary Fill with Flood Fill to highlight their specific uses and efficiencies. Its very useful to learn these algorithms and their applications because it allows the developers and graphic designers to choose the most appropriate method for their projects, ensuring optimal performance and results in their graphics rendering tasks.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass