How to Move Circle in a Rounded Path by C Graphics with Source Code

circle around circle by C or C++ language

Introduction :

Moving an object in a rounded or circular path is a common and fundamental task in graphics program. Here, I use trigonometric functions and simple animation techniques for moving a circle in a circular path. In this article, you will learn you how to move a circle in a rounded or circular path in C or C++ graphics programming language. I also show you the source code of the program which is built using graphics.h library in Turbo C++ IDE.

Logic for moving a circle in a circular path :

In order to move a circle in a rounded or circular path, it’s important to understand the principles of circular motion. Circular motion is the motion of an object in a circular path around a fixed point, which is called the center of the circle.

In circular motion, the object moves at a constant speed, but it changes direction at each point on the circle. The angle between the current position of the moving circle and the center point of the circle determines the direction of the moving circle’s movement.

About the program :

In C or C++ graphics program, you can move the circle in a rounded or circular path by drawing the circle at different positions around the path. First calculate the position of the circle at every point on the path using trigonometry. By using trigonometry, the ratios of the sides of a right-angled triangle to calculate angles and lengths. In this program, I shall move a circle around an ellipse in C or C++ programming language.

Explanation of the program :

In the program, include the graphics.h, conio.h, dos.h and math.h header files. Then, declare integer variables (i, j, m, graphic_driver and graphic_mode) in the main() function. After that, initialize the graphics mode using initgraph() function. Now, set the background color of the graphics screen using setbkcolor() function. Then, create an ellipse using ellipse() function.

To move the circle around the ellipse, you need to update its position in a loop. Here, I use the while loop to continuously update the position of the circle. The loop will exit when user presses a key. I use the kbhit() function to check if a key has been pressed or not. After that, I use setcolor() function to set the color of the circle. Using circle() function, create the circle which moves around the ellipse.

You can use delay() function to control the speed of the circle which moves around the ellipse. To calculate the new coordinates of the circle, use trigonometric functions such as cos() and sin(). Finally, closing the graphics mode using closegraph() function. You can use the following formula to calculate rounded path (x and y coordinates) of the moving circle.

How run the program :

To move a circle around ellipse using C or C++ programming language on your PC, first open Turbo C++ IDE. Then, create a C or C++ file. If you have not installed Turbo C++ on your PC, install it. Now, copy the below source code and paste it in your C or C++ file that you have just created. You can see how to copy paste in Turbo C++ from here. You can also see how to run a graphics program in Turbo C++.

Source code of the program :

The following code is the source code of moving a circle around ellipse using graphics.h library in C or C++ programing language. To use the source code, Just copy the code and paste it in your project.

/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <math.h>
int main(void)
{
    int i = 0, j = 0, m = 0;
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    setbkcolor(0);
    ellipse(250, 250, 0, 360, 200, 150);
    while (!kbhit())
    {
        setcolor(4);
        circle(250 + i, 250 - j, 10);
        delay(50);
        setcolor(0);
        circle(250 + i, 250 - j, 10);
        i = 200 * sin(m * 3.14159 / 180);
        j = 150 * cos(m * 3.14159 / 180);
        m++;
        if (m == 360)
            m = 0;
    }
    closegraph();
    return 0;
}

Output of the program :

When you run the program on your PC, you can see the output of the program like below video. You can also see the output in my YouTube channel.

Conclusion :

In this article, you have learned how to move a circle around ellipse step by step by C or C++ graphics programming language. Here I have used trigonometric functions and animation techniques to create a smooth circular motion. I think you understood my source code and use the code in your project. Thank you for visiting my site.

Scroll to Top