Boundary Fill

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.


#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 pointn"); 
    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(); 
}

Comments

Post a Comment

Popular posts from this blog

Fish Movement

Pendulum

Moving Wheel