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

draw Square one by one in C graphics program

Introduction :

Graphics.h is a popular library for creating graphics applications by C programing language. Using graphics.h, developers can create simple and complex designs. In this article, I shall show you how you create square or rectangle one by one by C Graphics program.

About The Program :

In the article, you see how to create a series of squares or rectangle one by one using Graphics.h library. Here you see draw the square using the line() function, and repeat the process to create multiple squares one by one. I shall also provide the complete source code for this program, allowing you to use it for your own projects.

How run the program :

For run this program in your pc, first you have to install turboc++ in your pc. After that create a C or C++ file in turboc++. 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 turboc++ follow my link.
For Initialize the graphics mode, first call initgraph function. Now you have to show the path of bgi folder of your pc. Here my code show the bgi path as initgraph(&graphic_driver, &graphic_mode, “//turboc3/bgi”). Your path may be different from my code.

Here I use the following function

Source code :

The below source code is create a series of squares or rectangles one by one using C Graphics graphics programing mode. just copy the code and use it in your project.

/* Develop by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
#include <dos.h>
int main(void)
{
    int xmax, ymax;
    int 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(getmaxcolor());
    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;
}

Conclusion :

In this program I show you how to create square or rectangle one by one using C graphics programming language. You can see the output in my youtube channel.

First, I thanks you for visit my site. I hope you use the above code in your project.

You can also see my other program :

You can see my other blog :

Scroll to Top