This algorithm picks a seed point inside an object and starts to fill until it encounters the boundary of the object. The color of the boundary and the color that we fill should be different for this algorithm to work.
In this algorithm, we assume that color of the boundary is same for the entire object. The boundary fill algorithm can be implemented by 4-connected pixels or 8-connected pixels.
4 Connected Approach:
In 4-connected technique pixels are used as shown in the figure above. We put the pixels above, below, to the right, and to the left side of the current pixels and this process will continue until we find a boundary with different color.
8 Connected Approach:
In 8-connected technique pixels are used as shown in the figure above. We put pixels above, below, right and left side of the current pixels as we were doing in 4-connected technique.In addition to this, we are also putting pixels in diagonals so that entire area of the current pixel is covered. This process will continue until we find a boundary with different color.
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Title: Flood Fill Algorithm | |
Description: A C Program to draw any shape and fill color in it using Flood Fill Algorithm's either 4-connected or 8-connected approach | |
Author: Saideep Dicholkar | |
*/ | |
#include<stdio.h> | |
#include<conio.h> | |
#include<graphics.h> | |
void ffill(int,int,int,int,int); | |
void convex(int,int); | |
void concave(int,int); | |
void main() | |
{ | |
int gd=DETECT,gm,ch,x,y; | |
initgraph(&gd,&gm,"C:\\tc\\bgi"); | |
cleardevice(); | |
printf("1.Convex Polygon\n2.Concave Polygon\nEnter Choice: "); | |
scanf("%d",&ch); | |
x=(100+152)/2; | |
y=(100+152)/2; | |
switch(ch) | |
{ | |
case 1: convex(x,y); | |
break; | |
case 2: concave(x,y); | |
break; | |
default: printf("Wrong choice entered"); | |
break; | |
} | |
getch(); | |
closegraph(); | |
restorecrtmode(); | |
} | |
void convex(int x,int y) | |
{ | |
line(100,100,150,100); | |
line(150,100,150,150); | |
line(150,150,100,150); | |
line(100,150,100,100); | |
ffill(x,y,0,12,1); | |
} | |
void concave(int x,int y) | |
{ | |
line(100,100,125,125); | |
line(125,125,150,100); | |
line(150,100,150,150); | |
line(150,150,100,150); | |
line(100,150,100,100); | |
ffill(x,y,0,12,0); | |
} | |
void ffill(int x,int y,int ocolor,int fcolor,int n) | |
{ | |
int c=getpixel(x,y); | |
if(n==1) | |
{ | |
if(c==ocolor) | |
{ | |
delay(2); | |
putpixel(x,y,fcolor); | |
ffill(x+1,y,ocolor,fcolor,0); | |
ffill(x-1,y,ocolor,fcolor,0); | |
ffill(x,y+1,ocolor,fcolor,0); | |
ffill(x,y-1,ocolor,fcolor,0); | |
} | |
} | |
else | |
{ | |
if(c==ocolor) | |
{ | |
delay(2); | |
putpixel(x,y,fcolor); | |
ffill(x+1,y,ocolor,fcolor,1); | |
ffill(x-1,y,ocolor,fcolor,1); | |
ffill(x,y+1,ocolor,fcolor,1); | |
ffill(x,y-1,ocolor,fcolor,1); | |
ffill(x+1,y+1,ocolor,fcolor,1); | |
ffill(x-1,y+1,ocolor,fcolor,1); | |
ffill(x+1,y-1,ocolor,fcolor,1); | |
ffill(x-1,y-1,ocolor,fcolor,1); | |
} | |
} | |
} |
Output:
Note: Just copy the files "CGA.bgi" &" EGAVGA.bgi" from bgi folder to bin. So, if you have copied these 2 files then you can skip writing "C:\\tc\\bgi" in initgraph with just ""(2 double quotes)
Comments
Post a Comment