Saturday, March 11, 2017

C Program to draw an elliptical pie-slice using sector()

Graphics in C-Draw Elliptical Pie


There are two more functions, which I am going to describe now. Please type this program in your editor.

#include<graphics.h>
void main()
{
          int gd = DETECT, gm, maxx, maxy;
          initgraph(&gd, &gm, "c:\\tc\\bgi");
          maxx = getmaxx();
          maxy = getmaxy();
          moveto(maxx/2-150, maxy/4-50);
          outtext("Demonstration of sector() & outtext()");
          setfillstyle(LINE_FILL, RED);
          sector(maxx/4-50, maxy/2, 30, 250, 100, 60);
          setfillstyle(SOLID_FILL, BLUE);
          sector(maxx/2, maxy/2, -30, -250, 100, 60);
          setfillstyle(HATCH_FILL, GREEN);
          sector(maxx*3/4+50, maxy/2, 0, 360, 100, 60);
          getch();
          closegraph();
          restorecrtmode();
}

Compile and execute this program. The output will be a text string, telling the name of functions which I’ll describe now, and three graphics, the variations of elliptical shapes.

First, I have obtained maximum screen coordinates by these statements.

maxx = getmaxx();
maxy = getmaxy();

Then, function moveto() moves the current position(C.P.) to the point (maxx/2-150, maxy/4-50).

moveto(maxx/2-150, maxy/4-50);

For displaying the text string on the monitor, I have used the function outtext(). Function outtext() displays a string in the viewport on the current position, using the current justification settings, current font, current direction, and current size.

Declaration:

void far outtext(char far *textstring);

Syntax for calling this function is:

outtext(text-string);

Therefore the statement:

outtext("Demonstration of sector() & outtext()");

outputs the string "Demonstration of sector() & outtext()" on the monitor, in the current position, which I have set by the function moveto().

Now, something about the elliptical shapes…

The three elliptical shapes have been drawn by using the function sector(). Function sector() draws and fills an elliptical pie slice in the current drawing color, then fills it using the color and pattern defined by function setfillstyle() or function setfillpattern().

Declaration:

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

where (x, y) is the center of the elliptical pie, ‘stangle’ is the starting angle in degrees, ‘endangle’ is the ending angle in degrees, ‘xradius’ is the horizontal axis in pixels, and ‘yradius’ is the vertical axis in pixels.

Syntax for calling this function is:

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

Therefore, the statements:

setfillstyle(LINE_FILL, RED);
sector(maxx/4-50, maxy/2, 30, 250, 100, 60);

draw an elliptical pie filled with red lined pattern, whose center is ‘(maxx/4-50, maxy/2)’, start-angle is 30 degrees, end-angle is 250 degrees, x-radius is 100 pixels, and y-radius is 60 pixels.

If start-angle and end-angle are given positive(+ve), elliptical pie is drawn in anticlockwise direction.

If these angles are given negative(-ve), elliptical pie is drawn in clockwise direction. See these statements:

setfillstyle(SOLID_FILL, BLUE);
sector(maxx/2, maxy/2, -30, -250, 100, 60);

If only one of these angles is given negative, what happens, please experiment yourself.

If start-angle is given ‘0’, and end-angle is given ‘360’, a full ellipse is drawn. Please see these statements:

setfillstyle(HATCH_FILL, GREEN);
sector(maxx*3/4+50, maxy/2, 0, 360, 100, 60);

Function setfillstyle() has been used to set the fill pattern, and fill color.

Now, the description of function outtext(), and function sector() is complete. For description of the remaining functions and terms, please refer my previous posts.

OUTPUT:




No comments:

Post a Comment

Please give your comment