Thursday, February 13, 2020

Flood Fill Algorithm Implementation in C-Language

Flood Fill Algorithm Implementation in C-Language

Flood Fill Algorithm fills area bordered with multiple color.
Flood fill algorithm replaces interior color with fill color instead of checking boundary color.
It uses 4-connected or 8- connected approach.


Program:


#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void flood_fill_algo(int,int,int,int);
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setbkcolor(8);
setcolor(3);
rectangle(200,200,250,250);
flood_fill_algo(201,201,0,4);

getch();

}
void flood_fill_algo(int x,int y,int old_c,int new_c)
{
int a=getpixel(x,y);
if(a==old_c)
{
//delay(10);
putpixel(x,y,new_c);
flood_fill_algo(x+1,y,old_c,new_c);
flood_fill_algo(x-1,y,old_c,new_c);
flood_fill_algo(x,y+1,old_c,new_c);
flood_fill_algo(x,y-1,old_c,new_c);
}

}


Output: