Introduction :
In C or C++ graphics programming language, rectangle is a basic shape that is used in many applications. There is a rectangle() function in graphics.h library which is used to create rectangles. In this article, I shall show you how to create rectangle by adding lines using line() function in C Graphics programming language. Here, you can see an animated program of rectangle using graphics.h library in Turbo C++ IDE.
About the program :
In the program, I show you how to create rectangle one by one using line() function of Graphics.h library. Here, you can see the drawing of lines one by one and add all the line with each other to form rectangles.
Explanation of the program :
You have to include graphics.h, conio.h and dos.h in the program. Then, declare integer variables (xmax, ymax, x1, x2, y1, y2, y, i, x, graphic_driver and graphic_mode) in the main() function. After that, initialize the graphics mode using initgraph() function. You can use setbkcolor() and setcolor() functions to set the background and drawing color respectively.
You have to use getmaxx() and getmaxy() functions to get the maximum x and y coordinates of the screen. Using line() function, draw the top, bottom, left and right edges of the rectangles. To create an animation effect, use delay() function. To get a key press, you can call getch() function. At last, close the graphics mode using closegraph() function.
How run the program :
If you run this program, first install Turbo C++ IDE on your PC. Now, create a C or C++ file in Turbo C++ IDE. Then, copy the below source code and paste in your C or C++ file. If you do not know how to copy paste in Turbo C++ IDE, click here. Do you know how to use graphics.h in Turbo C++ IDE?
Source code of the program :
The below code is the source code of animation program that creates a series of rectangles one by one using line() function in C or C++ programming language. Here, I shall give the source code for this program. Just, copy the below code and use it in your own project.
/* Developed by Puskar Jasu */
#include <graphics.h>
#include <conio.h>
#include <dos.h>
int main(void)
{
int xmax, ymax, x1, x2, y1, y2, y, i, x;
int graphic_driver = DETECT, graphic_mode;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
setbkcolor(5);
setcolor(1);
xmax = getmaxx();
ymax = getmaxy();
x1 = xmax / 2 - 10;
x2 = xmax / 2 + 10;
y = ymax / 2;
for (i = 1; i <= 20; i++)
{
line(x1, y, x2, y);
x1 = x1 - 10;
x2 = x2 + 10;
y = y - 10;
delay(300);
}
x1 = xmax / 2 - 10;
x2 = xmax / 2 + 10;
y = ymax / 2 + 10;
for (i = 1; i <= 20; i++)
{
line(x1, y, x2, y);
x1 = x1 - 10;
x2 = x2 + 10;
y = y + 10;
delay(300);
}
x = xmax / 2 + 10;
y1 = ymax / 2;
y2 = ymax / 2 + 10;
for (i = 1; i <= 20; i++)
{
line(x, y1, x, y2);
y1 = y1 - 10;
y2 = y2 + 10;
x = x + 10;
delay(300);
}
x = xmax / 2 - 10;
y1 = ymax / 2;
y2 = ymax / 2 + 10;
for (i = 1; i <= 20; i++)
{
line(x, y1, x, y2);
y1 = y1 - 10;
y2 = y2 + 10;
x = x - 10;
delay(300);
}
getch();
closegraph();
return 0;
}
Output of the program :
When you run the program on your PC, you can see the output of the program. You can see the output in my YouTube channel.
Conclusion :
In this program, I show you how to create rectangle one by one using line() function of C or C++ graphics programming language. Thank you for visiting my site.