TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Computer Graphics Flood Fill Algorithm

Computer Graphics Flood Fill Algorithm with Computer Graphics Tutorial, Line Generation Algorithm, 2D Transformation, 3D Computer Graphics, Types of Curves, Surfaces, Computer Animation, Animation Techniques, Keyframing, Fractals etc.

<< Back to COMPUTER

Flood Fill Algorithm:

In this method, a point or seed which is inside region is selected. This point is called a seed point. Then four connected approaches or eight connected approaches is used to fill with specified color.

The flood fill algorithm has many characters similar to boundary fill. But this method is more suitable for filling multiple colors boundary. When boundary is of many colors and interior is to be filled with one color we use this algorithm.

Flood Fill Algorithm

In fill algorithm, we start from a specified interior point (x, y) and reassign all pixel values are currently set to a given interior color with the desired color. Using either a 4-connected or 8-connected approaches, we then step through pixel positions until all interior points have been repainted.

Disadvantage:

  1. Very slow algorithm
  2. May be fail for large polygons
  3. Initial pixel required more knowledge about surrounding pixels.

Algorithm:

Procedure floodfill (x, y,fill_ color, old_color: integer)
	If (getpixel (x, y)=old_color)
   {
	setpixel (x, y, fill_color);
	fill (x+1, y, fill_color, old_color);
	 fill (x-1, y, fill_color, old_color);
	fill (x, y+1, fill_color, old_color);
	fill (x, y-1, fill_color, old_color);
     }
}

Program1: To implement 4-connected flood fill algorithm:

#include
#include
#include
#include
void flood(int,int,int,int);
void main()
{
	intgd=DETECT,gm;
	initgraph(&gd,&gm,"C:/TURBOC3/bgi");
	rectangle(50,50,250,250);
	flood(55,55,10,0);
	getch();
}
void flood(intx,inty,intfillColor, intdefaultColor)
{
	if(getpixel(x,y)==defaultColor)
	{
		delay(1);
		putpixel(x,y,fillColor);
		flood(x+1,y,fillColor,defaultColor);
		flood(x-1,y,fillColor,defaultColor);
		flood(x,y+1,fillColor,defaultColor);
		flood(x,y-1,fillColor,defaultColor);
	}
}

Output:

Flood Fill Algorithm

Program2: To implement 8-connected flood fill algorithm:

#include
#include
#include
#include
void floodfill(intx,inty,intold,intnewcol)
{
                int current;
                current=getpixel(x,y);
                if(current==old)
                {
                                delay(5);
                                putpixel(x,y,newcol);
                                floodfill(x+1,y,old,newcol);
                                floodfill(x-1,y,old,newcol);
                                floodfill(x,y+1,old,newcol);
                                floodfill(x,y-1,old,newcol);
                                floodfill(x+1,y+1,old,newcol);
                                floodfill(x-1,y+1,old,newcol);
                                floodfill(x+1,y-1,old,newcol);
                                floodfill(x-1,y-1,old,newcol);
                }
}
void main()
{
                intgd=DETECT,gm;
                initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
                rectangle(50,50,150,150);
                floodfill(70,70,0,15);
                getch();
                closegraph();
}

Output:

Flood Fill Algorithm




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf