BGI library – Part 3 (BGI Drawing Functions)

  In the previous article, we discussed several drawing functions from the list of BGI library functions. In this one, we shall discuss some more BGI drawing functions. Some of these functions draw ellipses and polygons while others are used for filling a given area with the specified color.

BGI drawing functions

These functions are listed below :


a) ellipse (int xc, int yc, int stangle, int endangle, int xradius, int yradius)
b) fillellipse (int xc, int yc, int xradius, int yradius)
c) drawpoly (int numpoints, int *polypoints)
d) fillpoly (int numpoints, int *polypoints)
e) setfillstyle (int pattern, int color)
f) floodfill (int x, int y, int border_color)
g) cleardevice ()

a) 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.

Example :

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

int main()
{
  int gd = DETECT, gm;
  initgraph (&gd, &gm, “c://turboc3//bgi”);
  ellipse (300, 200, 0, 360, 135, 45);
  getch();
  closegraph();
  return 0;
}

Output of ellipse() Example

BGI Drawing Functions - Output of ellipse() Example

b) fillellipse (int xc, int yc, int xradius, int yradius)

This function creates a filled-up elliptical region with (xc, yc) as centre and xradius/yradius as the two axes.

Example :

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

int main()
{
  int gd = DETECT, gm;
  initgraph (&gd, &gm, “c://turboc3//bgi”);
  fillellipse (300, 200, 45, 135);
  getch();
  closegraph();
  return 0;
}

Output of fillellipse() Example

Output of fillellipse() Example

c) 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.

Example :

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

int main()
{
  int gd = DETECT, gm;
  int poly[12] = {250, 150, 250, 300, 300, 350, 400, 300, 320, 190, 250, 150};

  initgraph (&gd, &gm, “c://turboc3//bgi”);
  drawpoly (6, poly);
  getch();
  closegraph();
  return 0;
}

Output of drawpoly() Example

Output of drawpoly() Example

d) 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.

Example :

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

int main()
{
  int gd = DETECT, gm;
  int poly[12] = {250, 150, 250, 300, 300, 350, 400, 300, 320, 190, 250, 150};
  initgraph (&gd, &gm, “c://turboc3//bgi”);
  fillpoly (6, poly);
  getch();
  closegraph();
  return 0;
}

Output of fillpoly() Example

Output of fillpoly() Example
 e) setfillstyle (int pattern, int color)

This function sets the current fill pattern and fill color. Fill patterns for this functions are given below :

Fill patterns for setfillstyle()

f) floodfill (int x, int y, int border_color)

This function fills an enclosed area on bitmap devices. The area bounded by the border_color is ‘flooded’ with the current fill pattern and fill color. (x, y) is a ‘seed point’. If this seed is inside the enclosed area, the inner region of that enclosure will be filled. In case it is outside the enclosed area, the region outside the enclosure will be filled.

Example :

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

int main()
{
  int gd = DETECT, gm;
  initgraph (&gd, &gm, “c://turboc3//bgi”);
  setcolor (WHITE);
  setfillstyle (SOLID_FILL, LIGHTRED);
  circle (320, 240, 100);
  floodfill (320, 240, WHITE);
  getch();
  closegraph();
  return 0;
}

Output of floodfill() Example

Output of floodfill() Example

g) cleardevice ()

This function clears the entire screen and moves the current position to (0, 0);

Example :

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

int main()
{
  int gd = DETECT, gm;
  int radius = 20;
  initgraph (&gd, &gm, “c://turboc3//bgi”);
  setcolor (WHITE);
  while (!kbhit())
  {
    if (radius == 200)
      radius = 20;
    circle (320, 240, radius);
    delay (100);
    cleardevice ();
    radius += 10;
  }
  closegraph();
  return 0;
}

Output of cleardevice() Example

Output of cleardevice() Example

The above program shows an animation in which the circle grows to its maximum size and shrinks back to original size repeatedly until a key is pressed on the keyboard.

In the next article, we hav discussed the remaining functions, including those that work with images.

BGI library functions – Part 4 (Image Handling Functions & Linear Drawing Functions)

 

Did you like this article? Tell us in your comments below.


(Visited 413 times, 2 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *