Home Docs
Graphics.h Documentation
Compiler

Polygons and Fill

Polygons & Fill in graphics.h

When basic shapes are not enough, use polygon and region-fill functions. This guide explains drawpoly(), fillpoly(), and floodfill() with clear rules, examples, and debugging tips.

On This Page

Core Functions

void drawpoly(int numpoints, int* polypoints);
void fillpoly(int numpoints, int* polypoints);
void floodfill(int x, int y, int border);
  • drawpoly(): Draws only the polygon boundary using the current drawing color.
  • fillpoly(): Draws and fills the polygon using the active fill style and fill color.
  • floodfill(): Starts at a seed point and fills until it hits the specified border color.

Typical flow: draw or define boundary, choose fill style, then fill the interior.

How Polygon Points Work

Polygon points are passed as alternating x, y values in one integer array.

int pts[] = {100,120, 200,80, 280,160, 150,220};  // 4 points
drawpoly(4, pts);
  • numpoints is number of vertices, not array length.
  • Array length must be 2 * numpoints.
  • Vertices are connected in order, and the last point connects back to the first point.

drawpoly() in Detail

drawpoly() draws only the border of a polygon. Use it when you want an outline shape without fill.

void drawpoly(int numpoints, int* polypoints);
  • numpoints: Number of vertices.
  • polypoints: Array in x1, y1, x2, y2, ... format.
  • Border color comes from setcolor().

This example draws a 5-vertex polygon outline.

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

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

    int points[] = {120, 120, 240, 90, 320, 170, 270, 260, 150, 230};
    setcolor(WHITE);
    drawpoly(5, points);

    getch();
    closegraph();
    return 0;
}

Click Run (DOS) to test drawpoly().

fillpoly() in Detail

fillpoly() draws and fills a polygon in one call. It uses active fill settings from setfillstyle().

void fillpoly(int numpoints, int* polypoints);
  • Set boundary color using setcolor().
  • Set interior style and color using setfillstyle(pattern, color).
  • Then call fillpoly() with correct vertex count.

This example fills a pentagon with a solid light-blue color.

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

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

    int points[] = {100, 230, 190, 130, 300, 170, 260, 290, 130, 300};
    setcolor(YELLOW);
    setfillstyle(SOLID_FILL, LIGHTBLUE);
    fillpoly(5, points);

    getch();
    closegraph();
    return 0;
}

Click Run (DOS) to test fillpoly().

floodfill() in Detail

floodfill() fills an enclosed area from a seed point. It stops where it finds the border color you pass.

void floodfill(int x, int y, int border);
  • Draw a closed shape first.
  • Set fill style before calling floodfill().
  • Pass the exact border color used to draw the boundary.

This example fills the inside of a rectangle and then a circle using two seed points.

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

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

    setcolor(WHITE);
    rectangle(80, 90, 280, 250);
    circle(430, 170, 80);

    setfillstyle(HATCH_FILL, GREEN);
    floodfill(120, 120, WHITE);

    setfillstyle(SOLID_FILL, LIGHTRED);
    floodfill(430, 170, WHITE);

    getch();
    closegraph();
    return 0;
}

Click Run (DOS) to test floodfill().

Common Mistakes

  • Passing array length instead of vertex count to drawpoly()/fillpoly().
  • Forgetting to set fill style before fillpoly().
  • Using an open boundary with floodfill().
  • Passing the wrong border color to floodfill().

Quick debug checklist: verify closed edges, verify border color argument, and verify seed point is inside the region.

Continue with Colors & Palette and Fill Styles for better control of filled regions.