# This program computes the trajectory of a projectile. # This version is decomposed into functions. import math # constant for Earth acceleration in meters/second^2 ACCELERATION = -9.81 # Explains program to user with print statements. def intro(): print("This program computes the") print("trajectory of a projectile given") print("its initial velocity and its") print("angle relative to the") print("horizontal.") print() # Reads input from user for velocity, angle, and steps. # Returns those three values. def read_input(): velocity = float(input("velocity (meters/second)? ")) angle = math.radians(float(input("angle (degrees)? "))) steps = int(input("number of steps to display? ")) print() return velocity, angle, steps # Prints the table of x/y position of projectile over time. def print_table(velocity, angle, steps): x_velocity = velocity * math.cos(angle) y_velocity = velocity * math.sin(angle) total_time = -2.0 * y_velocity / ACCELERATION time_increment = total_time / steps x_increment = x_velocity * time_increment x = 0.0 y = 0.0 t = 0.0 print("step", "x", "y", "time", sep="\t") for i in range(steps + 2): print(i, round(x, 2), round(y, 2), round(t, 2), sep="\t") t += time_increment x += x_increment y = y_velocity * t + 0.5 * ACCELERATION * t * t def main(): intro() velocity, angle, steps = read_input() print_table(velocity, angle, steps) main()