Flood-Fill Algorithm

Flood-fill algorithm is used for region filling when the region is interior-defined i.e. when all the pixels in the region have same colour(old_col).This algorithm starts with a seed pixel inside the region which is the starting point for the recursive algorithm. The algorithm checks to see if colour of pixel is same as original colour of the region.If answer is yes,pixel is filled with fill_col and a recursive call is made using each neighbouring pixel as a new seed.Neighbours can be 4-connected(horizontal and vertical neighbours) or 8 connected (horizontal,vertical and diagonal neighbours).

/*Header file for built-in graphics functions such as plot() and initgraph()*/

void flood(int,int,int,int);

int main()
    int gm,gd=DETECT,x,y,fill_col;
    initgraph(&gd,&gm,"c:\\tc\\bgi");/*loads the graphic driver and puts system into graphics mode*/

    syntax for initgraph() is:
    void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);
    *graphdriver is an integer that specifies the graphics driver to be used.DETECT requests auto-detect for the driver.
    *graphmode is an integer that specifies the initial graphics mode.If *graphdriver equals DETECT,*graphmode is set by initgraph to the highest resolution available for the detected driver.

    printf("\n\tFLOOD FILLING ALGORITHM\n\n");
    /*rectangle(x1,y1,x2,y2) draws rectangle on screen with (x1,y1) and (x2,y2) specifying coordinates of top-left and bottom-right vertex respectively.*/
    printf("Enter coordinates of seed point\n");
    scanf("%d %d",&x,&y);
    printf("Enter fill colour\n");
    printf("The object after filling is:\n");
    flood(x,y,fill_col,getpixel(175,185));/*getpixel(x,y) returns the colour of pixel at (x,y)*/
    /*closegraph function closes the graphics mode, deallocates all memory allocated by graphics system and restores the screen to the original mode*/
    return 0;

//Flood Filling Function
void flood(int x,int y,int fill_col,int old_col)
    (x,y) specify coordinates of seed point.
    fill_col specifies the colour to be filled in closed region.
    old_col specifies the original colour of region. 

       /*This function plots a pixel at coordinates(x,y) specified by first 2 arguments and third argument specifies the color of the pixel*/
    return ;


You can run this code in Turbo C++. Dev C++ and Microsoft visual studio do not support graphics libraries.
You can download Turbo C++ using this link Turbo C++

Other articles related to Computer Graphics available on TutsHeap are:
Cohen Sutherland Line Clipping Algorithm
Boundary-Fill Algorithm
Mid-Point Ellipse Drawing Algorithm
Mid-Point Circle Drawing Algorithm
DDA Line Drawing Algorithm
Bresenham’s Line Drawing Algorithm

This content has been helpful to you?

Thanks for contributing!

Yes No