0% found this document useful (0 votes)
28 views18 pages

PLC Final

Uploaded by

dakax40517
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views18 pages

PLC Final

Uploaded by

dakax40517
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

B. M. S.

COLLEGE OF ENGINEERING

Bull Temple Road, Bengaluru – 560019

Autonomous Institute, Affiliated to VTU – Belagavi

A Report on
Title: Planetary Orbit Simulation Using Turtle
Graphics in Python

Submitted towards AAT of the course


‘INTRODUCTION TO PYTHON PROGRAMMING’
22CS1EPYP

Submitted by students of 2nd sem, ‘C-A’ section


YASH GUPTA (1BM23ET061)
YUKTA H (1BM23ET062)

Faculty In-charge
DR. CHETAN RAJ D
Assistant Professor
Department of Electrical and Electronics Engineering
B. M. S. College of Engineering

JULY 2024

ASSESSMENT

Data Collection Content Relevance Pictures Flow charts / Organization of Total Marks
to Title Block Diagrams Report
Signature of faculty in-charge with date

ABSTRACT

This Python program utilizes the Turtle graphics module to simulate the motion of planets around a
sun-like object on a graphical screen. Each planet is represented as a subclass of ‘turtle.Turtle’,
initialized with specific attributes such as name, radius, and colour. The planets are placed at
calculated positions around the sun using trigonometric functions (cos and sin), based on their orbital
radius and an incrementally increasing angle for each time step. The animation is controlled within a
while True loop, where each planet's position is updated iteratively to simulate its orbital motion.
The screen is updated periodically to reflect these changes, creating a dynamic visualization of
planetary orbits.

1
TABLE OF CONTENTS

SL.NO. TITLE PAGE NO.

1. INTRODUCTION 3

2. OBJECTIVES 4

3. KEY FEATURES OF THE 5


PLANETARY ORBIT SIMULATION PROGRAM

4. REQUIREMENTS 6

5. IMPLEMENTATION DETAILS AND FUNCTIONAL 7


OVERVIEW

6. ALGORITHM 11

7. CODE 13

8. EXEMPLER OUTPUTS 15

9. CONCLUSION 16

2
INTRODUCTION

This python program explores the dynamics of our solar system through visualization has long
captivated both scientists and enthusiasts alike. This Python program harnesses the capabilities of the
Turtle graphics module to simulate the orbital paths of planets around a central sun. By leveraging
simple yet powerful geometric principles and the dynamic rendering capabilities of Turtle graphics,
each planet is depicted as a distinct object orbiting a stationary sun at the centre of the screen. This
simulation not only illustrates planetary orbits but also offers a visually engaging way to understand
fundamental concepts such as orbital mechanics and periodic motion in celestial bodies.

The program initializes a graphical window using the ‘turtle.Screen()’ function and sets up a sun
object represented by a yellow circle. Subsequently, each planet is instantiated as a subclass of
turtle.Turtle, configured with specific characteristics including name, radius, and colour. These
planets are then placed at calculated positions relative to the sun using trigonometric functions to
simulate their orbital paths. As the simulation progresses, each planet's position is updated iteratively
based on an increasing angle parameter, which corresponds to the planet's angular velocity around
the sun. This continuous adjustment creates a dynamic representation of planetary orbits that evolves
over time on the screen.

Beyond its visual appeal, this simulation serves educational purposes by illustrating fundamental
principles of planetary motion and orbital dynamics in a tangible manner. For students and learners
of astronomy and physics, observing how planets move in relation to their central star can provide
insights into concepts like Kepler's laws of planetary motion and gravitational influences. Moreover,
the interactive nature of the simulation makes it accessible and engaging for individuals interested in

3
astronomy as a hobby or educational pursuit, offering a hands-on way to explore the intricacies of
our solar system's structure and dynamics.

OBJECTIVES

1. Simulation of Planetary Orbits: The primary objective of the program is to simulate the
orbits of planets around a central sun using Python and Turtle graphics. This involves
accurately representing the relative positions and movements of each planet based on orbital
parameters such as radius and angular velocity.
2. Visualization of Celestial Mechanics: To demonstrate fundamental principles of celestial
mechanics through visualization. This includes showcasing concepts like orbital paths,
periodic motion, and the gravitational interaction between planets and their central star.
3. Educational Tool: Serve as an educational tool for understanding astronomical concepts.
The program aims to provide a visual aid that helps students and enthusiasts grasp complex
astronomical phenomena in a simplified and interactive manner.
4. Interactive Exploration: Enable users to interactively explore the solar system's dynamics.
By adjusting parameters such as orbital radius and angular velocity, users can observe how
these factors influence planetary orbits and positions over time.
5. Demonstration of Python Graphics: Showcase the capabilities of Python's Turtle graphics
module for creating dynamic and visually appealing simulations. This includes demonstrating
how to initialize a graphical environment, create objects with specific attributes, and animate
their movements effectively.
6. Engagement and Outreach: Foster interest in astronomy and scientific visualization among
learners and the general public. By providing an engaging and accessible simulation, the
program aims to spark curiosity and encourage further exploration of astronomy as a field of
study or hobby.

4
KEY FEATURES OF THE
PLANETARY ORBIT SIMULATION PROGRAM

1. Interactive Visualization: The program offers an interactive graphical representation of


planetary orbits using Turtle graphics in Python. Each planet is depicted as a distinct object
orbiting a central sun, providing a dynamic visualization of celestial mechanics.
2. Customizable Parameters: Users can customize various parameters such as the orbital
radius, angular velocity, and colors of planets. This flexibility allows for experimentation and
observation of how different factors affect planetary orbits and positions.
3. Accurate Orbital Mechanics: The simulation accurately models planetary motion based on
fundamental principles of orbital mechanics, including Kepler's laws and gravitational
interactions. This ensures that the simulated orbits realistically reflect the dynamics observed
in our solar system.
4. Educational Insights: Designed as an educational tool, the program helps users understand
complex astronomical concepts in a visual and intuitive manner. It facilitates learning about
planetary orbits, orbital periods, and the influence of gravitational forces on celestial bodies.
5. Real-time Animation: Utilizing screen updates and iterative position adjustments, the
program creates a real-time animation of planetary orbits. This feature allows users to
observe and analyze how planets move relative to each other and their central star over time.
6. Python and Turtle Graphics Integration: Demonstrates the integration of Python
programming language with Turtle graphics module for creating graphical simulations. It
showcases Python's capabilities in scientific visualization and simulation development.

5
7. Engaging User Experience: The program offers an engaging user experience by providing a
visually appealing simulation that encourages exploration and discovery. It aims to spark
interest in astronomy and promote scientific curiosity among users of all ages.
8. Scalability and Extension: The modular structure of the program allows for scalability and
extension. Additional features, such as adding more planets or incorporating more detailed
orbital parameters, can be implemented to enhance the simulation further.

REQUIREMENTS

Hardware Requirements:

1. Computer: Any standard desktop or laptop computer capable of running Python.


2. Graphics Capability: Basic graphics capability to display visual simulations smoothly.

Software Requirements:

1. Operating System: Compatible with Windows, macOS, or Linux operating systems.


2. Python Interpreter: Python 3.x installed on the system. Python can be downloaded from
python.org.
3. Integrated Development Environment (IDE): Optional but recommended for coding
convenience. Popular choices include PyCharm, VS Code, and IDLE (included with Python).

Library Requirements:

1. Turtle Graphics: Included in Python's standard library, no additional installation required.


Provides a simple and effective way to create graphics and animations.
2. math Module: Standard Python module for mathematical operations (import math). Used in
the program for trigonometric calculations necessary for positioning planets in their orbits.

6
IMPLEMENTATION DETAILS AND FUNCTIONAL OVERVIEW

FUNCTIONS:

1. main() Function:

 Although not explicitly defined as a function in the provided code snippet, the while True
loop serves as the main function of the program. It manages the continuous execution of the
simulation by updating the screen and moving each planet.

2. Planet Class and Methods:

 __init__(self, name, radius, color): Initializes a planet object with attributes such as name,
