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

shading (f_color) up to a boundary shading determined boundary shading (b_color)

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.

4 Connected Region (Image Source)

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 gm,gd=DETECT,radius;
	int x,y;
	
	printf("Enter x and y positions for circle\n");
	scanf("%d%d",&x,&y);
	printf("Enter radius of circle\n");
	scanf("%d",&radius);
	
	initgraph(&gd,&gm,"c:\\turboc3\\bgi");
	circle(x,y,radius);
	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 gm,gd=DETECT,radius;
	int x,y;
	cout<<"Enter x and y positions for circle\n";
	cin>>x>>y;
	cout<<"Enter radius of circle\n";
	cin>>radius;
	
	initgraph(&gd,&gm,"c:\\turboc3\\bgi");
	circle(x,y,radius);
	boundaryfill(x,y,4,15);
	delay(5000);
	closegraph();
	
	return 0;
}

Remark underneath in the event that you have questions or discovered anything off base in above boundary fill algorithm in C and C++.

Leave a Comment