1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include <iostream> #include <cmath> #include <vector> #include <iomanip> const double PI = 3.14159265358979323846; const double DEG_TO_RAD = PI / 180.0; // Class to represent a celestial body class CelestialBody { public: CelestialBody(const std::string& name, double distanceFromSun, double orbitalPeriod) : name(name), distanceFromSun(distanceFromSun), orbitalPeriod(orbitalPeriod), angle(0.0) {} // Method to update the position of the celestial body void updatePosition(double time) { double angularVelocity = 360.0 / orbitalPeriod; // degrees per unit time angle = fmod(angularVelocity * time, 360.0); // Update angle based on time x = distanceFromSun * cos(angle * DEG_TO_RAD); y = distanceFromSun * sin(angle * DEG_TO_RAD); } // Method to print the celestial body's information void printInfo() const { std::cout << name << " is at (" << std::fixed << std::setprecision(2) << x << ", " << y << ") from the Sun.\n"; } private: std::string name; double distanceFromSun; // Distance from the Sun double orbitalPeriod; // Orbital period in arbitrary time units double angle; // Current angle in degrees double x, y; // Cartesian coordinates }; int main() { // Create celestial bodies std::vector<CelestialBody> celestialBodies = { CelestialBody("Mercury", 57.9, 88.0), CelestialBody("Venus", 108.2, 225.0), CelestialBody("Earth", 149.6, 365.0), CelestialBody("Mars", 227.9, 687.0) }; // Simulation parameters double simulationTime = 0.0; double timeStep = 1.0; // Time step for the simulation // Run the simulation for (int step = 0; step < 10; ++step) { std::cout << "Time: " << simulationTime << "\n"; for (auto& body : celestialBodies) { body.updatePosition(simulationTime); body.printInfo(); } std::cout << std::endl; simulationTime += timeStep; } return 0; } |
Explanation
- CelestialBody Class:
- Attributes:
name
: Name of the celestial body (e.g., planet).distanceFromSun
: Average distance from the Sun (in arbitrary units).orbitalPeriod
: Time taken to complete one orbit (in arbitrary time units).angle
: Current angle of the body in its orbit.x
,y
: Cartesian coordinates of the body.
- Methods:
updatePosition(double time)
: Updates the position of the celestial body based on time. The angle of the body is calculated and then converted to Cartesian coordinates.printInfo()
: Prints the current position of the celestial body.
- Attributes:
- Main Function:
- Celestial Bodies Initialization: Creates a list of
CelestialBody
objects representing planets in the solar system. - Simulation Loop:
- Time Update: Advances the simulation time in discrete steps.
- Position Update: Updates and prints the position of each celestial body based on the current time.
- Celestial Bodies Initialization: Creates a list of
Usage
- Solar System Simulation: The program models a basic solar system with planets orbiting around the Sun, demonstrating their positions over time.
- Orbital Calculation: Computes the position of each celestial body using simple circular orbit equations.