radius, and color. It configures the turtle graphics shape, color, and initial position relative to
the sun.
 move(self): Calculates and updates the position of the planet based on its current orbital angle
(self.angle). This method uses trigonometric functions (cos and sin) to determine the planet's
coordinates relative to the sun.

3. Initialization and Setup Functions:

 Screen and Sun Setup:


o screen = turtle.Screen(): Initializes the graphical screen where the simulation will be
displayed.

7
o screen.tracer(50): Controls the speed of animation by updating the screen at
intervals, enhancing performance.
o sun = turtle.Turtle(): Creates a turtle object to represent the central sun, setting its
shape ('circle') and color ('yellow').
 Planet Initialization:
o Planets such as mercury, Venus, earth, etc., are initialized as instances of the Planet
class with specific attributes (name, radius, color). These planets are added to the
myList for systematic management.

4. Animation and Update Functions:

 while True Loop: This serves as the animation loop, continuously updating the screen and
moving each planet in myList.
o screen.update(): Updates the graphical screen to reflect changes in the positions of
planets.
o Iterates through myList: For each planet in the list, calls its move() method to
update its position based on its current orbital angle (self.angle).

5. Mathematical Functions:

 cos() and sin() (from the math module): Used within the move() method of each planet to
calculate x and y coordinates based on the orbital angle, ensuring accurate positioning of
planets in their orbits.

6. Educational and Visual Functions:

 The simulation functions as an educational tool by visually demonstrating concepts such as


planetary motion, orbital mechanics, and gravitational interactions. Users can observe how
changing parameters (radius, angle) affect planetary orbits, enhancing understanding and
engagement.

7. Python and Turtle Graphics Integration:

8
 Demonstrates the integration of Python's programming capabilities with Turtle graphics for
creating interactive simulations. This integration underscores Python's suitability for
scientific visualization and educational purposes.

VARIABLES:

1. Screen and Turtle Objects:

 screen: Represents the graphical window where the simulation is displayed (turtle.Screen()).
 sun: Turtle object representing the central sun, used for visualization (turtle.Turtle()).

2. Planet Objects:

 Planets (e.g., mercury, venus, etc.): Each planet is instantiated as an object of the Planet
class with specific attributes:
o name: Name of the planet.
o radius: Orbital radius from the central sun.
o color: Color representing the planet.
o Methods: Each planet object has methods inherited from turtle.Turtle, customized for
its specific attributes and orbital mechanics.

3. Lists and Iteration:

 myList: List containing instances of all planets ([mercury, venus, earth, ...]). Allows for
systematic iteration through each planet during animation.
 for i in myList:: Iterates through the list of planets to update their positions and manage their
movement in the simulation loop.

9
4. Mathematical Variables:

 angle: Attribute within each planet object (self.angle). Represents the current angular
position or phase of the planet in its orbit. Incrementally updated to simulate planetary
motion.
 Trigonometric Functions: Used to calculate new coordinates (x, y) based on angle, ensuring
accurate placement of planets relative to the sun (cos(angle), sin(angle) from the math
module).

5. Educational and Interaction Variables:

 Parameters: Variables such as orbital radius (radius) and angular velocity (angle increment)
are configurable. Users can adjust these parameters to observe changes in planetary orbits,
enhancing educational value and interactivity.

6. Performance and Animation Control:

 screen.tracer(): Controls the speed and smoothness of animation by setting the update
interval for the graphical screen.
 while True Loop: Manages the continuous execution of the simulation, updating the screen
and iterating through planets (myList) to animate their movements.

7. Python and Turtle Graphics Integration:

 Demonstrates the integration of Python programming language with Turtle graphics module
for creating interactive simulations. Variables are utilized to manage and control the
simulation's behavior, enhancing the visual and educational aspects of the program.

10
ALGORITHM

 Initialization:

 Import necessary modules (turtle, time, math).


 Create a graphical screen (turtle.Screen()) and configure animation speed (screen.tracer()).
 Create a turtle object (sun) to represent the central sun, setting its shape ('circle') and color
