Advertisement
Guest User

typeerrorhelp

a guest
Oct 6th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.63 KB | None | 0 0
  1. from turtle import *
  2. from math import *
  3.  
  4. def dist(velo,ang,grav): # Function to calculate distance of the shot
  5.    
  6.     dist = velo*sin(ang) # Formula to calculate distance
  7.     dist = round(((dist / grav)*2)*dist,3)
  8.     return dist # Returns dist variable to the main(): function.
  9.  
  10. def hi(velo,ang,grav): # Function to calculate the maximum height of the shot
  11.    
  12.     hi = round(((velo*sin(ang))**2)/(2*grav),3) # Formula to calculate the maximum height
  13.     return hi # Returns hi variable to the main(): function.
  14.  
  15. def xtoy(dist,velo,ang,grav,i): # Function to calculate the sety value for a given increment.
  16.  
  17.     setx = int(dist*(i/15))
  18.     sety = int(dist*tan(ang)-(grav*dist**2)/(2*velo**2*cos*((cos(ang)))))
  19.     return setx,sety
  20.  
  21. def main():
  22.    
  23.     print("Welcome to The Great Planetary Trajectory Calculator!") # Greeting
  24.     print() # Blank line for organizational purposes.
  25.     velo = eval(input("Please enter the velocity of the projectile. (m/s) ")) # Input velocity that the projectile will launch from.
  26.     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.
  27.     print() # Blank line for organizational purposes.
  28.     ctr = 0 # Creating the counter variable and setting it to 0.
  29.     for ctr in range(3): # For Loop that gives the current ctr variable to the proper function to get a result.
  30.         if ctr == 0: # If Then statement to detect which gravitational constant to use
  31.             grav = 9.81 # Earth
  32.             print("The distance travelled on Earth, would be:",dist(velo,ang,grav),"m. With a maximum height of:",hi(velo,ang,grav),"m.")
  33.         if ctr == 1:
  34.             grav = 1.62 # Moon
  35.             print("The distance travelled on the Moon, would be:",dist(velo,ang,grav),"m. With a maximum height of:",hi(velo,ang,grav),"m.")
  36.         if ctr == 2:
  37.             grav = 3.77 # Mars
  38.             print("The distance travelled on Mars, would be:",dist(velo,ang,grav),"m. With a maximum height of:",hi(velo,ang,grav),"m.")
  39.         #Easy to copy paste my format for more planets as needed.
  40.         ctr = ctr + 1 # Adds 1 to ctr variable for the next iteration of the loop.
  41.  
  42.     degang = degrees(ang)
  43.  
  44. #### Turtles!
  45.  
  46.     wn = Screen() # Makes a screen.
  47.     wn.setup(800,600) # Sets it up with the proper coordinates.
  48.     wn.title("The Great Planetary Trajectory Calculator!") # Titles the screen.
  49.     wn.setworldcoordinates(-20,-20,780,580) # Sets the coordinates for the screen.
  50.  
  51.     line = Turtle() # Creates frame for the graph.
  52.    
  53.     line.speed(0) # Creates the vertical and horizontal edges of the graph.
  54.     line.fd(1000)
  55.     line.up()
  56.     line.goto(0,0)
  57.     line.down()
  58.     line.left(90)
  59.     line.fd(1000)
  60.     line.up()
  61.     line.goto(0,0)
  62.     line.left(180)
  63.     line.down()
  64.    
  65.     for i in range(40): # Horizontal hashmarks
  66.         line.forward(10)
  67.         line.up()
  68.         line.left(90)
  69.         line.forward(25)
  70.         line.left(90)
  71.         line.forward(10)
  72.         line.left(180)
  73.         line.down()
  74.  
  75.     line.up() # Realignment for the vertical hashmarks.
  76.     line.goto(0,0)
  77.     line.right(90)
  78.     line.down()
  79.    
  80.     for i in range(40): # Vertical hashmarks
  81.         line.forward(10)
  82.         line.up()
  83.         line.right(90)
  84.         line.forward(25)
  85.         line.right(90)
  86.         line.forward(10)
  87.         line.left(180)
  88.         line.down()
  89.  
  90.     earth = Turtle() # Creates line for trajectory on Earth
  91.     mars = Turtle() # Creates line for trajectory on Mars
  92.     moon = Turtle() # Creates line for trajectory on the Moon
  93.  
  94.     earth.color('green')
  95.     mars.color('red')
  96.     moon.color('red')
  97.  
  98.     ctr = 0
  99.    
  100.     for ctr in range(3):
  101.  
  102.         if ctr == 0: # If Then statement to detect which gravitational constant to use
  103.             grav = 9.81 # Earth
  104.             for i in range(15):
  105.                 x1,y1 = xtoy(dist(velo,ang,grav),velo,ang,grav,i)
  106.                 earth.setpos(x1,y1)
  107.                
  108.         if ctr == 1:
  109.             grav = 1.62 # Moon
  110.             for i in range(15):
  111.                 x2,y2 = xtoy(dist(velo,ang,grav),velo,ang,grav,i)
  112.                 moon.setpos(x2,y2)
  113.                
  114.         if ctr == 2:
  115.             grav = 3.77 # Mar
  116.             for i in range(15):
  117.                 x3,y3 = xtoy(dist(velo,ang,grav),velo,ang,grav,i)
  118.                 mars.setpos(x3,y3)
  119.                
  120.         #Easy to copy paste my format for more planets as needed.
  121.         ctr = ctr + 1 # Adds 1 to ctr variable for the next iteration of the loop.
  122.        
  123.    
  124.     wn.exitonclick() # Closes window on click after the Turtles have finished.
  125.    
  126. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement