Coding

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>
 
main()
{
   int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   drawpoly(4, points);
 
   getch();
   closegraph();
   return 0;
}

 

READ  10 Websites to Learn Coding For Free

About the author

Venkatesan Prabu

Venkatesan Prabu

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.

Add Comment

Click here to post a comment