Boundary Fill

On October 7, 2008, in Algorithms, by , Views : 23011


The boundary fill algorithm fills a region with the given fill color until the given boundary color is found. Here i have used the 8-point technique to color a region. The program is using a recurssive function. The function return when the pixel to be colored is the boundary color or is already the fill color.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
 
void boundary_fill(int x, int y, int fcolor, int bcolor)
{
 
    if ((getpixel(x, y) != fcolor) && (getpixel(x, y) != bcolor)) {
	putpixel(x, y, fcolor);
	boundary_fill(x + 1, y, fcolor, bcolor);
	boundary_fill(x - 1, y, fcolor, bcolor);
	boundary_fill(x, y - 1, fcolor, bcolor);
	boundary_fill(x, y + 1, fcolor, bcolor);
	boundary_fill(x + 1, y - 1, fcolor, bcolor);
	boundary_fill(x + 1, y + 1, fcolor, bcolor);
	boundary_fill(x - 1, y - 1, fcolor, bcolor);
	boundary_fill(x - 1, y + 1, fcolor, bcolor);
    }
}
 
 
void main()
{
    int x, y, fcolor, bcolor;
 
    clrscr();
    printf("Enter the seed point (x,y) : ");
    scanf("%d%d", &x, &y);
    printf("Enter boundary color : ");
    scanf("%d", &bcolor);
    printf("Enter new color : ");
    scanf("%d", &fcolor);
 
    int gd = DETECT, gm = DETECT;
    initgraph(&gd, &gm, "");
    cleardevice();
 
    /* 
       please ceate your own shapes  
       to make a closed area. 
     */
 
    boundary_fill(x, y, fcolor, bcolor);
 
    getch();
}

Download the code using the below link:

Please 'Like' or '+1' us to get the download link! (If you have already liked us on facebook, then like us either on G+ or twitter)



Hope, it helps! 🙂



4 Responses to Boundary Fill

  1. shivangi says:

    very nice….thank u

  2. suraj bhatt says:

    thnks man for the useful information

Leave a Reply

Your email address will not be published. Required fields are marked *


*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

  • Dipin Krishna

Archives

All entries, chronologically...

Read more:
Flood Fill
Flood Fill

Rotated Figure
2D Transformations: Translations, Scaling, Rotation and Shearing

kite
Flying Kite

MinMaxClose