Flood Fill
The Flood Fill algorithm is used primarily in computer graphics to determine and change the colour
of an area bounded by a particular colour. It is used in tools like the paint bucket in digital drawing
programs, but it can also be applied in game development or graph traversal.
There are several ways to implement the flood fill algorithm, but the most common ones are:
Depth-First Search (DFS)
Breadth-First Search (BFS)
Flood fill is very similar to these graph traversal methods because it essentially "explores" a grid of
pixels (or cells in a matrix).
Key Concepts
1. Grid/Matrix Representation: A 2D array (matrix) is used to represent the pixels or regions.
2. Base Condition: The algorithm stops when it encounters a boundary or a pixel that does not
match the original target colour.
3. Recursive or Iterative: Can be implemented either recursively (DFS) or iteratively (BFS).
Steps for Flood Fill:
1. Start at a pixel (node) and check if it has the target colour.
2. If yes, change it to the new colour.
3. Move to the neighbouring pixels (up, down, left, right) and repeat the process.
4. Continue until all the connected pixels with the target colour are changed.
Disadvantage:
Very slow algorithm
May be fail for large polygons
Initial pixel required more knowledge about surrounding pixels.
Example:
#include <graphics.h>
#include <conio.h>
#include <dos.h>
// Flood Fill function using recursion
void floodFill(int x, int y, int fill_color, int boundary_color) {
// Get the color of the current pixel
int current_color = getpixel(x, y);
// Check if the current pixel is not the boundary and not already filled
if (current_color != boundary_color && current_color != fill_color) {
// Set the pixel to the fill color
putpixel(x, y, fill_color);
// Recursively call floodFill for neighboring pixels (up, down, left, right)
floodFill(x + 1, y, fill_color, boundary_color); // Right
floodFill(x - 1, y, fill_color, boundary_color); // Left
floodFill(x, y + 1, fill_color, boundary_color); // Down
floodFill(x, y - 1, fill_color, boundary_color); // Up
int main() {
// Initialize graphics mode
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
// Set the boundary color and fill color
int boundary_color = WHITE;
int fill_color = YELLOW;
// Draw a rectangle to fill (boundary of rectangle is WHITE)
rectangle(100, 100, 200, 200);
// Fill the inside of the rectangle using flood fill
floodFill(150, 150, fill_color, boundary_color);
// Hold the screen to see the output
getch();
// Close the graphics mode
closegraph();
return 0;
}
Boundary Filling
Boundary Fill Algorithm starts at a pixel inside the polygon to be filled and paints the interior
proceeding outwards towards the boundary. This algorithm works only if the colour with which the
region has to be filled and the colour of the boundary of the region are different. If the boundary is
of one single colour, this approach proceeds outwards pixel by pixel until it hits the boundary of the
region.
Boundary Fill Algorithm is recursive in nature. It takes an interior point (x, y), a fill colour, and a
boundary colour as the input. The algorithm starts by checking the colour of (x, y). If it’s colour is not
equal to the fill colour and the boundary colour, then it is painted with the fill colour and the
function is called for all the neighbours of (x, y). If a point is found to be of fill colour or of boundary
colour, the function does not call its neighbours and returns. This process continues until all points
up to the boundary colour for the region have been tested.
The boundary fill algorithm can be implemented by 4-connected pixels or 8-connected pixels.
4-connected pixels: After painting a pixel, the function is called for four neighbouring points. These
are the pixel positions that are right, left, above, and below the current pixel. Areas filled by this
method are called 4-connected.
Four connected approaches is more suitable than the eight connected approaches.
1. Four connected approaches: In this approach, left, right, above, below pixels are tested.
2. Eight connected approaches: In this approach, left, right, above, below and four diagonals are
selected.
Boundary can be checked by seeing pixels from left and right first. Then pixels are checked by seeing
pixels from top to bottom. The algorithm takes time and memory because some recursive calls are
needed.
Algorithm Steps:
1. Start from a given point (usually inside the boundary).
2. If the current pixel is neither the boundary color nor the fill color, change it
to the fill color.
3. Recursively apply the fill to the neighboring pixels (up, down, left, right,
and optionally diagonals).
4. The algorithm continues until all reachable pixels inside the boundary are
filled.
Example Program:
#include <graphics.h>
#include <conio.h>
// Boundary Fill Algorithm using recursion
void boundaryFill(int x, int y, int fill_color, int boundary_color) {
// Get the current color of the pixel
int current_color = getpixel(x, y);
// If the pixel is not the boundary color and not already filled
if (current_color != boundary_color && current_color != fill_color) {
// Set the pixel to the fill color
putpixel(x, y, fill_color);
// Recursively fill the neighboring pixels (up, down, left, right)
boundaryFill(x + 1, y, fill_color, boundary_color); // Right
boundaryFill(x - 1, y, fill_color, boundary_color); // Left
boundaryFill(x, y + 1, fill_color, boundary_color); // Down
boundaryFill(x, y - 1, fill_color, boundary_color); // Up
int main() {
// Initialize the graphics mode
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
// Set the boundary color and fill color
int boundary_color = WHITE;
int fill_color = RED;
// Draw a closed boundary shape (e.g., a rectangle)
rectangle(100, 100, 300, 300); // Rectangle with top-left (100, 100) and bottom-right (300, 300)
// Set the boundary color for the rectangle
setcolor(WHITE);
// Call the boundary fill function, starting from a point inside the rectangle
boundaryFill(150, 150, fill_color, boundary_color);
// Hold the screen to display the output
getch();
// Close the graphics mode
closegraph();
return 0;