Implementing Flood Fill Algorithm in C++

Here is implementation of flood fill algorithm in C++.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void ffill(int x,int y,int o_color,int n_color)
{
	if(getpixel(x,y)==o_color)
	{

		putpixel(x,y,n_color);
		ffill(x+1,y,o_color,n_color);
		ffill(x,y+1,o_color,n_color);
		ffill(x,y-1,o_color,n_color);
		ffill(x-1,y,o_color,n_color);
		ffill(x+1,y+1,o_color,n_color);
		ffill(x-1,y-1,o_color,n_color);
		ffill(x-1,y+1,o_color,n_color);
		ffill(x+1,y-1,o_color,n_color);
	}
}
void main()
{
	 int gdriver = DETECT, gmode, errorcode;
	 initgraph(&gdriver, &gmode, "C:\\bgi");
	 rectangle(50,50,100,100);
	 ffill(55,55,0,3);
	 getch();
}

Output:

Flood Fill
Flood Fill

Leave a Reply

Your email address will not be published. Required fields are marked *