# Boundary Fill Algorithm in C and C++

Here you will find out about boundary fill algorithm in C and C++.

Boundary Fill is another seed fill algorithm in which edges of the polygon are drawn.

At that point beginning with some seed any point inside the polygon, we look at the neighbouring pixels to

check whether the boundary pixel is come to. In the event that boundary pixels are not come to, pixels are

featured and the procedure proceeds until boundary pixels are come to.

Following is the algorithm for filling a district in a recursive way with shading indicated fill

Algorithm

Make a capacity named as boundaryfill with 4 parameters (x,y,f_color,b_color).

Call it recursively until the boundary pixels are come to.

Stop.

Program for Boundary Fill Algorithm in C and C++

#### C Program

``````#include<stdio.h>
#include<graphics.h>
#include<dos.h>

void boundaryfill(int x,int y,int f_color,int b_color)
{
if(getpixel(x,y)!=b_color && getpixel(x,y)!=f_color)
{
putpixel(x,y,f_color);
boundaryfill(x+1,y,f_color,b_color);
boundaryfill(x,y+1,f_color,b_color);
boundaryfill(x-1,y,f_color,b_color);
boundaryfill(x,y-1,f_color,b_color);
}
}
//getpixel(x,y) gives the color of specified pixel

int main()
{
int x,y;

printf("Enter x and y positions for circle\n");
scanf("%d%d",&x,&y);

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
boundaryfill(x,y,4,15);
delay(5000);
closegraph();

return 0;
}``````

#### C++ Program

``````#include<iostream.h>
#include<graphics.h>
#include<dos.h>

void boundaryfill(int x,int y,int f_color,int b_color)
{
if(getpixel(x,y)!=b_color && getpixel(x,y)!=f_color)
{
putpixel(x,y,f_color);
boundaryfill(x+1,y,f_color,b_color);
boundaryfill(x,y+1,f_color,b_color);
boundaryfill(x-1,y,f_color,b_color);
boundaryfill(x,y-1,f_color,b_color);
}
}
//getpixel(x,y) gives the color of specified pixel

int main()
{
int x,y;
cout<<"Enter x and y positions for circle\n";
cin>>x>>y;