Flood Fill Algorithm
Sometimes we want to fill in (or recolor) an area that is not defined within a single color boundary. Here is a figure describing the situation.
We can paint such areas by replacing a specified interior color instead of searching for a boundary color value. This approach is called a flood-fill algorithm. We can use either a 4-connected or 8-connected approach,
Follow The Instructions To Successfully Run The Program In Dev-Cpp/CodeBlock: Whenever you #include <graphics.h> in a program, you must instruct the linker to link in certain libraries. The command to do so from Dev-C++ is Alt-P. Choose the Parameters tab from the pop-up window and type the following into the Linker area: -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
Code
#include <stdio.h>
#include <graphics.h>
/* oc = old color and fc = fill color */
void floodfill(int x, int y, int fc, int oc)
{
int c = getpixel(x, y);
if (c == oc) {
putpixel(x, y, fc);
floodfill(x, y + 1, fc, oc);
floodfill(x, y - 1, fc, oc);
floodfill(x - 1, y, fc, oc);
floodfill(x + 1, y, fc, oc);
}
}
int main()
{
int arr[] = {10, 10, 100,10, 10, 100, 10, 10};
initwindow(300,300,"FloodFill");
drawpoly(4, arr);
delay(3000);
floodfill(40, 40, 4, 0);
while(!kbhit());
return 0;
}
