Wednesday, February 12, 2020

Boundary Fill Algorithm implementation in C Language

Boundary Fill Algorithm implementation in C Language


Boundary Fill Algorithm



In Boundary fill algorithm start filling from a inside point towards outward till the boundary. 
It accepts coordinates of inside point (seed point), fill color and boundary color.

It fills seed pixel with fill color and test neighbors of seed pixel. If neighbor pixel is not having fill color or boundary color then paint with fill color this process continues till boundary. Neighbors are tested using 4-connected or 8- connected approach.

Program for Boundary Fill Algorithm

#include<stdio.h>

#include<graphics.h>

#include<dos.h>
#include<conio.h>

void boundary_fill_algo(int,int,int,int); // function prototype


void main()


{


int gd=DETECT,gm;


initgraph(&gd,&gm,"c:\\turboc3\\bgi");


setbkcolor(12); //used to set background color


setcolor(6); //used to set circle boundary color


circle(200,200,40); // function used to draw circle directly


boundary_fill_algo(200,200,6,3);//boundary_fill function call 

setcolor(8);

rectangle(300,300,350,350);

boundary_fill(301,301,8,10);

setcolor(6);

circle(450,200,40);

boundary_fill(450,200,6,3);

getch();

}

//boundary_fill function definition 

void boundary_fill_algo(int x,int y,int bound_col,int fill_col)

{

int b=getpixel(x,y);

if(b!=bound_col & b!=fill_col)

{

 delay(50);   //delay() function adds delay into execution

 putpixel(x,y,fill_col);

 boundary_fill_algo( x+1, y, bound_col, fill_col);

 boundary_fill_algo( x-1, y, bound_col, fill_col);

 boundary_fill_algo( x, y+1, bound_col, fill_col);

 boundary_fill_algo( x, y-1, bound_col, fill_col);

}

}


Output: