C++ bar() - Draw a Bar Between Two Points

- July 11, 2018
cpp bar library function

In C++, the bar() function is used to draw a bar between two points on the screen. The general syntax of this function is as follows:
bar (left, top, right, bottom);
Where:
left and top represents the x & y co-ordinates of the first point. The "left" indicates x-coordinate of the first point and the "top" indicates the y-coordinate of the first point.
right and bottom represents the x & y co-ordinates of the second point. The "right" indicates x-coordinate of the second point and the "bottom" indicates the y-coordinate of the second point.


These four parameters may be variables of "int" type or values.

Example of C++ bar() Function

The following C++ program draws a bar between two points (200, 100) & (240, 600) on the screen.
#include<iostream>
#include<canio>
#include<graphics>
using namespace std;
  main()
   {
     int driver, mode;
     driver = DETECT;
     initgraph (&driver, &mode, " ");
     cleardevice();
     bar(200, 100, 240, 600);
     getch();
     closegraph();
  }
The following C++ program draws bar graph for different values in different colors and patterns.

#include<iostream>
#include<canio>
#include<graphics>
using namespace std;
  main()
   {
      int d, m, x1, x2, i;
      driver = DETECT;
      initgraph (&driver, &m, " ");
     cleardevice();
     for (i = 1, x1 = 100, x2 = 120; i<=13; i++, x1+=30, x2+= 30)
        {
          setfillstyle (i, i);
          bar (x1, 50, x2, 300);
        }
     getch();
     closegraph();
  }

Leave your reply.
EmoticonEmoticon