Sunday, January 29, 2017

C Program to draw a polygon

Graphics in C - Draw Polygons


So far we have drawn simple graphics using C programming language. Turbo C offers variety of graphics functions for creating simple to complex figures. Here I shall demonstrate how to draw polygons and how to fill them with patterns. First, please type this program in your editor.

#include<graphics.h>
void main()
{
         int gd = DETECT, gm, maxx, maxy;
         int array1[10];
         int array2[10];
         int array3[10];
         initgraph(&gd, &gm, "c:\\tc\\bgi");

         /* Finds maximum screen coordinates */
         maxx = getmaxx();
         maxy = getmaxy();

         /* Initializes array elements with
            coordinates of vertices of first polygon */
         array1[0] = maxx/5;
         array1[1] = maxy/5;
         array1[2] = maxx/5+100;
         array1[3] = maxy/5-20;
         array1[4] = maxx/5+150;
         array1[5] = maxy/5+100;
         array1[6] = maxx/5-50;
         array1[7] = maxy/5+120;
         array1[8] = maxx/5+10;
         array1[9] = maxy/5+20;

         /* Draws an open polygon with 5 vertices */
         drawpoly(5, array1);

         /* Initializes array elements for
            second polygon */
         array2[0] = maxx/2;
         array2[1] = maxy/3;
         array2[2] = maxx/2+100;
         array2[3] = maxy/3-20;
         array2[4] = maxx/2+150;
         array2[5] = maxy/3+100;
         array2[6] = maxx/2-50;
         array2[7] = maxy/3+120;
         array2[8] = array2[0];
         array2[9] = array2[1];

         /* Sets the outline color */
         setcolor(RED);

         /* Draws a closed polygon with 4 vertices */
         /* First and fifth(last) vertices are overlapping */
         drawpoly(5, array2);

          /* Initializes array elements for third polygon */
         array3[0] = maxx/5;
         array3[1] = maxy*3/5;
         array3[2] = maxx/5+100;
         array3[3] = maxy*3/5-20;
         array3[4] = maxx/5+150;
         array3[5] = maxy*3/5+100;
         array3[6] = maxx/5-50;
         array3[7] = maxy*3/5+120;
         array3[8] = array3[0];
         array3[9] = array3[1];

         /* Sets the fill-pattern and color */
         setfillstyle(WIDE_DOT_FILL, GREEN);

         /* Draws the third closed polygon
             with 4 vertices, first and last(fifth)
             vertices are overlapped */
         drawpoly(5, array3);

         /* Fills the insides of polygon with
            current fill-pattern and fill-color */
         floodfill(maxx/5+2, maxy*3/5+2, RED);

         getch();
         closegraph();
         restorecrtmode();
}

Compile, and execute this program. In the output, there are three graphics. First is an open polygon in default white outline, second is a closed polygon in red outline, and third is green pattern filled polygon in red outline.

To draw polygon, I have used the function drawpoly(). Function drawpoly() draws the outline of a polygon using the current line style and color.

Declaration:

void far drawpoly(int numpoints, int far *polypoints);

Where ‘numpoints’ specifies number of points, ‘*polypoints’ points to a sequence of integers, where number of integers is numpoints * 2. Each pair of integers gives the x, and y coordinates of a point on the polygon.

Syntax for calling this function is given below-

drawpoly(numpoints, sequence-of-points);

Hence, the statement given below draws a open polygon with 5 points in default colour outline (white). ‘array1’ is the address of the array having sequence of points or coordinates of vertices.

drawpoly(5, array1);

I have declared three arrays, ‘array1’, ‘array2’, and ‘array3’. In each array, I have given values to each array element individually. You can initialise the arrays at the time of array declaration. The number of array elements would be 2*number of points.

The statement given below sets the colour of outline to ‘RED’.

setcolor(RED);

Hence, the statement given below draws a polygon in red outline. This polygon is closed, and contains four vertices, while in the given statement, first argument is ‘5’, and number of elements in array2 are 10 (5 * 2). This is because, to draw a closed polygon, coordinates of first and last point should be same. Since, first, and last points are same, they are merged into one vertex. That’s why in this polygon, there are four vertices.

drawpoly(5, array2);

The statements given below draw a closed polygon in red outline, filled with a green pattern. Function setfillstyle() sets the current fill pattern, and current fill colour to ‘WIDE_DOT_FILL’, and ‘GREEN’. Function floodfill() fills, or floods the polygon with the current fill-pattern, and fill-colour. In function floodfill(), third argument is ‘RED’, this is because of red outline of the polygon.

setfillstyle(WIDE_DOT_FILL, GREEN);
drawpoly(5, array3);
floodfill(maxx/5+2, maxy*3/5+2, RED);


The description of function drawpoly() is complete. In the next article, there would be a few more functions. For the description of remaining functions and terms, please refer my previous posts..

No comments:

Post a Comment

Please give your comment