0% found this document useful (0 votes)
19 views1 page

Flood Fill

C++ Graphics Program

Uploaded by

Sanjay Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views1 page

Flood Fill

C++ Graphics Program

Uploaded by

Sanjay Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <graphics.

h>
#include <conio.h>
#include <dos.h>

// Flood Fill function using recursion


void floodFill(int x, int y, int fill_color, int boundary_color) {
// Get the color of the current pixel
int current_color = getpixel(x, y);

// Check if the current pixel is not the boundary and not already filled
if (current_color != boundary_color && current_color != fill_color) {
// Set the pixel to the fill color
putpixel(x, y, fill_color);

// Recursively call floodFill for neighboring pixels (up, down, left,


right)
floodFill(x + 1, y, fill_color, boundary_color); // Right
floodFill(x - 1, y, fill_color, boundary_color); // Left
floodFill(x, y + 1, fill_color, boundary_color); // Down
floodFill(x, y - 1, fill_color, boundary_color); // Up
}
}

int main() {
// Initialize graphics mode
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

// Set the boundary color and fill color


int boundary_color = WHITE;
int fill_color = YELLOW;

// Draw a rectangle to fill (boundary of rectangle is WHITE)


rectangle(100, 100, 200, 200);

// Fill the inside of the rectangle using flood fill


floodFill(150, 150, fill_color, boundary_color);

// Hold the screen to see the output


getch();

// Close the graphics mode


closegraph();

return 0;
}

You might also like