Flood Fill Algorithm

The Flood Fill Algorithm is a region-filling technique used in computer graphics to fill a connected area with a specified color, starting from a given seed point.

1. Introduction

Flood fill is commonly used in paint programs to fill an enclosed area with a selected color. It replaces all pixels of a target color connected to a seed point.

2. Basic Idea

Starting from a seed point, the algorithm checks neighboring pixels and fills them if they match the target color, continuing until the region is filled.

3. Working Principle

The algorithm examines the color of the current pixel. If it matches the target color, it is replaced with the fill color and neighboring pixels are processed.

4. Types of Connectivity

5. Steps of Flood Fill Algorithm

6. Flood Fill Pseudocode

FloodFill(x, y, targetColor, fillColor):
    if color(x, y) != targetColor
        return
    set color(x, y) = fillColor
    FloodFill(x+1, y)
    FloodFill(x-1, y)
    FloodFill(x, y+1)
    FloodFill(x, y-1)

7. Advantages

8. Disadvantages

9. Applications

Practice Questions

  1. What is flood fill algorithm?
  2. Explain 4-connected and 8-connected fill.
  3. Write steps of flood fill algorithm.
  4. List advantages and disadvantages.
  5. Where is flood fill used?

Practice Task

Explain with diagram: ✔ Flood fill working ✔ 4-connected vs 8-connected fill ✔ Seed point filling