Introduction :
Creating a design using graphics.h library in C or C++ programming language is very popular in software world. Using graphics programming in C or C++ language, you can create various designs and graphical applications. To create different types of designs on the graphics screen, you can use functions of graphics.h in Turbo C++ IDE.
You can create a variety of designs using C or C++ programming language. In this article, I shall show you how to create various designs in C or C++ programming language. Here, I use the graphics.h library of Turbo C++ IDE for this purpose.
About the program :
In this program, you can see different type of design is created by a C graphics program. Here, different type design is built one by one with different colors and backgrounds. When you run the program, you can see a series of visual effects and animations such as radial lines, random circular designs, expanding and contracting circles and orbiting circles on the graphics screen.
Explanation of the program :
In the program, first include necessary header files such as graphics.h, stdlib.h, conio.h, dos.h and math.h.
You have to declare variables such as “xmax”, “ymax”, “x”, “y”, “i”, “j”, “m”, “n”, “k”, “a”, “b”, “radius”, “graphic_driver” and “graphic_mode”. Then, call initgraph() function to initialize the graphics mode. Using getmaxx() and getmaxy() calculate the center of the graphics screen and store in “x” and “y” variables.
The setbkcolor() and setcolor() functions are used to set the background and shape color respectively. You can use line() and circle() functions to draw the different design on the screen. The delay() function is used to delay the program for animation effect. Using cleardevice() function, clear the entire screen for next design. To get random value, you have to use rand() function.
To get a key press, you can use getch() function. Then, close the graphics mode using closegraph() function.
How run the program :
You can run this program on your PC. To run this program, first install Turbo C++ IDE on your PC. Now create a C or C++ file in Turbo C++ IDE. After that, copy paste the below source code in your C or C++ file. If you do not know how to copy paste in Turbo C++ IDE, follow my link. Do you know how to use graphics.h in Turbo C++ IDE?
Source code of the program :
The following code is the source code of creating different design program in C or C++ programming language. You can copy the code and use in your project.
/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <math.h>
int main(void)
{
int xmax, ymax, x, y, i = 0, j = 0, m = 0, n = 0, k = 0, a, b, radius = 5;
int graphic_driver = DETECT, graphic_mode;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
xmax = getmaxx();
ymax = getmaxy();
x = xmax / 2;
y = ymax / 2;
setbkcolor(1);
while (1)
{
setbkcolor(k + 1);
setcolor(k);
for (n = 0; n <= 350; n++)
{
for (m = 0; m <= 360; m += 10)
{
i = n * cos(m * 3.14159 / 180);
j = n * sin(m * 3.14159 / 180);
line(x, y, x + i, y - j);
}
delay(5);
n++;
}
cleardevice();
k++;
if (k == 16)
{
k = 0;
break;
}
}
while (1)
{
setbkcolor(k + 1);
setcolor(k);
for (n = 0; n <= 100; n++)
{
for (m = 0; m <= 360; m += 10)
{
i = n * cos(m * 3.14159 / 180);
j = n * sin(m * 3.14159 / 180);
line(a, b, a + i, b - j);
}
delay(5);
n++;
}
cleardevice();
a = rand() % xmax;
b = rand() % ymax;
k++;
if (k == 16)
{
k = 0;
break;
}
}
while (1)
{
setbkcolor(15);
setcolor(k);
for (i = 1; i <= 40; i++)
{
circle(x, y, radius);
delay(50);
radius = radius + 5;
}
setcolor(15);
for (i = 1; i <= 41; i++)
{
circle(x, y, radius);
delay(50);
radius = radius - 5;
}
k++;
if (k == 15)
{
k = 0;
break;
}
}
cleardevice();
setbkcolor(4);
m = 0;
while (1)
{
setcolor(14);
circle(x + 100, y, 100);
circle(x - 100, y, 100);
if (m <= 360)
{
setcolor(k);
i = 100 * cos((180 + m) * 3.14159 / 180);
j = 100 * sin((180 + m) * 3.14159 / 180);
circle(x + 100 + i, y - j, 20);
delay(5);
m++;
}
if (m > 360)
{
setcolor(k);
i = 100 * sin((0 + m - 270) * 3.14159 / 180);
j = 100 * cos((0 + m - 270) * 3.14159 / 180);
circle(x - 100 + i, y - j, 20);
delay(5);
m++;
if (m == 720)
{
m = 0;
break;
}
}
k++;
}
getch();
closegraph();
return 0;
}
Output of the program :
If you run the program, you can see the output like below video. Here, I show you how to build a program using C or C++ programming language which shows different types of designs. You can see the output in my YouTube channel.
Conclusion :
At last, you have learned how to create various designs in C or C++ graphics programming language. Thank you for visiting my site.