from Tkinter import * #need Tkinter module for graphics from math import * #need math module for floor, pi, cos, sin, etc. #for canvas coordinate system canvas_width=400 canvas_height=400 #for our x-y coordinate system xmin=0 xmax=12 ymin=0 ymax=12 #The canvas coordinate system is different from usual x-y coordinate system #In the canvas system, the upper left corner is (0,0) and y points down. #In the our x-y system, the bottom left corner is (0,0) and y points up. #In both systems, x points to the right. #these two functions convert from our x-y system to the canvas system def canvas_x(x, xmin, xmax, canvas_width): #floor rounds down to nearest whole number return floor((x - xmin) * canvas_width / (xmax - xmin)) def canvas_y(y, ymin, ymax, canvas_height): return floor((ymax - y) * canvas_height / (ymax - ymin)) #create window master = Tk() #create 'canvas' that we draw lines on w = Canvas(master, width=canvas_width, height=canvas_height, bg='white') #actually displays canvas in window w.pack() def simulate_parabola(rx0, ry0, theta0, v0, ax, ay, dt): """ draws parabola and outputs final horiztonal position rx0 is initial x position ry0 is initial y position theta0 is initial direction of velocity v0 is initial magnitude of velocity ax is constant horizontal acceleration ay is constant horizontal acceleration dt is time increment size """ #store initial values of position and velocity vx = v0 * cos(theta0) vy = v0 * sin(theta0) rx = rx0 ry = ry0 #run simulation until impact with ground at y = 0 while (ry > 0 or vy > 0): #'c' for 'canvas' #'s' for 'start' of line segment cxs = canvas_x(rx, xmin, xmax, canvas_width) cys = canvas_y(ry, ymin, ymax, canvas_height) dvx = ax * dt dvy = ay * dt vx = vx + dvx vy = vy + dvy drx = vx * dt dry = vy * dt rx = rx + drx ry = ry + dry #'f' for 'finish' of line segment cxf = canvas_x(rx, xmin, xmax, canvas_width) cyf = canvas_y(ry, ymin, ymax, canvas_height) #make short line segment from old position (rx,ry) to #esimated new position (rx,ry) at time dt later. w.create_line(cxs, cys, cxf, cyf) return rx degree=pi/180. #1 degree equals pi/180 radians g=9.80 #graviational acceleration in meters/second/second v=10. #initial speed, i.e., magnitude of displacement, in meters/second print "Velocity is in meters/second." print "Angle is in degrees and measures initidal velocity angle with x-axis." print "Displacement is in meters and measured at impact." print print "(speed, angle, simulated displacement, theoretical displacement):" for angle in [15,30,45,60,75]: theta=angle * degree disp_sim = simulate_parabola(rx0 = 0, ry0 = 0, v0 = v, theta0 = theta, ax = 0., ay = -g, dt = 0.01) disp_theory = v**2 * sin(2 * theta) / g print (v, angle, round(disp_sim,3), round(disp_theory,3)) #actually display window mainloop()