How to Draw Moving of Moon and Earth around Sun by C Program

movement of the Moon and Earth around the Sun using C or C++ graphics programming language

Introduction :

The movement of the Moon and the Earth around the Sun is an interesting astronomical phenomenon. The graphics.h library in Turbo C++ provides a simple way to create the movement of the moon and Earth around the sun using C or C++ programming language. In this program, I show you how to draw the movement of the Moon and Earth around the Sun using C or C++ graphics programming language with source code. Here, I create an animation of a visual representation of the Moon and Earth orbiting the Sun to life on computer screens.

About Moon, Earth and Sun :

The Moon is a beautiful heavenly body that orbits around the Earth (our home planet). It is the only natural satellite of earth. The diameter of the Moon is about 3,474 kilometers. It is roughly one-fourth the size of earth. It is about 384,400 kilometers far away from Earth. It is the fifth-largest moon in our solar system.

The Earth is our home planet which has a several range of ecosystems and a rich variety of organisms. The Earth is the only known celestial body where life has been found. Earth is the fifth-largest planet and third planet from the Sun in our solar system. The diameter of Earth is about 12,742 kilometers. The nitrogen, oxygen, carbon dioxide and trace amounts of other gases are found in the atmosphere of Earth. The Earth is generally known as the “Blue Planet” due to its abundant water resources. Most of the surface of Earth is covered by oceans, lakes, rivers and groundwater.

The Sun is a glorious and vital part of our solar system. The Sun is a star and it is located at the center of our solar system. It is an enormous, hot and luminous ball of plasma that provides heat and light energy for the entire solar system. It is responsible for supplying heat and light energy to life on Earth. The diameter of Sun is about 1.4 million kilometers. The hydrogen and helium are found in Sun. By nuclear fusion, the Sun generates heat and light energy.

The Moon moves around the Earth and at the same time Earth moves around the Sun. So the combined motion of the Earth and the Moon around the Sun results a more complex motion. The combined motion is responsible for the day- night cycle, the lunar phases, the changing seasons and other celestial phenomena like solar eclipse and lunar eclipse. When the Moon comes between the Earth and the Sun, we can see a solar eclipse. On the other hand, during a lunar eclipse the Earth comes between the Sun and the Moon.

The Moon, Earth and Sun are interconnected through gravitational interactions. The gravitational interactions influence their orbits, tides and other phenomena in our solar system. The Moon and Sun exerts a gravitational force on Earth which causes the tides in the oceans of earth. The gravitational interaction between the Earth, Moon and the Sun creates two high tides and two low tides each day. By the changing relative positions of the Moon, Earth, and the Sun we can see different type of phases from Earth such as Full Moon (completely visible), New Moon (not visible), First Quarter and Last Quarter.

About the program :

In this program, you see the Moon and Earth are moving around the Sun in their own orbits. Here, you can see the relative positions of the Moon, Earth, and the Sun will be displayed on the screen. You can also see how solar eclipse and lunar eclipse have been created. The program runs continuously. When the user presses any key, the program will be stopped immediately.

Explanation of the program :

At the beginning of the program, you have to first include the necessary libraries. Here, graphics.h is used for graphic functions. Then include conio.h, dos.h and math.h for use kbhit(), delay() and both sin() and cos() functions respectively. Now, initialize the graphics mode using the initgraph() function which specifies the graphics driver and graphics mode. To get the x and y coordinate of the graphics window, you can use getmaxx() and getmaxy() functions.

Within the while loop, using settextstyle() and outtextxy() functions write some text like moon, earth, sun and my name. After that, set the black background color of the graphics window using setbkcolor() function. Next, draw the sun at the center of the graphics window by fillellipse() function and set the red color and style by setfillstyle() function. Now draw the Moon and Earth with their relative position and different colors using setcolor(), setfillstyle() and fillellipse() functions.

Display the orbits of the Moon and Earth using ellipse() function. Using the sin() and cos() functions, I calculate the changing position of the Moon and Earth. When the Moon comes between the Earth and the Sun, you see a black spot on Earth (from where Sun has not seen) is called solar eclipse. On the other hand, when the Earth comes between the Sun and the Moon, draw the moon by black color so nobody can not see the Moon it is called lunar eclipse.

