In the previous article, we discussed some BGI library functions, namely, line(), getx(), linerel(), circle(), arc() etc. In the present article, we shall discuss some more BGI drawing functions. These functions draw various shapes like, rectangle, bar, ellipse etc.
This article discusses the following functions :
a) void rectangle (int left, int top, int right, int bottom)
b) void bar (int left, int top, int right, int bottom)
c) void bar3d (int left, int top, int right, int bottom, int depth, int topflag)
d) void ellipse (int xc, int yc, int stangle, int endangle, int xradius, int yradius)
e) void fillellipse (int xc, int yc, int xradius, int yradius)
f) void sector(int x, int y, int stangle, int endangle, int xradius, int yradius)
g) void drawpoly (int numpoints, int *polypoints)
h) void fillpoly (int numpoints, int *polypoints)
a) void rectangle (int left, int top, int right, int bottom)
This function draws a rectangle with (left, top) and (right, bottom) as corners.
Example 1 :
#include <graphics.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
setcolor(GREEN);
rectangle(200, 200, 400, 300);
rectangle(100, 100, 500, 400);
getch();
closegraph();
return 0;
}
Output of this rectangle() program
Example 2 :
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
while(!kbhit())
{
setcolor(random(getmaxcolor()));
rectangle(random(getmaxx()), random(getmaxy()), random(getmaxx()), random(getmaxy()));
}
getch();
closegraph();
return 0;
}
Output of this rectangle() program
b) void bar (int left, int top, int right, int bottom)
This function draws a filled-in two-dimensional rectangular bar. It does not draw the bar boundary. The rectangle is drawn using the current fill pattern and color, with (left, top) as the top-left corner and (right, bottom) as the bottom-right corner.
For example,
#include <graphics.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
setcolor(GREEN);
bar(200, 200, 400, 300);
getch();
closegraph();
return 0;
}
Output of this bar() program
c) void bar3d (int left, int top, int right, int bottom, int depth, int topflag)
This function draws a three-dimensional rectangular bar, then fills it using the current fill pattern and color. The three-dimensional outline of the bar is drawn in the current line style and color.
The values stored by its arguments have been listed below :
# depth : Bar's depth in pixels
# topflag : Governs whether a three-dimensional top is put on the bar
# (left, top) : Rectangle's upper left corner
# (right, bottom) : Rectangle's lower right corner
If topflag is non-zero, a top is put on the bar. If topflag is 0, no top is put on the bar : This makes it possible to stack several bars on top of one another.
For example,
#include <graphics.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
setcolor(GREEN);
bar3d(200, 200, 400, 300, 30, 1);
getch();
closegraph();
return 0;
}
Output of this bar3d() program
d) void ellipse (int xc, int yc, int stangle, int endangle, int xradius, int yradius)
This function draws an ellipse with (xc, yc) as centre. The starting angle and the ending angle are represented by stangle and endangle respectively. The semi-major axis is xradius and the semi-minor axis is yradius.
If stangle = 0 and endangle = 180, then only the upper half of the ellipse will be drawn.
If stangle = 0 and endangle = 360, the call to ellipse draws a complete ellipse.
For example,
#include <graphics.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
ellipse(300, 200, 0, 360, 45, 135);
getch();
closegraph();
return 0;
}
Output of this ellipse() program
e) void fillellipse (int xc, int yc, int xradius, int yradius)
This function creates a filled-up elliptical region with (xc, yc) as centre and xradius and yradius as the horizontal and vertical axes, and fills it with the current fill color and fill pattern.
For example,
#include <graphics.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
fillellipse(300, 200, 60, 135);
getch();
closegraph();
return 0;
}
Output of this fillellipse() program
f) void sector(int x, int y, int stangle, int endangle, int xradius, int yradius)
This function draws and fills an elliptical pie slice in the current drawing color, then fills it using the pattern and color defined by setfillstyle or setfillpattern.
The values stored by its arguments have been listed below :
# (x,y) : Center of ellipse
# xradius : Horizontal axis
# yradius : Vertical axis
# stangle : Starting angle
# endangle : Ending angle
For example,
#include <graphics.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
sector(300, 300, 45, 135,200,150);
getch();
closegraph();
return 0;
}
Output of this sector() program
g) void drawpoly (int numpoints, int *polypoints)
This function draws a polygon. The number of points in the polygon are represented by numpoints and polypoints is the base address of the array containing the list of coordinate points.
Each pair of integers gives the x and y coordinates of a point on the polygon.
To draw a closed figure with N vertices, you must pass N+1 coordinates to drawpoly, where the Nth coordinate equals to the 0th coordinate.
For example,
h) void fillpoly (int numpoints, int *polypoints)
This function creates a filled-up polygonal region where numpoints is the number of vertices of the polygon and polypoints is the base address of the array containing the list of coordinate points.
For example,
For example,
#include <graphics.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
int poly[12] = {200, 100, 200, 300, 300, 350, 400, 300, 320, 190, 200, 100};
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
drawpoly(6, poly);
getch();
closegraph();
return 0;
}
Output of this drawpoly() program
h) void fillpoly (int numpoints, int *polypoints)
This function creates a filled-up polygonal region where numpoints is the number of vertices of the polygon and polypoints is the base address of the array containing the list of coordinate points.
For example,
#include <graphics.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
int poly[12] = {200, 100, 200, 300, 300, 350, 400, 300, 320, 190, 200, 100};
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
fillpoly(6, poly);
getch();
closegraph();
return 0;
}
Output of this fillpoly() program
No comments:
Post a Comment