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.
- Region filling technique
- Used after boundary definition
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.
- Uses recursion or stack
- Stops at boundary or different color
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.
- Seed point based filling
- Pixel connectivity important
4. Types of Connectivity
- 4-connected: Up, Down, Left, Right
- 8-connected: Includes diagonals
5. Steps of Flood Fill Algorithm
- Select a seed point (x, y)
- Check target color at seed
- Replace with fill color
- Recursively process neighbors
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
- Simple implementation
- Effective for closed regions
- Widely used in graphics software
8. Disadvantages
- High memory usage (recursion)
- Slow for large regions
- Stack overflow risk
9. Applications
- Paint and drawing programs
- Region coloring
- Image editing tools
Practice Questions
- What is flood fill algorithm?
- Explain 4-connected and 8-connected fill.
- Write steps of flood fill algorithm.
- List advantages and disadvantages.
- Where is flood fill used?
Practice Task
Explain with diagram:
✔ Flood fill working
✔ 4-connected vs 8-connected fill
✔ Seed point filling