BGI library functions – Part 2 (Drawing Functions)

By | July 11, 2015
In the previous article, we discussed some BGI library functions, namely, putpixel(), getpixel(), setcolor() etc. In the present article, we shall discuss some BGI library drawing functions. These functions draw various shapes like, circle, arc, line etc.

BGI Library Drawing Functions

These functions are listed below :

a) circle (int x, int y, int r)
b) line (int x1, int y1, int x2, int y2)
c) arc (int xc, int yc, int stangle, int endangle, int r)
d) rectangle (int left, int top, int right, int bottom)
e) bar (int left, int top, int right, int bottom)
f) bar3d (int left, int top, int right, int bottom, int depth, int topflag)

a) circle (int x, int y, int r)

This function draws a circle with centre (x, y) and radius r.

Example 1 :

```
#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "c://turboc3//bgi");
setcolor(RED);
circle(320, 240, 70);
getch();
closegraph();
return 0;
}

```

Output of circle() Example 1 Example 2 :

```
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>

int main()
{
int gd = DETECT, gm, x, y, r = 50, color = 1;
initgraph(&gd, &gm, "c://turboc3//bgi");
while (!kbhit())
{
if (color == 15)
color = 1;
x = random (getmaxx());
y = random (getmaxy());

/* getmaxx() and getmaxy() return the maximum value of x or y screen coordinate. For example, in a CGA in 320 * 200 mode, getmaxx() returns 319 and getmaxy() returns 199 */

setcolor (color);
circle (x, y, r);
color++;
}
closegraph();
return 0;
}

```

Output of circle() Example 2 b) line (int x1, int y1, int x2, int y2)

This function draws a line from (x1, y1) to (x2, y2).

Example :

```
#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "c://turboc3//bgi");
setcolor(RED);
line (100, 100, 300, 300);
getch();
closegraph();
return 0;
}

```

Output of line() Example c) arc (int xc, int yc, int stangle, int endangle, int r)

This function draws an arc with (xc, yc) as starting point, r as radius and stangle and endangle as starting and ending angles respectively.

Example :

```
#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "c://turboc3//bgi");
setcolor(RED);
arc (300, 200, 45, 135, 70);
getch();
closegraph();
return 0;
}

```

Output of arc() Example d) rectangle (int left, int top, int right, int bottom)

This function draws a rectangle with (left, top) and (right, bottom) as corners.

Example :

```
#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "c://turboc3//bgi");
setcolor(RED);

rectangle (100, 100, 300, 200);
getch();
closegraph();
return 0;
}

```

Output of rectangle() Example e) 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 colour, with (left, top) as the top-left corner and (right, bottom) as the bottom-right corner.

Example :

```
#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "c://turboc3//bgi");
setcolor(RED);
bar (100, 100, 300, 200);
getch();
closegraph();
return 0;
}

```

Output of bar() Example f) bar3d (int left, int top, int right, int bottom, int depth, int topflag)

This function draws a three-dimensional rectangular bar where (left, top) is the upper left corner and (right, bottom) is the lower right corner. The bar’s depth is represented by depth and lastly, the value of topflag decides whether a three-dimensional top is put on the bar. If topflag is 0, no top is put on the bar.

Example :

```
#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "c://turboc3//bgi");
setcolor(RED);
bar3d (100, 100, 300, 200, 30, 1);
getch();
closegraph();
return 0;
}

```

Output of bar3d() Example In the next article, we have discussed more drawing functions.

BGI library functions – Part 3 (More Drawing Functions)