Introduction :
There are various types of shapes used to create graphics programming in C or C++ language. One of them is rectangle shape. You can use the rectangle() function of graphics.h library to draw a rectangle on the graphics screen. In this article, I shall show you how to draw rectangle() function using the C or C++ graphics programming language. Here, I also show you the syntax and usage of rectangle() function.
What is the rectangle() function :
The rectangle() function is a popular function of the graphics.h library in Turbo C++IDE. It is used to draw a rectangle using C or C++ programming language. It takes the coordinates of the top left and bottom right corners as parameters.
Syntax of the rectangle() function :
The following code is the syntax of the rectangle() function in C or C++ programming language.
void rectangle(int l, int t, int r, int b);
The rectangle() function does not return any value. It requires four parameters. The l and t parameters are the x and y coordinate of the top left corner of the rectangle. The r and b parameters are the x and y coordinate of the bottom right corner of the rectangle.
About the program :
This is an example of using rectangle() function in C or C++ programming language. When you run the program, you see a black color graphical window will open. At the same time, you see a red color rectangle on the graphics screen. You have to press any key to stop the program.
Explanation of the program :
In the program, you have to include the graphics.h and conio.h header files. Then, declare integer variables such as “l”, “t”, “r”, “b”, “graphic_driver” and “graphic_mode” in the main function. The “l” and “t” are the coordinates of the top left corner of the rectangle. The”r” and “b” are the coordinates of the bottom right corner of the rectangle.
You can use the initgraph() function to initialize the graphics mode with the graphics driver and mode. Now, set the current drawing color using setcolor() function. Then, you can call the rectangle() function with appropriate parameters. You have to use getch() function to get a key from users. To close the graphics mode you can use closegraph() function.
How run the program :
To run the following program, first install the Turbo C++ IDE on your pc. After that, open it and create a C or C++ file. Now, copy the below code and paste in your file. You can follow my link to know how to copy paste in the Turbo C++ IDE. Do you know how to use graphics.h in Turbo C++ IDE?
Source code of the program :
The following code is used for rectangle() function in C or C++ programming language.
/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
int main()
{
int l = 200, t = 100, r = 400, b = 300;
int graphic_driver = DETECT, graphic_mode;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
setcolor(4);
rectangle(l, t, r, b);
getch();
closegraph();
return 0;
}
Output of the program :
You can see the output of the program like below image on your PC.

Conclusion :
At last, you have learned how to use rectangle() function to draw rectangle using C or C++ language. Now you can use rectangle() function in your program. Thank you for visiting my site.