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.
You would also like these programs given below:
C Program to draw a line
C Program to draw a circle and a point
C Program to draw different types of ellipses
C Program to draw arc, pie-slice, and rectangle
C Program to draw a polygon
C Program to draw filled polygon, 2-D bar, and 3-D bar
C Program to create the user-defined fill-pattern
C Program to explain different line-styles
C Progarm to explain functions moveto() and lineto()
C Program to explain moverel() and linerel()
C Program to draw an elliptical pie-slice using sector()
C Program to write text in different font styles using settextstyle()
C Program to justify text using settextjustify()
C Program to explain function setusercharsize()
C Program to define palette of colors
C Program to generate captcha code
C Program for moving a ball on screen
C Program for creating animated circles
Playing with mouse using C language
C Program to restrict mouse pointer in a window
C Program for getting the position of mouse pointer
C Program to create a clock
Interrupt Handling With C
C Program to draw a line
C Program to draw a circle and a point
C Program to draw different types of ellipses
C Program to draw arc, pie-slice, and rectangle
C Program to draw a polygon
C Program to draw filled polygon, 2-D bar, and 3-D bar
C Program to create the user-defined fill-pattern
C Program to explain different line-styles
C Progarm to explain functions moveto() and lineto()
C Program to explain moverel() and linerel()
C Program to draw an elliptical pie-slice using sector()
C Program to write text in different font styles using settextstyle()
C Program to justify text using settextjustify()
C Program to explain function setusercharsize()
C Program to define palette of colors
C Program to generate captcha code
C Program for moving a ball on screen
C Program for creating animated circles
Playing with mouse using C language
C Program to restrict mouse pointer in a window
C Program for getting the position of mouse pointer
C Program to create a clock
Interrupt Handling With C
No comments:
Post a Comment
Please give your comment