×

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:

Bar3d() function in C Graphics

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:

Bar3d() function in C Graphics

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:

Bar3d() function in C Graphics

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:

Bar3d() function in C Graphics

Related Topics

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

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

3 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.