Bar3d() function in C Graphics
The bar3d function is used to create a 2-dimensional filled-in rectangular bar. We can also create three-dimensional shapes in C using helpful function. The first step in creating this 3D bar is to start a graph. It is necessary to call the bar3d() function after starting the graph. There are six parameters that should all be numbers for this bar3d() function.
To draw the bar, you need to know the coordinates of its left top and right bottom corners. Left specifies the X-coordinate of the top left corner. Top specifies the Y-coordinate of the top left corner. Right specifies the X-coordinate of the right bottom corner. Bottom specifies the Y-coordinate of the right bottom corner. Depth specifies the depth of the bar in pixels. Top flag specifies whether a three-dimensional top is placed on the bar (if it is non-zero then it is placed otherwise not).
The bar is filled using the current fill pattern and fill colour. Use setfillstyle to alter the fill colour and pattern. So, the smallest element on the graphics display screen is a pixel or a dot.
Syntax:
void bar3d(int top flag, int depth, int left, int top, int right, int bottom);
where,
left indicates the top-left corner's X coordinate,
top indicates the top left corner's Y coordinate,
right indicates the right bottom corner's X-coordinate,
bottom indicates the right bottom corner's Y coordinate,
depth indicates the pixel depth of the bar,
The top flag controls whether a three-dimensional top is worn.
or whether the bar (if it is non-zero then it is put otherwise not).
Example:
Input: left = 150, top=250,
Right=190, bottom=350,
Depth=20, top flag=1
left = 220, top=150,
Right=260, bottom=350,
Depth=20, top flag=0
left = 290, top=200,
Right=330, bottom=350,
Depth=20, top flag=1
Output:
The implementation of the bar3d() method is shown below:
// Using the bar3d() method in C
#include <graphics.h>
// driver's code
intmain()
{
// Graphics mode, or gm, is
// a setting on a computer display
// image is produced using pixels.
// A macro named DETECT is defined in
// "header file "graphics.h"
intgd = DETECT, gm;
// the initialization of the
// during the loading of a
// driver for graphics from disc
initgraph(&gd, &gm, "");
// place of sides
int left, top, right, bottom;
// the bar's depth
int depth;
// A top flag indicates the top line.
inttopflag;
// top, left, right, and bottom,
// topflag locations, depth
bar3d(left = 150, top = 250,
right = 190, bottom = 350,
depth = 20, topflag = 1);
bar3d(left = 220, top = 150,
right = 260, bottom = 350,
depth = 20, topflag = 0);
bar3d(left = 290, top = 200,
right = 330, bottom = 350,
depth = 20, topflag = 1);
// vertical axis
Line(100, 50, 100, 350);
// Horizontal axis
line(100, 350, 400, 350);
getch();
// The close graph function ends
// and deals located in graphics mode
// every RAM space allocated by
// graphic design system.
closegraph();
return 0;
}
Output:
When creating games and puzzles, these 3D bars and shapes enable us to create incredibly imaginative and intricate shapes. The graphics.h library-using application makes extensive use of these shapes.
The graphics.h library should always be included in order to use these functions.
Example of the C function bar3d() in graphics.h
#include<graphics.h>
#include<conio.h>
int main()
{
//launching the graphics driver
//graphical mode switch
int graphicdriver=DETECT,graphicmode;
//the initgraph command
initgraph(&graphicdriver,&graphicmode,"c:\\turboc3\\bgi");
//printing a user message
outtextxy(10, 10 + 10, "Program to draw cubes of diffrent sizes in C graphics");
//Making use of the bar3d function, I created both cubes.
bar3d(200, 200, 200, 300, 50, 1);
bar3d(100, 200, 200, 300, 25, 1);
getch();
return 0;
}
Output:
C program to draw 3D bar graph using graphics
#include <graphics.h>
#include <conio.h>
intmain() {
intgd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
settextstyle(BOLD_FONT,HORIZT_DIR,2);
outtextxy(275,0,"3D BAR GRAPH");
setlinestyle(SOLID_LINE,0,1);
/* Printing of X and Y Axis of graph */
line(90,420,80,50);
line(80,410,580,410);
line(85,60,90,50);
line(95,60,80,50);
line(585,410,580,410);
line(575,415,560,410);
outtextxy(70,60,"Y");
outtextxy(575,420,"X");
outtextxy(65,415,"O");
/* Printing of 3D bars */
setfillstyle(XHATCH_FILL, RED);
bar3d(160,80,250,410, 15, 1);
bar3d(225,100,275,400, 15, 1);
bar3d(300,130,360,410, 15, 1);
bar3d(365,170,425,410, 15, 1);
bar3d(450,125,590,410, 15, 1);
getch();
closegraph();
return 0;
}
Output: