Flood Fill

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


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.

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
47
48
49
50
#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 (x,y): ");
    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();
}

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! 🙂



7 Responses to Flood Fill

  1. vikarant says:

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

  2. Vivek says:

    thank u very much 4 ur every program

  3. Amol says:

    ocolor and n color is not accepting may colors…
    how to put color to them
    what values should i put

  4. Anurag says:

    what value should i give in enter seed point(x,y)…..
    plzzzz reply its urgent

  5. Rohan says:

    Short and Simple .G8

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:
Bouncing-ball
Bouncing Ball

Animated Road
A Simple Animation of a Road

car moving over a hump
Car moving over a Hump

MinMaxClose