Flood Fill

The flood fill algorithm replaces a current color with a new color.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 replaced is not the given old color.

#include<stdio.h> 
#include<conio.h> 
#include<graphics.h> 

void flood_fill(int x,int y, int ncolor,int ocolor) 
{ 
    if(getpixel(x,y) == ocolor) 
    { 
        putpixel(x,y,ncolor); 
        flood_fill(x+1,y,ncolor,ocolor); 
        flood_fill(x+1,y-1,ncolor,ocolor); 
        flood_fill(x+1,y+1,ncolor,ocolor); 
        flood_fill(x,y-1,ncolor,ocolor); 
        flood_fill(x,y+1,ncolor,ocolor); 
        flood_fill(x-1,y,ncolor,ocolor); 
        flood_fill(x-1,y-1,ncolor,ocolor); 
        flood_fill(x-1,y+1,ncolor,ocolor); 
    } 
} 


void main() 
{ 
    int x,y,ncolor,ocolor; 

    clrscr(); 
    printf("Enter the seed point\n"); 
    scanf("%d%d",&x,&y); 
    printf("Enter old color : "); 
    scanf("%d",&ocolor); 
    printf("Enter new color : "); 
    scanf("%d",&ncolor); 

    int gd=DETECT,gm=DETECT; 
    initgraph(&gd,&gm,""); 
    cleardevice(); 

    /* 

    Draw some figures 
    to create closed shapes 
    you can use any biut-in functions.  
    Everyone, please make different figures. 

    */ 
    circle(300,200,50);

    flood_fill(x,y,ncolor,ocolor); 

    getch(); 
}

Comments

  1. thanks for this programming....this will help me in improvement in my programming.

    ReplyDelete
  2. thank u very much 4 ur every program

    ReplyDelete

Post a Comment

Popular posts from this blog

Pendulum

Fish Movement

Moving Wheel