FLOODFILL ALGORITHM USING OPENGL[C++]

Parag Varshney
1 min readMay 25, 2021

--

Flood fill is an algorithm mainly used to determine a bounded area connected to a given node in a multi-dimensional array. It is a close resemblance to the bucket tool in paint programs.

The most approached implementation of the algorithm is a stack-based recursive function, and that’s what we’re gonna talk about next.

The problem is pretty simple and usually follows these steps:

  1. Take the position of the starting point.
  2. Decide whether you want to go in 4 directions (N, S, W, E) or 8 directions (N, S, W, E, NW, NE, SW, SE).
  3. Choose a replacement color and a target color.
  4. Travel in those directions.
  5. If the tile you land on is a target, replace it with the chosen color.
  6. Repeat 4 and 5 until you’ve been everywhere within the boundaries.

Ok, so theory part is over now come to the implementation part.

Given code is for four connected components if you want it for 8 just add four more lines of diagonal directions i.e.(NW, NE, SW, SE)👻

This code creates a rectangle of using glutDisplayFunc(createrectangle).You can make shape of your choice by changing the code for the drawing shape.Like I am creating a rectangle using draw_line() function. Define function of your choice if you want another shape .

Now to run the code you need to run the following command in your terminal(linux,ubuntu ).It may not work for wsl 😓.

g++ floodfill.cpp -o gl -lGL -lGLU -lglut

--

--