Drawing Planetary Orbits in Python - Dive into the world of planetary orbits using Python's Turtle library. Discover how orbital eccentricity shapes cosmic paths, from Earth's near-circular orbit to a comet's elliptical journey. Code your own solar system simulation in this inter

Drawing Planetary Orbits in Python

Today, we're diving into the fascinating world of planetary orbits. We'll not only learn about the shapes of these cosmic dance paths but also bring them to life using Python's turtle library. Buckle up for an adventure that combines astronomy, physics, and programming!

1. Introduction to Planetary Orbits

Planets don't just wander aimlessly through space. They follow specific paths around their parent stars, guided by the invisible hand of gravity. These paths, or orbits, can vary in shape from nearly perfect circles to elongated ellipses.

2. Understanding Orbital Eccentricity

One of the key characteristics of an orbit is its eccentricity. Orbital eccentricity is a measure of how much an orbit deviates from a perfect circle. It's represented by a number between 0 and 1:

  • An eccentricity of 0 means the orbit is a perfect circle.
  • As eccentricity increases towards 1, the orbit becomes more elliptical.
  • An eccentricity of 1 would represent a parabolic trajectory (not a closed orbit).

In our solar system, most planets have low eccentricities, resulting in nearly circular orbits. However, some objects like comets have highly eccentric orbits.

3. Setting Up Our Python Environment

Let's start by importing the necessary libraries and setting up our black space canvas:

import turtle
import math

# Set up the screen
screen = turtle.Screen()
screen.setup(800, 600)
screen.bgcolor("black")
screen.title("Planetary Orbits Simulator")

# Create the central star
star = turtle.Turtle()
star.shape("circle")
star.color("yellow")
star.shapesize(2)
star.penup()

4. Creating Planets and Orbits

Now, let's create a function to generate planets and another to calculate their orbital paths:

def create_planet(color, size):
    planet = turtle.Turtle()
    planet.shape("circle")
    planet.color(color)
    planet.shapesize(size)
    planet.penup()
    return planet

def orbit(planet, a, e, color):
    planet.pencolor(color)
    
    # Calculate the starting position
    r = a * (1 - e**2) / (1 + e * math.cos(0))
    x = r * math.cos(0)
    y = r * math.sin(0)
    
    # Move the planet to the starting position without drawing
    planet.goto(x, y)
    
    # Now start drawing the orbit
    planet.pendown()
    
    theta = 0
    while theta <= 2 * math.pi:
        r = a * (1 - e**2) / (1 + e * math.cos(theta))
        x = r * math.cos(theta)
        y = r * math.sin(theta)
        planet.goto(x, y)
        theta += 0.01
    
    planet.penup()

In this `orbit` function:

  • a is the semi-major axis (half the longest diameter of the ellipse)
  • e is the eccentricity

The equation r = a * (1 - e**2) / (1 + e * math.cos(theta)) is the polar form of an ellipse, which gives us the distance r from the focus (where our star is) for any angle theta.

5. Simulating Different Orbits

Let's create some planets with different eccentricities:

# Create and draw orbits
# Nearly circular orbit (like Earth)
earth = create_planet("blue", 0.5)
orbit(earth, 200, 0.017, "blue")

mars = create_planet("red", 0.4)
orbit(mars, 250, 0.0934, "red")

comet = create_planet("white", 0.2)
orbit(comet, 300, 0.8, "white")

turtle.done()

6. Explaining the Results

When you run this code, you'll see three orbits:

  • The blue orbit (Earth) is nearly circular with an eccentricity of 0.017.
  • The red orbit (Mars) is slightly more elliptical with an eccentricity of 0.0934.
  • The white orbit (comet) is highly elliptical with an eccentricity of 0.8.

Notice how the more eccentric orbits deviate further from a perfect circle. The comet's orbit brings it very close to the star at one point (perihelion) and then takes it far away at another (aphelion).

7. The Significance of Orbital Eccentricity

Orbital eccentricity plays a crucial role in a celestial body's journey around its parent star:

  • It affects the body's distance from the star throughout its orbit, influencing temperature variations.
  • Higher eccentricity can lead to more extreme seasonal changes on a planet.
  • In some cases, eccentricity can affect a planet's habitability.

8. Conclusion

Through this interactive Python simulation, we've explored the concept of orbital eccentricity and how it shapes the paths of planets and other celestial bodies. As future space explorers at Gdynia Space Academy, understanding these orbital dynamics is crucial for planning missions, predicting celestial events, and unraveling the mysteries of our universe.

Remember, the universe is vast and full of orbits far more complex than we've simulated here. Some systems have multiple planets influencing each other's orbits, leading to fascinating phenomena like orbital resonances. As you continue your journey in space science, you'll encounter these and many other intriguing concepts.

Keep exploring, future astronauts! The stars are waiting for you!

Extra Challenge: Try modifying the code to add more planets, or to animate the planets moving along their orbits. Can you add labels to show each planet's current distance from the star? The universe is your playground!

import turtle
import math

# Set up the screen
screen = turtle.Screen()
screen.setup(800, 600)
screen.bgcolor("black")
screen.title("Planetary Orbits Simulator")

# Create the central star
star = turtle.Turtle()
star.shape("circle")
star.color("yellow")
star.shapesize(2)
star.penup()

def create_planet(color, size):
    planet = turtle.Turtle()
    planet.shape("circle")
    planet.color(color)
    planet.shapesize(size)
    planet.penup()
    return planet

def orbit(planet, a, e, color):
    planet.pencolor(color)
    
    # Calculate the starting position
    r = a * (1 - e**2) / (1 + e * math.cos(0))
    x = r * math.cos(0)
    y = r * math.sin(0)
    
    # Move the planet to the starting position without drawing
    planet.goto(x, y)
    
    # Now start drawing the orbit
    planet.pendown()
    
    theta = 0
    while theta <= 2 * math.pi:
        r = a * (1 - e**2) / (1 + e * math.cos(theta))
        x = r * math.cos(theta)
        y = r * math.sin(theta)
        planet.goto(x, y)
        theta += 0.01
    
    planet.penup()

# Create and draw orbits
# Nearly circular orbit (like Earth)
earth = create_planet("blue", 0.5)
orbit(earth, 200, 0.017, "blue")

mars = create_planet("red", 0.4)
orbit(mars, 250, 0.0934, "red")

comet = create_planet("white", 0.2)
orbit(comet, 300, 0.8, "white")

turtle.done()