How to Create Square one by one by C Graphics Program with Source Code

draw Square one by one in C graphics program

Introduction :

The graphics.h is a popular library of Turbo C++ IDE. It is used for creating graphics applications in C programming language. Using graphics.h library, developers can create simple and complex graphics, designs or animations. In this article, I shall show you how to create square shapes one by one in C graphics Programming language.

About the program :

This is a C program which creates an animated visual effect on the graphics screen. It draws different size of squares with the help of line. When you run this program, you can see a dynamic graphical display on the screen. Here, the squares are drawn using line and repeated the process to create multiple squares of different size.

Explanation of the program :

At the beginning of the program, include graphics.h, conio.h and dos.h libraries in the program. Now, you have to declare integer variables such as “xmax”, “ymax”, “x”, “y”, “xx”, “yy”, “x1”, “x2”, “y1”, “y2”, “xx1”, “xx2”, “yy1”, “yy2”, “i, “graphic_driver” and “graphic_mode” in the main function. Then, initialize the graphics mode using initgraph() function. You can set the current drawing color using setcolor() function.

To get the maximum x and y coordinates of the graphics screen, you can use getmaxx() and getmaxy() functions respectively. In the for loop, draw each side of the square one by one using line() function. Here, you have to call delay() function to create an animated effect. After the for loop, use getch() function to get a key press. At last, close the graphics mode using closegraph() function.

How run the program :

To run this program on your PC, first you have to install the Turbo C++ IDE. After that, create a C or C++ file in Turbo C++ IDE. Now, copy the below source code and paste it in your C or C++ file. If you do not know how to copy paste in Turbo C++ IDE, follow my link. You have to also know how to use graphics.h in Turbo C++ IDE.

Source code of the program :

The following code is the source code of the program that creates a series of square one by one in C programming language. Here, I provide the complete source code for this program. You can use it in your own projects.

/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
#include <dos.h>
int main(void)
{
    int xmax, ymax, x, y, xx, yy, x1, x2, y1, y2, xx1, xx2, yy1, yy2, i;
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    setcolor(15);
    xmax = getmaxx();
    ymax = getmaxy();
    yy = ymax / 2 + 10;
    xx1 = xmax / 2 - 10;
    xx2 = xmax / 2 + 10;
    xx = xmax / 2 - 10;
    yy1 = ymax / 2 - 10;
    yy2 = ymax / 2 + 10;
    y = ymax / 2 - 10;
    x1 = xmax / 2 - 10;
    x2 = xmax / 2 + 10;
    x = xmax / 2 + 10;
    y1 = ymax / 2 - 10;
    y2 = ymax / 2 + 10;
    for (i = 1; i <= 20; i++)
    {
        line(x1, y, x2, y);
        delay(300);
        line(x, y1, x, y2);
        delay(300);
        line(xx1, yy, xx2, yy);
        delay(300);
        line(xx, yy1, xx, yy2);
        delay(300);
        xx = xx - 10;
        yy1 = yy1 - 10;
        yy2 = yy2 + 10;
        x1 = x1 - 10;
        x2 = x2 + 10;
        y = y - 10;
        x = x + 10;
        y1 = y1 - 10;
        y2 = y2 + 10;
        yy = yy + 10;
        xx1 = xx1 - 10;
        xx2 = xx2 + 10;
    }
    getch();
    closegraph();
    return 0;
}

Output of the program :

If you run the program, you can see the output. You can see the output in my YouTube channel.

Conclusion :

At last, you have learned how to create square one by one in C graphics Programming language. Thank you for visiting my site.

Scroll to Top