draw arc with color by c with Source code

Introduction :

Computer graphics programming in C or C++ play an important role in various fields from games to scientific applications. Using C or C++ programming language and graphics.h library of Turbo C++ IDE, you can create any type of basic graphical applications or design in a simple way.

In this article, you see how to build a program of draw arcs with different colors and create an animated sequence of colorful arcs using graphics.h by C or C++ graphics programing language with source code. By the end of this article, you have learned how to bring colorful arcs (a portion of a circle) to life on the screen.

About The Program :

The program is a simple graphics animation that draws a colorful and animated pattern of arcs and lines. In this program, you see an animation of a growing and changing arc with changing colors drawn across the screen. The animation will continue until it has used 16 different colors. At last, it will wait for user to press any key.

Explanation of the program :

First, you have to include graphics.h, conio.h and dos.h library in the program for graphics function, getch() function and delay() function respectively. In the main function, you have to declare “x”, “y”, “i” and “j” integer variables. Then declare “graphic_driver” and “graphic_mode” variables which used to specify the graphics driver and mode. Using initgraph() function, you can initialize the graphics system using the detected graphics driver and mode.

Now calculates the x and y coordinate of the center of the screen using getmaxx() and getmaxy() functions and store in “x” and “y” variables respectively. Next, setbkcolor() function use for set the background color of graphics mode. In the while loop, use setcolor() function to set the current drawing color. To set the fill pattern and color of arc use the setfillstyle() and floodfill() functions. To draw arc and line, you can use arc() and line() function.

Here, you can introduce a delay of 100 milliseconds using delay() function to control the animation speed for better views. In the loop, increment the “i” and “j” variables to increase the size and change the color of the arcs. Now, the program waits for a key press before proceeding to close the graphics window using getch() function. At last, closegraph() function closes the graphics system.

How run the program :

If you want to run this program on your computer, you have to install Turbo C++ in your computer. If you have not installed Turbo C++, first install Turbo C++ IDE. After that Just copy my following source code and paste it in your c or c++ file or create a c or c++ file and write the code. Then run the code after build and see the output on your pc.

In this program, I use graphics.h library. For use graphics.h library you have to show the path of graphics.h like my code. initgraph(&gdriver, &gmode, “//turboc3/bgi”); your path may be different from my path. You have to show the bgi folder of your pc. You can also see how to use graphics.h in Turbo C++ IDE in my other post.

Source code of arc with color by C or C++ programming language :

You can use my source code in your project. Please copy the following code and use it.

/* Develop by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
#include <dos.h>
int main(void)
{
    int x, y, i = 0, j = 0;
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    x = getmaxx() / 2;
    y = getmaxy() / 2;
    setbkcolor(0);
    while (1)
    {
        setcolor(i);
        setfillstyle(1, i);
        arc(x, y, 0, 180, 5 + j);
        arc(x, y, 0, 180, 15 + j);
        line(x - 15 - j, y, x + 15 + j, y);
        floodfill(x, y - 10 - j, i);
        delay(100);
        j += 10;
        i++;
        if (i == 16)
            break;
    }
    getch();
    closegraph();
    return 0;
}

Output :

You can see the output in my YouTube channel.

Conclusion :

In this above article, you have learned how to draw colorful arcs using the graphics.h library of Turbo C++ IDE in the C or C++ programming language. You can create more complex graphical elements and applications using other graphical function of the graphics.h library of Turbo C++ IDE to bring your creative ideas to life on the screen. Thank you for visiting my site.

Scroll to Top