Boundary-fill algorithm is used for region filling when the region is boundary-defined i.e. pixels having colour equal to `boundary_col`

comprise the boundary of region.This algorithm starts with a seed pixel inside the region which is the starting point for the recursive algorithm. The algorithm checks to see if pixel is boundary pixel(defined by `boundary_col`

) or it has already been filled.If answer is no,pixel is filled with `fill_col`

and a recursive call is made using each neighbouring pixel as a new seed.Neighbours can be 4 connected (horizontal and vertical neighbours) or 8-connected (horizontal,vertical and diagonal neighbours).

```
#include<stdio.h>
#include<conio.h>
/*Header file for built-in graphics functions such as plot() and initgraph()*/
#include<graphics.h>
void boundary(int,int,int,int);
int main()
{
int gd=DETECT,gm,x,y,fill_col;
initgraph(&gd,&gm,"c:\\tc\\bgi");/*loads the graphic driver and puts system into graphics mode*/
/*
syntax for initgraph() is:
void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);
*graphdriver is an integer that specifies the graphics driver to be used.DETECT requests auto-detect for the driver.
*graphmode is an integer that specifies the initial graphics mode.If *graphdriver equals DETECT,*graphmode is set by initgraph to the highest resolution available for the detected driver.
*/
clrscr();
printf("\n\tBOUNDARY FILLING ALGORITHM\n\n");
printf("Enter coordinates of seed point\n");
scanf("%d %d",&x,&y);
printf("Enter fill colour\n");
scanf("%d",&fill_col);
printf("The object after filling is:\n");
/*rectangle(x1,y1,x2,y2) draws rectangle on screen with (x1,y1) and (x2,y2) specifying coordinates of top-left and bottom-right vertex respectively.*/
rectangle(150,150,200,200);
boundary(x,y,fill_col,getpixel(150,150));/*getpixel(x,y) returns the colour of pixel at (x,y)*/
getch();
closegraph();
/*closegraph function closes the graphics mode, deallocates all memory allocated by graphics system and restores the screen to the original mode*/
return 0;
}
//Boundary Filling Function
void boundary(int x,int y, int fill_col, int boundary_col)
{
/*
(x,y) specify coordinates of seed point.
fill_col specifies the colour to be filled in closed region.
boundary_col specifies the colour of boundary pixels.
*/
if(getpixel(x,y)!=boundary_col && getpixel(x,y)!=fill_col)
{
putpixel(x,y,fill_col);
boundary(x+1,y,fill_col,boundary_col);
boundary(x,y+1,fill_col,boundary_col);
boundary(x-1,y,fill_col,boundary_col);
boundary(x,y-1,fill_col,boundary_col);
}
return ;
}
```

You can run this code in Turbo C++. Dev C++ and Microsoft visual studio do not support graphics libraries.

You can download Turbo C++ using this link Turbo C++

Other articles related to Computer Graphics available on TutsHeap are:

Cohen Sutherland Line Clipping Algorithm

Flood-Fill Algorithm

Mid-Point Ellipse Drawing Algorithm

Mid-Point Circle Drawing Algorithm

DDA Line Drawing Algorithm

Bresenhamâ€™s Line Drawing Algorithm

This content has been helpful to you?

Thanks for contributing!

Pingback: Cohen Sutherland Line Clipping Algorithm | Tuts Heap()

Pingback: Mid-Point Ellipse Drawing Algorithm | Tuts Heap()

Pingback: Mid-Point Circle Drawing Algorithm - Tuts Heap()

Pingback: DDA Line Drawing Algorithm - Tuts Heap()

Pingback: Bresenham's Line Drawing Algorithm - Tuts Heap()