Open In App

Flood fill Algorithm – how to implement fill() in paint?

Last Updated : 05 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Flood Fill is a classic algorithm used to change the color of an area in a 2D image where all pixels are connected and have the same initial color. Think of it like the paint bucket tool in graphic design software like MS Paint or Photoshop—when you click on a spot, it automatically fills that area with a new color, but only if the neighboring pixels are of the same original color.

The key is that only pixels that are directly connected and share the same color are updated. Anything blocked by a different color won’t be changed, just like how a wall or boundary works in drawing software.

In Paint or Photoshop, when you click the paint bucket at a pixel:

  • It looks at the color of that pixel.
  • Then it spreads in all four directions (up, down, left, right) to all connected pixels of the same color.
  • It does not fill diagonally.
  • It stops filling when it hits a pixel of different color or the edge of the image.


For the implementation of the Flood Fill algorithm, please refer here.

Using BFS for Flood Fill, we start from the clicked pixel, use a queue to visit all 4-directionally connected pixels of the same color, and change them to the new color, stopping at different colors or edges—just like a paint bucket tool..


Next Article

Similar Reads