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:







                                                     


No comments:

Post a Comment

Please give your comment