Flood fill Algorithm – how to implement fill() in paint in C++

C++Server Side ProgrammingProgramming

In this problem, we are given a 2d array representing a 2-D screen, the coordinates of a pixel on the screen to be filled with color and the color. Our task is to create a program to Color the current pixel and all the adjacent pixels which have that color.

Coloring in paint, we will select a color and click on the given pixel with a brush.

Let’s take an example to understand the problem

Input: Sceen[][] =
{{W, W, B, W, W, W, W, W},
{W, W, W, W, W, W, B, B},
{W, B, B, W, W, B, W, W},
{W, Y, Y, Y, Y, B, W, B},
{B, W, W, Y, Y, B, W, B},
{B, W, W, Y, Y, Y, Y, B},
{W, B, W, W, W, Y, W, W},
{W, W, B, B, W, Y, Y, W}};
X = 5, Y = 5, newColor = R.
Output:
{{W, W, B, W, W, W, W, W},
{W, W, W, W, W, W, B, B},
{W, B, B, W, W, B, W, W},
{W, R, R, R, R, B, W, B},
{B, W, W, R, R, B, W, B},
{B, W, W, R, R, R, R, B},
{W, B, W, W, W, R, W, W},
{W, W, B, B, W, R, R, W}};

Flood Fill Algorithm

In this algorithm, the pixels will be filled with new color when it is already in selected previous color. If the previous color is not the previous color, that pixel will not be filled. After filling a pixel, it will check for its up, down, left and right pixels to do the same. Learn more here.

Solution Approach

One method to solve the problem is by using a recursive approach. We will find the first pinel which we need to color then check all its 4 neighbouring pixels. Fit of the same color then replace it with new color and repeat with the neighbours of the current pixel. If the neighbouring pixel is of any different color, leave it. Follow these steps untill you get all adjacent pinels with the same colour as starting pixel are colored. Then stop the filling algorithm.

Example

Program to illustrate the working of our solution

#include<iostream>
using namespace std;
#define M 8
#define N 8
void fillColorAdj(char screen[][N], int x, int y, char oldColor, char color){

   if (x < 0 || x >= M || y < 0 || y >= N)
      return;
   if (screen[x][y] != oldColor)
      return;
   if (screen[x][y] == color)
      return;
   screen[x][y] = color;

   fillColorAdj(screen, x+1, y, oldColor, color);
   fillColorAdj(screen, x-1, y, oldColor, color);
   fillColorAdj(screen, x, y+1, oldColor, color);
   fillColorAdj(screen, x, y-1, oldColor, color);
}
void fillColor(char screen[][N], int x, int y, char color){

   char oldColor = screen[x][y];
   if(oldColor==color) return;
   fillColorAdj(screen, x, y, oldColor, color);
}
int main(){

   char screen[M][N] = {{'W', 'W', 'B', 'W', 'W', 'W', 'W', 'W'},
      {'W', 'W', 'W', 'W', 'W', 'W', 'B', 'B'},
      {'W', 'B', 'B', 'W', 'W', 'B', 'W', 'W'},
      {'W', 'Y', 'Y', 'Y', 'Y', 'B', 'W', 'B'},
      {'B', 'W', 'W', 'Y', 'Y', 'B', 'W', 'B'},
      {'B', 'W', 'W', 'Y', 'Y', 'Y', 'Y', 'B'},
      {'W', 'B', 'W', 'W', 'W', 'Y', 'W', 'W'},
      {'W', 'W', 'B', 'B', 'W', 'Y', 'Y', 'W'},};
   int x = 5, y = 5;
   char color = 'R';

   cout<<"The initial screen cordinates are : \n";
   for (int i=0; i<M; i++){

      for (int j=0; j<N; j++)
         cout<<screen[i][j]<<"\t";
      cout<<endl;
   }

   fillColor(screen, x, y, color);
   cout<<"\nThe screen cordinates after coloring are : \n";
   for (int i=0; i<M; i++){

      for (int j=0; j<N; j++)
         cout<<screen[i][j]<<"\t";
      cout<<endl;
   }
}

Output

The initial screen cordinates are :
W   W   B   W   W   W   W   W
W   W   W   W   W   W   B   B
W   B   B   W   W   B   W   W
W   Y   Y   Y   Y   B   W   B
B   W   W   Y   Y   B   W   B
B   W   W   Y   Y   Y   Y   B
W   B   W   W   W   Y   W   W
W   W   B   B   W   Y   Y   W

The screen cordinates after coloring are :
W   W   B   W   W   W   W   W
W   W   W   W   W   W   B   B
W   B   B   W   W   B   W   W
W   R   R   R   R   B   W   B
B   W   W   R   R   B   W   B
B   W   W   R   R   R   R   B
W   B   W   W   W   R   W   W
W   W   B   B   W   R   R   W
raja
Updated on 01-Feb-2022 09:03:01

Advertisements