fillpoly() function in C

The fillpoly() function in C is part of the graphics.h library and is used to draw and fill a polygon with the current fill pattern and color. It takes the same arguments as drawpoly() but additionally fills the interior of the polygon.

Syntax

void fillpoly(int numpoints, int *polypoints);

Parameters

  • numpoints − Specifies the number of points in the polygon
  • polypoints − Pointer to an array containing x,y coordinates of each point

The polypoints array should contain numpoints * 2 integers, where each pair represents the x and y coordinates of a vertex. To close the polygon, the last point should match the first point.

Example: Drawing a Filled Triangle

Note: This example uses graphics.h which requires Turbo C/Borland C compiler with graphics support. Modern compilers may not support this header.

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

int main() {
    int gd = DETECT, gm;
    
    // Coordinates for a triangle
    int triangle[] = {320, 150, 400, 250, 240, 250, 320, 150};
    
    // Initialize graphics mode
    initgraph(&gd, &gm, "");
    
    // Set fill style and color
    setfillstyle(SOLID_FILL, RED);
    
    // Draw and fill the triangle
    fillpoly(4, triangle);
    
    getch();
    closegraph();
    return 0;
}

How It Works

(320,150) (400,250) (240,250) Back to start fillpoly() Function Coordinates must form a closed polygon Array: {320, 150, 400, 250, 240, 250, 320, 150} Points: (x1,y1) (x2,y2) (x3,y3) (x1,y1)

Key Points

  • The polygon is automatically closed by connecting the last point to the first point
  • Use setfillstyle() to change the fill pattern and color before calling fillpoly()
  • The number of coordinates in the array should be numpoints * 2
  • Graphics mode must be initialized with initgraph() before using this function

Conclusion

The fillpoly() function provides an efficient way to draw filled polygons in C graphics programming. It automatically handles the filling process and works with any polygon defined by coordinate points.

Updated on: 2026-03-15T12:54:18+05:30

772 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements