Boundary fill and Flood fill Algorithm

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void flood_fill(int x,int y,int old_col,int new_col)
{
    int cur=getpixel(x,y);
    if(cur==old_col)
    {
        delay(5);
        putpixel(x,y,new_col);
        flood_fill(x+1,y,old_col,new_col);
        flood_fill(x-1,y,old_col,new_col);
        flood_fill(x,y-1,old_col,new_col);
        flood_fill(x,y+1,old_col,new_col);
    }
}

void boundary_fill(int x,int y,int fill,int bou)
{
    int current=getpixel(x,y);
    if(current==bou)
    {
        setcolor(fill);
        putpixel(x,y,fill);
        delay(5);
        boundar_fill(x-1,y-1,fill,bou);
        boundary_fill(x+1,y,fill,bou);
        boundary_fill(x+1,y+1,fill,bou);
        boundary_fill(x-1,y,fill,bou);
        boundary_fill(x,y+1,fill,bou);
        boundary_fill(x,y-1,fill,bou);
        boundary_fill(x+1,y-1,fill,bou);
        boundary_fill(x-1,y+1,fill,bou);
    }
}

void main()
{
    int gm,gd=DETECT,errorcode;
    initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
    setcolor(WHITE);
    rectangle(200,200,300,300);
    flood_fill(200,200,15,4);
    boundary_fill(201,201,6,15);
    getch();
}

Output

1.Flood fill
flood

2.Boundary fillBoundary (2)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s