Fill Algorithm
Fill Algorithm
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:
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
2. Base Condition: The algorithm stops when it encounters a boundary or a pixel that does not
match the original target 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:
#include <graphics.h>
#include <conio.h>
#include <dos.h>
// Check if the current pixel is not the boundary and not already filled
// Recursively call floodFill for neighboring pixels (up, down, left, right)
int main() {
getch();
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:
Example Program:
#include <graphics.h>
#include <conio.h>
// If the pixel is not the boundary color and not already filled
putpixel(x, y, fill_color);
int main() {
setcolor(WHITE);
// Call the boundary fill function, starting from a point inside the rectangle
getch();
closegraph();
return 0;