Parameters
| Name | Type | Description |
|---|---|---|
x | int | The x-coordinate of the seed point. Must be located inside the boundary you wish to paint. |
y | int | The y-coordinate of the seed point. Must be located inside the boundary you wish to paint. |
border | int | The color index (from the BGI color table, 0-15) of the boundary outline where the fill should stop. |
The seed point coordinates `(x, y)` must be integers and represent a coordinate inside the target shape. If the seed point falls directly on the boundary outline color (matching `border`), or if the region is already filled with the target fill color, `floodfill()` exits immediately without making changes. The `border` argument is an integer color index corresponding to standard BGI colors like `WHITE`, `RED`, or `YELLOW`.
Return Value
floodfill() returns void. It modifies the frame buffer in place and returns control. Note that if BGI's internal fill stack overflows (which can happen when filling extremely large or complex fractal shapes on old 16-bit systems), the function may fail to complete. You can query graphresult() to check for errors, though standard shapes are handled without memory issues.
Code Examples
Basic Filled Circle
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
// 1. Draw a white circle outline
setcolor(WHITE);
circle(320, 240, 80);
// 2. Set fill style to Solid and color to Red
setfillstyle(SOLID_FILL, RED);
// 3. Flood fill starting from center of the circle (320, 240)
// stopping at the WHITE outline border
floodfill(320, 240, WHITE);
getch();
closegraph();
return 0;
}
This program renders a circle outline in white. It then sets the fill color to red and triggers `floodfill()` starting from the circle's center point `(320, 240)`. The red fill spreads outward and halts at the white border, leaving a red circle outlined in white.
Compartmentalized Fill Grid
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
// Draw outline grid using yellow lines
setcolor(YELLOW);
rectangle(100, 100, 400, 250);
line(250, 100, 250, 250); // Vertical divider
// Fill left compartment with green hatch
setfillstyle(HATCH_FILL, GREEN);
floodfill(150, 150, YELLOW);
// Fill right compartment with blue dots
setfillstyle(CLOSE_DOT_FILL, BLUE);
floodfill(300, 150, YELLOW);
getch();
closegraph();
return 0;
}
Here, a yellow boundary is split into two panels by a vertical divider line. By calling `floodfill()` twice with different seed points and fill styles, the program fills both panels independently because the yellow border blocks the fill from crossing between sections.
Common Mistakes
Mismatching border colors
The most common mistake is passing a different color for the `border` argument than the one used to draw the boundary. For example, if you draw a circle with setcolor(RED) and call floodfill(x, y, WHITE), BGI will search for a white outline. Since it doesn't find one, it will leak through the red outline and fill the entire screen. Ensure the border color argument matches the outline drawing color exactly.
Gaps in outlines (Paint Leaks)
If you construct a boundary out of multiple lines (like drawing a triangle manually) and leave even a single-pixel gap between lines, the paint will leak through the gap and fill the background. Double-check your coordinate math to ensure all endpoints connect perfectly.
Seed point on the border
If your seed point coordinate (x, y) lands directly on the boundary outline pixel itself, `floodfill()` will immediately stop, resulting in no fill being drawn. Ensure that your seed coordinates are strictly inside the shape interior.
Using floodfill on non-closed curves
Applying flood fill to shapes drawn with arc() or ellipse() without closed endpoints is guaranteed to leak. If you want to draw and fill ellipses or curves directly, use fillellipse(), fillpoly(), or sector() which draw and fill shapes in a single step without relying on boundary checks.
Try it online
Run these paint examples instantly. Copy and paste them into the compiler editor window to execute graphics code directly in the browser.