DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   python (http://www.daniweb.com/code/python.html)
-   -   Graphing Projectile Motion with wxPython (http://www.daniweb.com/code/snippet842.html)

vegaseat python syntax
Mar 27th, 2008
This well commented Python snippet uses wxPython's plotting widget to graph the projectile motion of two different firing angles on the same plotting canvas.

  1. # using wxPython for plotting projectile motion
  2. # draw line graph for 2 different firing angles
  3. # tested with Python25 and wxPython28 vegaseat 26mar2008
  4.  
  5. import wx
  6. import wx.lib.plot as plot
  7. import math
  8.  
  9. class MyFrame(wx.Frame):
  10. def __init__(self):
  11. self.frame1 = wx.Frame(None, title="wx.lib.plot", id=-1, size=(410, 340))
  12. self.panel1 = wx.Panel(self.frame1)
  13. self.panel1.SetBackgroundColour("yellow")
  14. # set up the plot canvas
  15. plotter = plot.PlotCanvas(self.panel1)
  16. plotter.SetInitialSize(size=(400, 300))
  17. # enable the zoom feature (drag a box around area of interest)
  18. plotter.SetEnableZoom(True)
  19.  
  20. # projectile motion equations:
  21. # height = y(t) = h0 + (t * v * sin(a)) - (g * t*t)/2
  22. # range = x(t) = v * cos(a) * t
  23. # where:
  24. # v is the muzzle velocity of the projectile (meter/second)
  25. v = 100
  26. # a is the firing angle with repsect to ground (radians)
  27. # h0 is starting height with respect to ground (meters)
  28. h0 = 0
  29. # g is the gravitational pull (meters/second_square)
  30. g = 9.8
  31.  
  32. # now calculate the list of (x, y) data point tuples ...
  33. # x axis is range (distance) in meters
  34. # y axis is height in meters
  35. # first for a firing angle of 45 degrees
  36. d = 45
  37. a = math.radians(d) # gives radians
  38. data45 = []
  39. for t in range(0, 200):
  40. # use the time in increments of tx (0.1) seconds
  41. tx = t/10.0
  42. # now calculate the height y
  43. y = h0 + (tx * v * math.sin(a)) - (g * tx * tx)/2
  44. # calculate the range x
  45. x = v * math.cos(a) * tx
  46. # append the (x, y) tple to the list
  47. data45.append((x, y))
  48.  
  49. # now for a firing angle of 30 degrees
  50. d = 30
  51. a = math.radians(d) # gives radians
  52. data30 = []
  53. for t in range(0, 200):
  54. # use the time in increments of tx (0.1) seconds
  55. tx = t/10.0
  56. # now calculate the height y
  57. y = h0 + (tx * v * math.sin(a)) - (g * tx * tx)/2
  58. # calculate the range x
  59. x = v * math.cos(a) * tx
  60. # append the (x, y) tuple to the data list
  61. data30.append((x, y))
  62.  
  63. # draw points as a line
  64. # 2 different lines one for an angle of 45 one of 30 degrees
  65. line45 = plot.PolyLine(data45, colour='red', width=1)
  66. line30 = plot.PolyLine(data30, colour='blue', width=1)
  67. # set up the plot
  68. gc = plot.PlotGraphics([line45, line30],
  69. 'Projetile Motion',
  70. 'range (meters) firing angle red=45, blue=30',
  71. 'height (meters)')
  72. # and draw it
  73. plotter.Draw(gc, xAxis=(0,1200), yAxis=(0,300))
  74.  
  75. self.frame1.Show(True)
  76.  
  77.  
  78. app = wx.PySimpleApp()
  79. f = MyFrame()
  80. app.MainLoop()