from turtle import *
from math import *
def dist(velo,ang,grav): # Function to calculate distance of the shot
dist = velo*sin(ang) # Formula to calculate distance
dist = round(((dist / grav)*2)*dist,3)
return dist # Returns dist variable to the main(): function.
def hi(velo,ang,grav): # Function to calculate the maximum height of the shot
hi = round(((velo*sin(ang))**2)/(2*grav),3) # Formula to calculate the maximum height
return hi # Returns hi variable to the main(): function.
def xtoy(dist,velo,ang,grav,i): # Function to calculate the sety value for a given increment.
setx = int(dist*(i/15))
sety = int(dist*tan(ang)-(grav*dist**2)/(2*velo**2*cos*((cos(ang)))))
return setx,sety
def main():
print("Welcome to The Great Planetary Trajectory Calculator!") # Greeting
print() # Blank line for organizational purposes.
velo = eval(input("Please enter the velocity of the projectile. (m/s) ")) # Input velocity that the projectile will launch from.
ang = radians(float(input("Please enter the angle your projectile will fire from (degrees) "))) # Input angle the projectile will launch from, then convert to radians.
print() # Blank line for organizational purposes.
ctr = 0 # Creating the counter variable and setting it to 0.
for ctr in range(3): # For Loop that gives the current ctr variable to the proper function to get a result.
if ctr == 0: # If Then statement to detect which gravitational constant to use
grav = 9.81 # Earth
print("The distance travelled on Earth, would be:",dist(velo,ang,grav),"m. With a maximum height of:",hi(velo,ang,grav),"m.")
if ctr == 1:
grav = 1.62 # Moon
print("The distance travelled on the Moon, would be:",dist(velo,ang,grav),"m. With a maximum height of:",hi(velo,ang,grav),"m.")
if ctr == 2:
grav = 3.77 # Mars
print("The distance travelled on Mars, would be:",dist(velo,ang,grav),"m. With a maximum height of:",hi(velo,ang,grav),"m.")
#Easy to copy paste my format for more planets as needed.
ctr = ctr + 1 # Adds 1 to ctr variable for the next iteration of the loop.
degang = degrees(ang)
#### Turtles!
wn = Screen() # Makes a screen.
wn.setup(800,600) # Sets it up with the proper coordinates.
wn.title("The Great Planetary Trajectory Calculator!") # Titles the screen.
wn.setworldcoordinates(-20,-20,780,580) # Sets the coordinates for the screen.
line = Turtle() # Creates frame for the graph.
line.speed(0) # Creates the vertical and horizontal edges of the graph.
line.fd(1000)
line.up()
line.goto(0,0)
line.down()
line.left(90)
line.fd(1000)
line.up()
line.goto(0,0)
line.left(180)
line.down()
for i in range(40): # Horizontal hashmarks
line.forward(10)
line.up()
line.left(90)
line.forward(25)
line.left(90)
line.forward(10)
line.left(180)
line.down()
line.up() # Realignment for the vertical hashmarks.
line.goto(0,0)
line.right(90)
line.down()
for i in range(40): # Vertical hashmarks
line.forward(10)
line.up()
line.right(90)
line.forward(25)
line.right(90)
line.forward(10)
line.left(180)
line.down()
earth = Turtle() # Creates line for trajectory on Earth
mars = Turtle() # Creates line for trajectory on Mars
moon = Turtle() # Creates line for trajectory on the Moon
earth.color('green')
mars.color('red')
moon.color('red')
ctr = 0
for ctr in range(3):
if ctr == 0: # If Then statement to detect which gravitational constant to use
grav = 9.81 # Earth
for i in range(15):
x1,y1 = xtoy(dist(velo,ang,grav),velo,ang,grav,i)
earth.setpos(x1,y1)
if ctr == 1:
grav = 1.62 # Moon
for i in range(15):
x2,y2 = xtoy(dist(velo,ang,grav),velo,ang,grav,i)
moon.setpos(x2,y2)
if ctr == 2:
grav = 3.77 # Mar
for i in range(15):
x3,y3 = xtoy(dist(velo,ang,grav),velo,ang,grav,i)
mars.setpos(x3,y3)
#Easy to copy paste my format for more planets as needed.
ctr = ctr + 1 # Adds 1 to ctr variable for the next iteration of the loop.
wn.exitonclick() # Closes window on click after the Turtles have finished.
main()