Saturday, February 25, 2017

C Progarm to explain functions moveto() and lineto()

Graphics in C - Demonstration of Functions moveto() and lineto()


In the previous post, lines have been drawn using function line(). The coordinates passed to this function were with respect to the origin (0, 0), which was the top-left corner of the screen. While drawing the line by using function line(), C.P.(Current Position) does not change. So far we have used the functions that do not change the C.P.  Coordinates passed to these functions are always with respect to the point, named origin (0, 0). However, there are a few functions, in which C.P. changes, and next graphic starts from C.P., not from origin. Function lineto() draws a line from C.P. to the specified location on the screen and moves the C.P. to its end point. Function moveto() moves the C.P. to the specified point.   To clear the point, please type the program given below in your editor. This program demonstrates functions moveto() and lineto().

#include<graphics.h>

void main()
{
    int gd = DETECT, gm, maxx, maxy;
    initgraph(&gd, &gm, "c:\\tc\\bgi");

    /* Sets the color of lines */
    setcolor(WHITE);

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

    /* Displays the message on screen */
    outtextxy(maxx/2-150, maxy/4-70, "Demonstration of moveto() and lineto()");

    /* Moves the C.P. to specified point */
    moveto(maxx/2, maxy/4-35);

     /* Draw the lines from C.P. to the specified
        point and move the C.P. to that specified
        point */
    lineto(maxx/4, maxy*3/4);
    lineto(maxx*3/4, maxy*3/4);
    lineto(maxx/2, maxy/4-35);
    moveto(maxx/4, maxy/4+50);
    lineto(maxx*3/4, maxy/4+50);
    lineto(maxx/2, maxy*3/4+85);
    lineto(maxx/4, maxy/4+50);

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

    /* Fills the enclosed area with current
       fill-style and color */
    floodfill(maxx/2+1, maxy/4-30, WHITE);

    setfillstyle(LINE_FILL, BLUE);
    floodfill(maxx/4+4, maxy*3/4-2, WHITE);
    setfillstyle(SLASH_FILL, GREEN);
    floodfill(maxx*3/4-2, maxy*3/4-1, WHITE);
    setfillstyle(HATCH_FILL, MAGENTA);
    floodfill(maxx/4+3, maxy/4+53, WHITE);
    setfillstyle(INTERLEAVE_FILL, YELLOW);
    floodfill(maxx*3/4-2, maxy/4+51, WHITE);
    setfillstyle(WIDE_DOT_FILL, CYAN);
    floodfill(maxx/2, maxy*3/4+75, WHITE);
    setfillstyle(XHATCH_FILL, LIGHTBLUE);
    floodfill(maxx/2, maxy/2, WHITE);

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

Compile and execute this program. The output of this program will be a star filled with different patterns in different colors in different bounding regions.

To draw the star, I have used functions moveto() and lineto() instead of function line() or function drawpoly().

Function moveto() moves the current position(C.P.) to a desired position (x, y).

Declaration:

void far moveto(int x, int y);

Where (x, y) is the coordinates of the desired location of the view port.

Syntax for calling this function is given below.

moveto(x, y);

Function lineto() draws a line from the current position(C.P.) to the point (x, y). After drawing the line it moves the current position to the location (x, y).

Declaration:

void far lineto(int x, int y);

Where (x, y) is the coordinates of the point, to which line has to be drawn.

Syntax for calling this function is given below.

lineto(x, y);

In the given program, I have moved the current position(C.P.) to the point (maxx/2, maxy/4-35) by the statement given below.

moveto(maxx/2, maxy/4-35);

Then I have drawn three lines by the statements given below, and formed a triangle.

 lineto(maxx/4, maxy*3/4);
 lineto(maxx*3/4, maxy*3/4);
 lineto(maxx/2, maxy/4-35);

The same way, I have drawn another triangle by the following statements.

 moveto(maxx/4, maxy/4+50);
 lineto(maxx*3/4, maxy/4+50);
 lineto(maxx/2, maxy*3/4+85);
 lineto(maxx/4, maxy/4+50);

And finally a star has been formed by combining these two triangles. As a result, seven bounded regions are formed, and then I have filled them by using  functions setfillstyle() and floodfill().

Now, the explanation of function moveto() and function lineto() is complete. For the explanation of remaining functions and terms, please refer my previous posts.

Monday, February 20, 2017

C Program to explain different line-styles

Graphics in C - Predefined and User-defined Line Styles


In the previous posts, we have drawn only solid lines. Sometimes we may want to draw lines in other styles. In C programming language, different types of lines can be drawn. Besides solid lines, you can draw dotted lines, center lines, dashed lines, and user-defined lines. I have described these line styles in this post. Please type this program in your editor.

#include<graphics.h>
void main()
{
     /* Declares and initializes array of pointers
        to string */
    char *style[5]={
                        "SOLID_LINE style(value = 0)",
                        "DOTTED_LINE style(value = 1)",
                        "CENTER_LINE style(value = 2)",
                        "DASHED_LINE style(value = 3)",
                        "USERBIT_LINE style(value = 4)"
                      };
    int i, x1=100, y1=125, x2=250, y2=100;
    int upattern = 0xAA; /* bit pattern: 1010 1010 */
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "c:\\tc\\bgi");

    /* Displays the message */
    outtextxy(200, 50, "Line Styles");

    for(i=0; i<=4; i++)
    {
          if(i==4)
          {
                 setlinestyle(i, upattern, 1);
          }
          else
          {
                 setlinestyle(i, 0, 1);
          }
          line(x1, y1, x1+100, y1);
          rectangle(x2, y2, x2+100, y2+50);
          outtextxy(x2+150, y2+25, style[i]);
          y1+=60;
          y2+=60;
    }
    getch();
    closegraph();
    restorecrtmode();
}

Compile and execute this program. In the output, you will see one set of five lines and another set of five rectangles in different line styles. For different line styles, I have used the function setlinestyle(). Function setlinestyle() sets the current line style, line pattern, and line width. Line pattern is the user-defined pattern. This function sets the style for all lines drawn by functions line(), rectangle(), drawpoly(), etc.

Declaration:

void far setlinestyle(int linestyle, unsigned upattern, int thickness);

Syntax for calling this function is given below-

setlinestyle(linestyle, upattern, thickness);

Where’ linestyle’ is either system-defined or user-defined, depending on the value of ‘linestyle’. Its values can be an integer from 0 to 4. You can give the name of constants instead of integer values. The names of constants and their corresponding integer values are given below-

Name of constant               Integer value
SOLID_LINE                                   0
DOTTED_LINE                               1
CENTER_LINE                               2
DASHED_LINE                               3
USERBIT_LINE                              4

Value ‘4’ is for user-defined line style. ‘upattern’ is user-defined pattern, which is given, when you give value ‘4’ for line-style, otherwise it is set to ‘0’.  ‘thickness’ is the width of line, and its value can be given from 1 to 3. If you give the invalid values to this function, the current line style remains unchanged.

In the above program, I have used a ‘for’ loop, which iterates through loop variable i = 0 to 4.  In the ‘for’ loop, an ‘if’ construct selects a ‘setlinestyle’ statement on the basis of the value of ‘i’. If the value of ‘i’ is 4, then line-style is USERBIT_LINE(means user-defined line-style), the statement setlinestyle(i, upattern, 1);  will be executed. ‘upattern’ has been given the hexadecimal value ‘0xAA’ (bit pattern 1010 1010), and thickness of line is ‘1’. If the value of  ‘i’ is from 0 to 3, statement setlinestyle(i, 0, 1); will be executed. This time, value of ‘upattern’ has been set to ‘0’, and ‘thickness’ of line is again ‘1’. On the basis of settings of function ‘setlinestyle(), lines and rectangles have been drawn. Function outtextxy() has been used to describe the line-styles.


Thus, this simple program describes the use of function setlinestyle(). For the remaining functions and other things, please refer my previous posts.

OUTPUT:






Monday, February 13, 2017

C Program to create the user-defined fill-pattern

Graphics in C - Demonstration of User-defined fill-pattern


In C programming language we can give fill-patterns by two methods. One is standard system defined, and other is user-defined. I have described function setfillstyle() previously. Function setfillstyle() fills the standard predefined fill patterns. If you want to give user-defined fill patterns, you can use  function setfillpattern(). Here is a simple program given below. Please type this program in your editor.

#include<graphics.h>

void main()
{
         /* Declares and initializes array with
            user-defined bit-pattern */
         char patt[] = {0x0F, 0xF0, 0x0F, 0xF0, 0x0F, 0xF0, 0x0F, 0xF0};

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

         /* Sets the user-defined fill-pattern and color */
         setfillpattern(patt, RED);

          /* Draws a rectangle */
         rectangle(100, 100, 400, 400);

         /* Fills the rectangle with user-defined
            fill-pattern and color */
         floodfill(101, 101, WHITE);

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

Compile and execute this program. The output will be a rectangle filled with nice checker red pattern. To create user-defined pattern, I have used the function setfillpattern()

Declaration:

void far setfillpattern(char far *upattern, int color);

Where ‘upattern’ is a pointer to a sequence of ‘8’ bytes, each byte consists of 8 pixels. You can store this 8 byte sequence of pattern in an array. In every byte, if a bit is set to ‘1’, a pixel will be drawn, and if a bit is set to ‘0’, pixel will not be drawn. ‘colour’ parameter specifies the colour of user-defined pattern.

Syntax for calling this function is given below-

setfillpattern(upattern, color);

In the given program, an array ‘patt[8]’ has been declared and initialized with 2 hexadecimal numbers ‘0x0F’ and ‘0xF0’ in alternating fashion (0x0Fà0000 1111 and 0xF0à 1111 0000).  Number of array elements is 8, which denotes to 8 bytes. Therefore, wherever a bit is ‘1’, a pixel will be drawn in given colour, which is ‘RED’ in the given program. Therefore, a pattern of bits will be like this-

      0000 1111
      1111 0000
      0000 1111
      1111 0000
      0000 1111
      1111 0000
      0000 1111
      1111 0000

The statement given below will set a checker like pattern in red color. ‘patt’ is the address of array ‘patt[8]’.

setfillpattern(patt, RED);

Since, function rectangle() is not automatically filled with given fill pattern, I have used the function floodfill().

floodfill(101, 101, WHITE);


This is the description of using user-defined fill patterns. For the description of other functions, and constants, please refer my previous posts.

OUTPUT:






You would also like these programs given below:



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: