vehicle0.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import sys, os, time
  2. sys.path.insert(0, os.path.join(os.pardir, os.pardir, os.pardir, os.pardir))
  3. from pysketcher import *
  4. drawing_tool.set_coordinate_system(xmin=0, xmax=15,
  5. ymin=-1, ymax=10)
  6. R = 1 # radius of wheel
  7. wheel1 = Circle(center=(4, R), radius=R)
  8. wheel2 = wheel1.copy()
  9. wheel2.translate((4,0))
  10. height1 = 2
  11. under = Rectangle(lower_left_corner=(2, 2*R),
  12. width=8, height=height1)
  13. height2 = 2.5
  14. over = Rectangle(lower_left_corner=(4, 2*R+height1),
  15. width=2.5, height=height2)
  16. wheels = Compose({'wheel1': wheel1, 'wheel2': wheel2})
  17. body = Compose({'under': under, 'over': over})
  18. vehicle = Compose({'wheels': wheels, 'body': body})
  19. ground = CurveWall(x=[0, 12], y=[0, 0], thickness=-0.3)
  20. fig = Compose({'vehicle': vehicle, 'ground': ground})
  21. fig.draw() # send all figures to plotting backend
  22. drawing_tool.display()
  23. drawing_tool.savefig('tmp1.png')
  24. fig['vehicle']['wheels'].set_filled_curves('blue')
  25. fig['vehicle']['wheels'].set_linewidth(6)
  26. fig['vehicle']['wheels'].set_linecolor('black')
  27. fig['vehicle']['body']['under'].set_filled_curves('red')
  28. fig['vehicle']['body']['over'].set_filled_curves(pattern='/')
  29. fig['vehicle']['body']['over'].set_linewidth(10)
  30. drawing_tool.erase() # avoid drawing old and new fig on top of each other
  31. fig.draw()
  32. drawing_tool.display()
  33. drawing_tool.savefig('tmp2.png')
  34. print fig
  35. time.sleep(1)
  36. # Animate motion
  37. fig['vehicle'].translate((3,0)) # move to start point for "driving"
  38. def v(t):
  39. return point(-t*(1-t/5.), 0)
  40. import numpy
  41. tp = numpy.linspace(0, 5, 35)
  42. dt = tp[1] - tp[0] # time step
  43. def move_vehicle(t, fig):
  44. displacement = dt*v(t)
  45. fig['vehicle'].translate(displacement)
  46. files = animate(fig, tp, move_vehicle, moviefiles=True,
  47. pause_per_frame=0)
  48. from scitools.std import movie
  49. movie(files, encoder='html', output_file='anim')
  50. raw_input()