# This program computes the trajectory of a projectile. # This version draws graphical output with a DrawingPanel. import math from DrawingPanel import * # constant for Earth acceleration in meters/second^2 ACCELERATION = -9.81 # projectile size on screen PROJECTILE_SIZE = 10 # 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 # create a graphical window panel = DrawingPanel(300, 120) x = 0.0 y = 0.0 t = 0.0 print("step", "x", "y", "time", sep="\t") for i in range(steps + 1): print(i, round(x, 2), round(y, 2), round(t, 2), sep="\t") draw_y = panel.height - PROJECTILE_SIZE - y # flip y axis panel.fill_oval(x, draw_y, PROJECTILE_SIZE, PROJECTILE_SIZE) panel.sleep(time_increment * 1000) 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()