Tuesday, June 26, 2012

54. FLOOD FILL & BOUNDARY FILL ALGORITHM


//FLOOD FILL WITH EIGHT CONNECTED REGIONS
#include<stdio.h>
#include<graphics.h>
main()
{
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
rectangle(50,50,100,100);
flood(55,55,4,15);
getch();
closegraph();
}
flood(seed_x,seed_y,foreground_col,background_col)
{

if(getpixel(seed_x,seed_y)!=background_col && getpixel(seed_x,seed_y)!= foreground_col)
{
putpixel(seed_x,seed_y,foreground_col);
flood(seed_x+1,seed_y,foreground_col,background_col);
flood(seed_x-1,seed_y,foreground_col,background_col);
flood(seed_x,seed_y+1,foreground_col,background_col);
flood(seed_x,seed_y-1,foreground_col,background_col);
flood(seed_x+1,seed_y+1,foreground_col,background_col);
flood(seed_x-1,seed_y-1,foreground_col,background_col);
flood(seed_x+1,seed_y-1,foreground_col,background_col);
flood(seed_x-1,seed_y+1,foreground_col,background_col);
}
}


//BOUNDARY FILL WITH EIGHT CONNECTED REGIONS

#include<stdio.h>
#include<graphics.h>
main()
{
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
rectangle(50,50,100,100);
boundary(55,55,10,15);
getch();
closegraph();
}
boundary (seed_x,seed_y,bound_col ,fill_col)
{
if(getpixel(seed_x,seed_y)!=background_col && getpixel(seed_x,seed_y)!= foreground_col)
{
putpixel(seed_x,seed_y,foreground_col);
boundary (seed_x+1,seed_y, bound_col, fill_col);
boundary (seed_x-1,seed_y, bound_col, fill_col);
boundary (seed_x,seed_y+1, bound_col, fill_col);
boundary (seed_x,seed_y-1, bound_col, fill_col);
boundary (seed_x+1,seed_y+1, bound_col, fill_col);
boundary (seed_x-1,seed_y-1, bound_col, fill_col);
boundary (seed_x+1,seed_y-1, bound_col, fill_col);
boundary (seed_x-1,seed_y+1, bound_col, fill_col);
}
}


SAMPLE OUTPUT: