Flood fill Algorithm how to implement fill() in paint? (original) (raw)

Last Updated : 23 Jul, 2025

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.

Try It Yourselfredirect icon

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

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..