Next, check that the user enters any key or not by kbhit() function. To make the movement of the Moon and Earth around the Sun come to life, you have to animate the program by updating the positions of the Moon and Earth and redrawing them in the next frame. To control the animation speed or the movement of the Moon and the Earth around the Sun, you can use delay() function. Using the cleardevice() function, you have to update the screen continuously. At last, use the closegraph() function to close the graphics mode.

How run the program :

First, you have to install the Turbo C++ IDE on your pc where graphics.h is included. After open the Turbo C++ IDE, create a C or C++ file with .c or .cpp extension. Now copy the below source code of the movement of the Moon and the Earth around the Sun using C or C++ graphics programming language and paste in your C or C++ file which you create in the Turbo C++ IDE. If you do not know how to copy paste in the Turbo C++ IDE, just click here. You can also see how to use graphics.h in Turbo C++ IDE from my link.

Source code of the movement of the Moon and Earth around the Sun :

You can copy the below source code of the movement of the Moon and the Earth around the Sun using C or C++ graphics programming language and use in your program.

/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <math.h>
int main(void)
{
    int x, y, i, a = 0, b = 0, c = 0, d = 0, f = 0, h = 0, j;
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    x = getmaxx() / 2;
    y = getmaxy() / 2;
    while (!kbhit())
    {
        setcolor(15);
        settextstyle(1, 0, 4);
        outtextxy(0, 0, "moon");
        outtextxy(0, 30, "earth");
        outtextxy(0, 60, "sun");
        outtextxy(530, 0, "puskar");
        outtextxy(530, 30, "jasu");
        setbkcolor(0);
        setcolor(4);
        setfillstyle(1, 4);
        fillellipse(x, y, 50, 50);
        i = 210 * cos((0 + f) * 3.14159 / 180);
        j = 170 * sin((0 + f) * 3.14159 / 180);
        f++;
        if (f == 360)
            f = 0;
        setcolor(1);
        setfillstyle(1, 1);
        fillellipse(x + i, y - j, 30, 30);
        a = 90 * cos((0 + c) * 3.14159 / 180);
        b = 70 * sin((0 + c) * 3.14159 / 180);
        ellipse(x, y, 0, 360, 210, 170);
        c += 5;
        if (c == 360)
            c = 0;
        setcolor(6);
        setfillstyle(1, 6);
        fillellipse(x + i + a, y - j - b, 15, 15);
        a = 300 * cos((0 + d) * 3.14159 / 180);
        b = 239 * sin((0 + d) * 3.14159 / 180);
        setcolor(0);
        setfillstyle(1, 0);
        fillellipse(x + a, y - b, 27, 27);
        setcolor(6);
        ellipse(x + i, y - j, 0, 360, 90, 70);
        d++;
        if (d == 360)
            d = 0;
        if ((f > 38 && f < 52) || (f > 128 && f < 142) || (f > 218 && f < 232) || (f > 308 && f < 322))
        {
            a = 35 * cos((0 + h) * 3.14159 / 180);
            b = 30 * sin((0 + h) * 3.14159 / 180);
            setcolor(0);
            setfillstyle(1, 0);
            fillellipse(x + i + a, y - j - b, 20, 20);
        }
        h += 5;
        if (h == 360)
            h = 0;
        delay(200);
        cleardevice();
    }
    getch();
    closegraph();
    return 0;
}

Output :

After running the program, you see the movement of the Moon and the Earth around the Sun by C or C++ graphics programming language come to life on your screen. You can also see the output of the above program in my YouTube channel. You can see the orbits of the Moon and Earth in the above program which you cannot see in my YouTube video. Because I have added the code for orbits of the Moon and Earth in my above program.

Conclusion :

By following the above article, you have learned how to create a basic graphical representation of the movement of the Moon and the Earth around the Sun by C or C++ graphics programming language. This visualization allows you to see the relative positions of the Moon and Earth around the Sun and also see how solar eclipse and lunar eclipse are created. Thanking you for visiting my site.

Scroll to Top