Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
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 callingfillpoly() - 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.
