You are here

drawpoly function in c

Drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc.

Declaration: void drawpoly( int num, int *polypoints );

num indicates (n+1) number of points where n is the number of vertices in a polygon, polypoints points to a sequence of (n*2) integers . Each pair of integers gives x and y coordinates of a point on the polygon. We specify (n+1) points as first point coordinates should be equal to (n+1)th to draw a complete figure.

To understand more clearly we will draw a triangle using drawpoly, consider for example,the array :-
int points[] = { 320, 150, 420, 300, 250, 300, 320, 150};

points array contains coordinates of triangle which are (320, 150), (420, 300) and (250, 300). Note that last point(320, 150) in array is same as first. See the program below and then its output, it will further clear your understanding.

C program for drawpoly

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

   int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};

   initgraph(&gd, &gm, "C:\\TC\\BGI");

   drawpoly(4, points);

   return 0;