## CSE 142 Python, Autumn 2008 ## This program displays the motion of a thrown projectile over time. ## ## Written in Java by Marty Stepp ## Ported to Python by Ian Gienger from math import * from drawingpanel import * GRAVITY = -9.81 # Plots the motion of a single projectile with the given # initial velocity v0, angle, and number of steps to plot. def projectile(panel, v0, angle, steps, color="black"): # v0y = v0 sin theta, v0x = v0 cos theta v0y = v0 * sin(radians(angle)) v0x = v0 * cos(radians(angle)) # t = -2v0 / a totalTime = -2 * v0y / GRAVITY dt = totalTime / steps print " x y time" for i in range(steps + 1): t = i * dt x = v0x * t y = displacement(v0y, GRAVITY, t) print "%8.2f %8.2f %8.2f \n" % (x, y, t) panel.canvas.create_oval(x, 300 - y, x + 5, 305 - y, fill=color, outline=color) panel.sleep(dt * 100) # Computes and returns the change in position of a projectile # given its initial velocity v0, acceleration a, and time t. def displacement(v0, a, t): return v0 * t + 0.5 * a * t**2 # Main panel = DrawingPanel(400, 300) projectile(panel, 30, 85, 20) projectile(panel, 60, 85, 50, color="red") projectile(panel, 120, 85, 100, color="green")