×

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

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

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

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.