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..

Saturday, January 21, 2017

C Program to draw arc, pie-slice, and rectangle

Graphics in C - Draw arc, pie-slice, rectangle


C programming language is a very powerful computer language. It has all the functions you will need to create graphics. Here are a few more functions, which I shall demonstrate today. Before I explain something, please type this program in your editor:

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

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

        /* Draws a circular arc */
        arc(maxx/5, maxy/5, 60, 250, 50);

        /* Draws a full circular arc (circle) */
        arc(maxx/3, maxy/5, 0, 360, 50);

        /* Draws a pie-slice */
        pieslice(maxx/2, maxy/5, 50, 270, 50);

        /* Draws a rectangle */
        rectangle(maxx*3/5, maxy/8, maxx*3/5+120, maxy/8+80);

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

        /* Draws the pie-slice filled with above
           fill-style and color */
        pieslice(maxx/5, maxy/2, 60, 300, 50);

        arc(maxx/3, maxy/2, 0, 360, 50);
        /* floodfill() fills the arc with current
           fill-style and color */
        floodfill(maxx/3+1, maxy/2+1, WHITE);

        rectangle(maxx/2, maxy*2/5, maxx/2+120, maxy*2/5+80);
        /* floodfill() fills the rectangle with
           current fill-style and color */
        floodfill(maxx/2+1, maxy*2/5+1, WHITE);

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

Compile and execute this program. There are seven graphics in the output. In these, first graphic is an arc,  second is a fully drawn arc or say, circle. For these I have used function arc()Function arc() draws a circular arc in the current drawing color.

Declaration:

void far arc(int x, int y, int stangle, int endangle, int radius);

Where ‘(x, y)’ is the center point of arc, ‘stangle’ is the start angle in degrees, ‘endangle’  is the end angle in degrees, and ‘radius’ is the radius of circular arc in pixels.

Syntax for calling this function is given below-

arc(x, y, stangle, endangle, radius);

Therefore, in the above program, the statement given below will draw a circular arc, whose center is ‘(maxx/5, maxy/5)’, start angle is ‘60’ degrees, end angle is ‘250’ degrees, and radius is ‘50’ pixels.

arc(maxx/5, maxy/5, 60, 250, 50);

If start angle is ‘0’, and end angle is ‘360’ degrees, the call to arc draws a complete circle. Hence, the statement given below will draw a complete circle.

arc(maxx/3, maxy/5, 0, 360, 50);

In the output, third graphic is a pie slice. For  drawing pie slice, I have used the function pieslice(). Function pieslice() draws a circular pie-slice in the current drawing color, then fills it using the current fill pattern and fill color.

Declaration:

void far pieslice(int x, int y, int stangle, int endangle, int radius);

Meaning of arguments is same as that of in the function arc().

Syntax for calling the function pieslice() is given below-

pieslice(x, y, stangle, endangle, radius);

Therefore, the statement given below will draw a circular pie slice with default fill pattern, and fill colour, since I have not set the fill pattern, and fill colour. On my computer default fill pattern is ‘solid fill’, and default fill colour is ‘white’.

pieslice(maxx/2, maxy/5, 50, 270, 50);

As I have mentioned earlier in the case of arc(), if the start angle is ‘0’, and end angle is ‘360’ degrees, the graphic drawn by the function pieslice() too will be a complete circle, filled  with current fill pattern, and current fill color. 

Fourth graphic is a rectangle. This has been drawn by the function rectangle(). Function rectangle() draws a rectangle in the current line style, thickness, and drawing colour.

Declaration:

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

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

Syntax for calling the function rectangle() is given below-

rectangle(left, top, right, bottom);

Therefore, the statement given below draws a rectangle from ‘(maxx*3/5, maxy/8)’ to ‘(maxx*3/5+120, maxy/8+80)’ in the default color(white), default thickness(1 pixel), and default line style(solid).

rectangle(maxx*3/5, maxy/8, maxx*3/5+120, maxy/8+80);

How to change the colour, thickness, and line style, I’ll explain this later on.

Fifth graphic is a pie slice, filled with red pattern. These pattern and colour have been defined by the statement given below-

setfillstyle(HATCH_FILL, RED);

Function setfillstyle() has already been defined in my previous post.

Now, current fill pattern is ‘HATCH_FILL’, and current fill colour is ‘RED’.

Therefore, the statement given below draws a pie slice, filled with current fill pattern(HATCH_FILL), and current fill colour(RED).

pieslice(maxx/5, maxy/2, 60, 300, 50);

Sixth, and seventh graphics are a full drawn arc(complete circle), and a rectangle. These are filled with current fill pattern(HATCH_FILL), and current fill color(RED). These graphics are drawn by the functions arc() and rectangle(), which I have already explained. These graphics are not automatically filled by current fill pattern, and current fill color. To fill these graphics I have used the function floodfill(). This function fills a bounded region or an enclosed area.

Declaration:

void far floodfill((int x, int y, int border);

The area bounded by the color border is flooded with the current fill pattern, and current fill color. ‘(x, y)’ is a ‘seed point’. If the seed is within an enclosed area, the inside will be filled. If the seed is outside the enclosed area, the exterior will be filled. Argument ‘border’ is the color of the border of the enclosed area.

Function floodfill() does not work with the IBM-8514 driver.

Syntax for calling this function is given below-

floodfill(x, y, border_color);

I have used function floodfill() every time after function arc(), and function rectangle().

arc(maxx/3, maxy/2, 0, 360, 50);
floodfill(maxx/3+1, maxy/2+1, WHITE);
rectangle(maxx/2, maxy*2/5, maxx/2+120, maxy*2/5+80);
floodfill(maxx/2+1, maxy*2/5+1, WHITE);

Points ‘(maxx/3+1, maxy/2+1)’, and ‘( maxx/2+1, maxy*2/5+1)’ are the seed points, which are slightly inside the arc(complete circle), and the rectangle. Therefore, these are filled with current fill pattern, and current fill colour, which are red hatched pattern in our case. ‘WHITE’ is the colour of border of these graphics.

Now, the explanation of this simple program is complete. I’ll explain a few more functions in my next article. For the remaining functions and terms, please refer my previous posts.

OUTPUT:







Saturday, January 14, 2017

C Program to draw different types of ellipses

Graphics in C - Different Types of Ellipses



You have seen some basic graphics functions in C computer language in my previous posts. Here are few more functions. First, type this program in your editor.

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

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

           /* Set the color of the outline
               of the graphics */
           setcolor(BLUE);

           /* Display a message on screen */
           outtextxy(150, 20, "Different types of ellipse");

           /* Draw a full ellipse */
           ellipse(maxx/5,  maxy/5, 0, 360, 80, 50);

            /* Plot a pixel on screen */
           putpixel(maxx/5, maxy/5, GREEN);

           /* Draw a partial ellipse */
           ellipse(maxx/2, maxy/5, 60, 270, 80, 50);
           putpixel(maxx/2, maxy/5, GREEN);

           /* Draw a filled ellipse */
           fillellipse(maxx*3/4, maxy/5, 80, 50);

           /* Set the fill-style of graphics */
           setfillstyle(LINE_FILL, CYAN);

           /* Draw another filled ellipse */
           fillellipse(maxx/5, maxy/2, 80, 50);

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

Compile and execute this program. This program will show four graphics. One is full drawn ellipse, second is partially drawn ellipse, third is solid filled ellipse, and fourth is line pattern filled ellipse.

The explanation of the above program is given below-

Function outtextxy() displays a text string at the specified location in the graphics mode.

Declaration:

void far outtextxy(int x, int y, char far *textstring);

The syntax for calling this function is given below-

outtextxy(x, y, textstring);

where (x, y) is the position on the screen where text is to be displayed. Third argument is the string to be displayed.

So, the statement given below displays the string “Different types of ellipse” at the position (150, 20).

outtextxy(150, 20, "Different types of ellipse");

Function ellipse() draws an elliptical arc in the current drawing color.

Declaration:

void far ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);

The syntax for calling this function is given below-

ellipse(x, y, stangle, endangle, xradius, yradius);

where (x, y) is the screen coordinate of the center, ‘stangle’ is the starting angle, ‘endangle’ is the end angle, ‘xradius’ is the horizontal axis, and ‘yradius’ is the vertical axis of the elliptical arc.

Therefore, the statement given below displays an ellipse outlined in blue color (I have set the color blue by the function setcolor() ) . Third and fourth arguments i.e. start and end angle are given ‘0’ and ‘360’ therefore ellipse has been drawn in full. ‘80’ is the horizontal axis or radius and ‘50’ is the vertical axis or radius.  

ellipse(maxx/5,  maxy/5, 0, 360, 80, 50);

The statement given below displays only the part of the ellipse, because third and fourth arguments(i.e. start and end angles) are given ‘60’ and ‘270’.

ellipse(maxx/2, maxy/5, 60, 270, 80, 50);

Function putpixel() has been used to display the center of the ellipses.

Function fillellipse() draws an ellipse then fills the ellipse with the current fill color and fill pattern.

Declaration:

void far fillellipse(int x, int y, int xradius, int yradius);

The syntax for t:he calling of this function is given below-

fillellipse(x, y, xradius, yradius);

x’ and ‘y’ are the coordinates of the center, ‘xradius’ is the horizontal axis, and ‘yradius’ is the vertical axis of the ellipse. 

In the given program, the statement given below draws an ellipse which is filled with current fill color and fill pattern. On my computer it is solid white fill.

fillellipse(maxx*3/4, maxy/5, 80, 50);

Function setfillstyle() sets the fill pattern and color.

Declaration:

void far setfillstyle(int pattern, int color);

For the first argument ‘pattern’, integer value 0-12 can be given. You can give the names of fill patterns in capital letters too, as I have given ‘LINE_FILL’.

The following list shows the names of fill patterns(constants) along with their integer value as defined in the “graphics.h” file.

Name                                               Value
EMPTY_FILL                                     0
SOLID_FILL                                       1
LINE_FILL                                          2
LTSLASH_FILL                                  3
SLASH_FILL                                      4
BKSLASH_FILL                                 5
LTBKSLASH_FILL                             6
HATCH_FILL                                      7
XHATCH_FILL                                    8
INTERLEAVE_FILL                            9
WIDE_DOT_FILL                              10
CLOSE_DOT_FILL                           11
USER_FILL                                       12

Value 0-11 are the standard fill patterns. Value ‘12’ or ‘USER_FILL’ is used, when you have to use your own fill pattern. How to make custom-defined fill pattern, I’ll show you later on.

For the value of colour, I have already explained this in the previous article.
Syntax for calling this function is given below-

setfillstyle(pattern, color);

Therefore, the statement given below will set the pattern ‘LINE_FILL’ and color ‘CYAN’, for the following enclosed graphics.

setfillstyle(LINE_FILL, CYAN);

Therefore, the ellipse drawn by the statement:

fillellipse(maxx/5, maxy/2, 80, 50);

will be filled by the above fill pattern and colour.

Now, explanation of the given program is complete. For the remaining functions and coding, please refer my previous posts.

OUTPUT: