vehicle0.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from pysketcher import *
  2. R = 1 # radius of wheel
  3. L = 4 # distance between wheels
  4. H = 2 # height of vehicle body
  5. w_1 = 5 # position of front wheel
  6. xmax = w_1 + 2*L + 3*R
  7. drawing_tool.set_coordinate_system(xmin=0, xmax=xmax,
  8. ymin=-1, ymax=2*R + 3*H,
  9. axis=False)
  10. wheel1 = Circle(center=(w_1, R), radius=R)
  11. wheel2 = wheel1.copy()
  12. wheel2.translate((L,0))
  13. under = Rectangle(lower_left_corner=(w_1-2*R, 2*R),
  14. width=2*R + L + 2*R, height=H)
  15. over = Rectangle(lower_left_corner=(w_1, 2*R + H),
  16. width=2.5*R, height=1.25*H)
  17. wheels = Composition({'wheel1': wheel1, 'wheel2': wheel2})
  18. body = Composition({'under': under, 'over': over})
  19. vehicle = Composition({'wheels': wheels, 'body': body})
  20. ground = Wall(x=[R, xmax], y=[0, 0], thickness=-0.3*R)
  21. fig = Composition({'vehicle': vehicle, 'ground': ground})
  22. fig.draw() # send all figures to plotting backend
  23. drawing_tool.display()
  24. drawing_tool.savefig('tmp1.png')
  25. fig['vehicle']['wheels'].set_filled_curves('blue')
  26. fig['vehicle']['wheels'].set_linewidth(6)
  27. fig['vehicle']['wheels'].set_linecolor('black')
  28. fig['vehicle']['body']['under'].set_filled_curves('red')
  29. fig['vehicle']['body']['over'].set_filled_curves(pattern='/')
  30. fig['vehicle']['body']['over'].set_linewidth(14)
  31. drawing_tool.erase() # avoid drawing old and new fig on top of each other
  32. fig.draw()
  33. drawing_tool.display()
  34. drawing_tool.savefig('tmp2.png')
  35. print fig
  36. fig.recurse('fig')
  37. fig.graphviz_dot('fig', False)
  38. import time
  39. time.sleep(1)
  40. # Animate motion
  41. fig['vehicle'].translate((L,0)) # move to start point for "driving"
  42. def v(t):
  43. return -8*R*t*(1 - t/(2*R))
  44. import numpy
  45. tp = numpy.linspace(0, 2*R, 25)
  46. dt = tp[1] - tp[0] # time step
  47. def move_vehicle(t, fig):
  48. x_displacement = dt*v(t)
  49. fig['vehicle'].translate((x_displacement, 0))
  50. files = animate(fig, tp, move_vehicle, moviefiles=True,
  51. pause_per_frame=0)
  52. os.system('convert -delay 20 %s anim.gif' % files)
  53. os.system('ffmpeg -i "tmp_frame_%04d.png" -b 800k -r 25 -vcodec mpeg4 -y -qmin 2 -qmax 31 anim.mpeg')
  54. try:
  55. from scitools.std import movie
  56. except ImportError:
  57. raise ImportError(
  58. 'scitools must be installed for running the "movie" function.\n'
  59. 'scitools is installed by sudo apt-get install python-scitools\n'
  60. 'on Ubuntu or by sudo python setup.py install if the code is\n'
  61. 'downloaded from http://code.google.com/p/scitools.')
  62. # HTML page showing individual frames
  63. movie(files, encoder='html', fps=4, output_file='anim.html')
  64. # Standard GIF file
  65. movie(files, encoder='convert', fps=4, output_file='anim2.gif')
  66. # AVI format
  67. movie('tmp_*.png', encoder='ffmpeg', fps=4,
  68. output_file='anim.avi') # requires ffmpeg package
  69. # MPEG format
  70. movie('tmp_*.png', encoder='ffmpeg', fps=4,
  71. output_file='anim3.mpeg', vodec='mpeg2video')
  72. # or
  73. movie(files, encoder='ppmtompeg', fps=24,
  74. output_file='anim2.mpeg') # requires the netpbm package
  75. raw_input()