('yellow').

 Define the Planet Class:

 Define a class Planet that inherits from turtle.Turtle.


 Initialize each planet object (mercury, venus, earth, etc.) with attributes:
o name: Name of the planet.
o radius: Orbital radius from the sun.
o color: Color representing the planet.
 Configure the turtle object (shape, color, up(), pd()) for each planet.

 Define Planet Movement (move() method):

 Use trigonometric functions (cos(angle), sin(angle)) to calculate new x and y coordinates for
each planet based on its current orbital angle.

11
 Update the planet's position relative to the sun (self.goto()).

 Simulation Loop (while True):

 Continuously update the screen (screen.update()) to reflect changes in planet positions.


 Iterate through each planet in myList:
o Call the move() method to update its position based on its angle.
o Increment the angle for each planet to simulate its orbital motion over time.

 Educational and Interactive Features:

 Allow customization of orbital parameters (radius, angle increment) to observe different


planetary orbits.
 Provide visual representation of celestial mechanics concepts such as orbital paths,
gravitational interactions, and periodic motion.

 End of Algorithm:

 The simulation continues indefinitely (while True) until manually stopped or interrupted.
 Users can interactively observe and learn about planetary orbits and their dynamics through
the visual simulation.

12
CODE
#importing the required modules
import turtle
import time
from math import *

screen = turtle.Screen()#creating the screen


screen.tracer(50)

sun = turtle.Turtle()#turtle object for sun


sun.shape('circle')#shape of sun
sun.color('yellow')#colour of sun

class Planet(turtle.Turtle):
def __init__(self,name,radius, color):#initialize function
super().__init__(shape='circle')
self.name = name
self.radius = radius
self.c = color
self.color(self.c)
self.up()
self.pd()
self.angle = 0
def move(self):
x = self.radius*cos(self.angle) # Angle in radians
y = self.radius*sin(self.angle)

self.goto(sun.xcor()+x,sun.ycor()+y)

13
# making plantes
mercury = Planet("Mercury",40, 'grey')
venus = Planet("Venus",80, 'orange')
earth=Planet("Earth",100,'blue')
mars = Planet("Mars",150, 'red')
jupiter=Planet("Jupiter",180, 'brown')
saturn=Planet("Saturn",230, 'pink')
uranus=Planet("Uranus",250, 'light blue')
neptune=Planet("Neptune",280, 'black')

#adding planets to a list


myList = [ mercury, venus,earth, mars,jupiter,saturn,uranus,neptune]

while True:#while statement


screen.update()#updating the screen
for i in myList:
i.move()#moving the elements of the list

# Increase the angle by 0.0x radians

mercury.angle += 0.05
venus.angle += 0.03
earth.angle += 0.01
mars.angle += 0.007

jupiter.angle += 0.02
saturn.angle += 0.018
uranus.angle += 0.016
neptune.angle += 0.005

14
EXEMPLER OUTPUTS

15
CONCLUSION

In conclusion, the planetary orbit simulation code developed using Python and Turtle graphics has
successfully demonstrated fundamental concepts of celestial mechanics in an interactive and visual

16
manner. Through the simulation, users can observe the dynamic movements of planets around a
central sun, with each planet's orbital parameters influencing its position and speed. The use of
Python's Turtle graphics module facilitated the creation of an intuitive graphical interface, allowing
for customization of planetary attributes such as orbital radius and color. This educational tool not
only enhances understanding of astronomical principles like Kepler's laws and gravitational
interactions but also fosters curiosity and engagement in astronomy among learners of all ages.

Moreover, the modular and object-oriented design of the code ensures scalability and extensibility.
Additional planets can be easily added to the simulation, and parameters can be adjusted to simulate
hypothetical scenarios or more complex astronomical models. This flexibility makes the simulation
not only a valuable educational resource but also a platform for further exploration and
experimentation in the field of astronomy.

Overall, the planetary orbit simulation code serves as a testament to Python's versatility in scientific
visualization and educational tools. It underscores the importance of interactive simulations in
making abstract concepts tangible and accessible, empowering users to explore and appreciate the
wonders of our solar system and beyond.

17

You might also like