Thursday, February 2, 2017

C Program to draw filled polygon, 2-D bar, and 3-D bar

Graphics in C - Draw Filled Polygon, 2-D bar, 3-D bar 


C Programming language has a wide range of simple graphics functions to create complex figures. In the previous post, we have seen polygons filled with the help of function floodfill(). Turbo C offers a function, function fillpoly(), which is filled automatically with current fill-pattern and colour. Besides this, C computer language has functions for creating 2-d and 3-d bars. Here are these functions described below. Please type this program in your editor.

#include<graphics.h>

void main()
{
         int gd = DETECT, gm;

         /* Declare and initialize arrays */
         int array1[] = {100, 100, 200, 150, 150, 240};
         int array2[] = {100, 200, 200, 400, 50, 300, 10, 150};

         initgraph(&gd, &gm, "c:\\tc\\bgi");

          /* Displays the text string */
         outtextxy(80, 30, "Filled Polygons");

         /* Draws filled polygon with 3 vertices
             filled with default pattern and color */
         fillpoly(3, array1);

          /* Sets the fill-pattern and color */
         setfillstyle(HATCH_FILL, RED);

          /* Draws the filled polygon with
              current fill-pattern and color */
         fillpoly(4, array2);

         outtextxy(320, 30, "Bars");

         /* Draws the 2-d bar with no outline */
         bar(300, 100, 350, 200);

         /* Draw the 2-d bar with outline */
         bar(300, 300, 350, 400);
         rectangle(300, 300, 350, 400);

         outtextxy(450, 30, "Three-dimensional bars");

         /* Draws 3-d bar with no top */
         bar3d(500, 100, 550, 200, 25, 0);

         /* Draws 3-d bar with top drawn */
         bar3d(500, 300, 550, 400, 25, 1);

         setfillstyle(LINE_FILL, GREEN);

         /* Draws 2-d bar with outline */
         bar3d(360, 200, 410, 300, 0, 0);

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

Compile and execute this program. In the output, there are seven graphics. First  two graphics are a set of filled polygons, next three graphics are a set of two-dimensional bars, and last two graphics are a set of three- dimensional bars.

To draw filled polygons, I have used function fillpoly(). Function fillpoly() draws the outline of a polygon using the current line style and colour, then fills the polygon using the current fill pattern and fill colour.

Declaration:

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

where ‘numpoints’ specifies number of points, ‘*polypoints’ points to a sequence of (numpoints*2) integers.

Syntax for calling this function is given below-

fillpoly(numpoints, address-of-array-of-points);

Therefore, the statement given below will draw a polygon filled with default fill pattern(SOLID), and default fill colour(WHITE). In this statement, first argument is ‘3’, so there would be three vertices. Second argument is the address of ‘array1’, which I have declared and initialized with six numbers(3 pairs for 3 vertices). First and last vertices need not to be same.

fillpoly(3, array1);

Now, I have set the current fill pattern, and current fill colour with this statement-

setfillstyle(HATCH_FILL, RED);

Next fillpoly() will draw a filled polygon with this fill pattern, and color. See the statement given below. In this polygon, there would be four vertices.

fillpoly(4, array2);

Next  three graphics are filled bars. Two of these bars have been drawn using the function bar(). Function bar() draws a filled-in rectangular two-dimensional bar. This bar is filled using the current fill pattern and fill colour.

Declaration:

void far bar(int left, int top, int right, int bottom);

Where ‘(left, top)’ is bar’s upper left corner, and ‘(right, bottom)’ is the bar’s lower right corner.

Therefore, the statement given below will draw a rectangular bar with current fill pattern (HATCH_FILL), and current fill color (RED). ‘(300, 100)’ is the upper left corner, and ‘(350, 200)’ is the lower right corner. Please note that  there is no outline in the bar. That is so, because function bar() does not outline the bar automatically.

bar(300, 100, 350, 200);

The statements given below will draw a bar with outline. To draw the outline, I have used function rectangle(). Please note that arguments of bar() and rectangle() are same. I’ll explain about the third bar later in this article.

bar(300, 300, 350, 400);
rectangle(300, 300, 350, 400);

Next two graphics are a set of 3-dimensional bars. To draw three-dimensional bar, I have used the function bar3d(). Function bar3d() draws a three-dimensional rectangular bar, then fills it using current fill pattern, and fill colour. The 3-dimensional outline of the bar is drawn using the current line style, and colour.

Declaration:

void far bar3d(int left, int top, int right, int bottom, int depth, int topflag);

Where ‘(left, top)’ is the bar’s rectangular portion’s upper left corner, and ‘(right, bottom)’ is the lower right corner, ‘depth’ is bar’s depth or third dimension in pixels. ‘topflag’ tells whether a 3-dimensional top is drawn or not. If ‘topflag’ is non-zero, a top is put on the bar. If ‘topflag’ is zero, no top is put on the bar. This way one can stack several bars on top of one another.

Syntax for calling this function is given below-

bar3d(left, top, right, bottom, depth, topflag);

To calculate the ‘typical’ depth for 3-d bar, take one-fourth(25%) of the width of the bar.

depth = (right – left)/4

Therefore, the statement given below will draw a 3-dimensional bar filled with current fill pattern ‘(HATCH_FILL)’, and current fill colour ‘(RED)’. Outline of the bar has been drawn using current line style(default, solid), and colour(default, white). ‘(500, 100)’ is the coordinates of the upper left corner, and ‘(550, 200)’ is the coordinates of the lower right corner. ‘25’ is the depth.’ topflag’ has been set to ‘0’, therefore no top has been put on the 3-d bar.

bar3d(500, 100, 550, 200, 25, 0);

The statement given below will draw a 3-d bar with top drawn, as ‘topflag’ has been set to 1(non-zero).

bar3d(500, 300, 550, 400, 25, 1);

If depth of the 3-d bar is set to ‘0’, a two dimensional bar is drawn with outline. Therefore, the statements given below will draw a 2-d bar with fill pattern(LINE_FILL), and fill color(GREEN). This bar has been outlined in current line style(default, solid), and current color(default, white). Since function bar() does not outline the 2-d bar automatically, therefore, to draw outlined 2-d bar, you can use function bar3d() with depth = 0. This is the explanation of the third graphic in the set of three 2-d bars. Thus, the statements given below will draw a 2-d bar with outline filled with fill-pattern(LINE_FILL) and fil-color(GREEN).

setfillstyle(LINE_FILL, GREEN);
bar3d(360, 200, 410, 300, 0, 0);


Now, the explanation of the functions fillpoly(), bar(), and bar3d() is complete. For the explanation of the remaining functions and terms, please refer my previous posts.

OUTPUT:





You would also like these programs given below:



No comments:

Post a Comment

Please give your comment