vehicle1.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 = Composition({
  11. 'wheel': Circle(center=(w_1, R), radius=R),
  12. 'cross': Composition({'cross1': Line((w_1,0), (w_1,2*R)),
  13. 'cross2': Line((w_1-R,R), (w_1+R,R))})})
  14. wheel2 = wheel1.copy()
  15. wheel2.translate((L,0))
  16. under = Rectangle(lower_left_corner=(w_1-2*R, 2*R),
  17. width=2*R + L + 2*R, height=H)
  18. over = Rectangle(lower_left_corner=(w_1, 2*R + H),
  19. width=2.5*R, height=1.25*H)
  20. wheels = Composition({'wheel1': wheel1, 'wheel2': wheel2})
  21. body = Composition({'under': under, 'over': over})
  22. vehicle = Composition({'wheels': wheels, 'body': body})
  23. ground = Wall(x=[R, xmax], y=[0, 0], thickness=-0.3*R)
  24. fig = Composition({'vehicle': vehicle, 'ground': ground})
  25. fig.draw() # send all figures to plotting backend
  26. drawing_tool.display()
  27. drawing_tool.savefig('tmp1.png')
  28. drawing_tool.savefig('tmp1.pdf')
  29. print fig
  30. import time
  31. time.sleep(1)
  32. from math import degrees
  33. # Animate motion
  34. w_1 += L # start position
  35. fig['vehicle'].translate((L,0)) # move whole figure to start position
  36. def v(t):
  37. return -8*R*t*(1 - t/(2*R))
  38. import numpy
  39. tp = numpy.linspace(0, 2*R, 25)
  40. dt = tp[1] - tp[0] # time step
  41. def move(t, fig):
  42. x_displacement = dt*v(t)
  43. fig['vehicle'].translate((x_displacement, 0))
  44. # Rotate wheels
  45. global w_1
  46. w_1 += x_displacement
  47. # R*angle = -x_displacement
  48. angle = - x_displacement/R
  49. w1 = fig['vehicle']['wheels']['wheel1']
  50. w1.rotate(degrees(angle), center=(w_1, R))
  51. w2 = fig['vehicle']['wheels']['wheel2']
  52. w2.rotate(degrees(angle), center=(w_1 + L, R))
  53. files = animate(fig, tp, move, moviefiles=True,
  54. pause_per_frame=0)
  55. files_wildcard = files.split('%')[0] + '*.png'
  56. os.system('convert -delay 20 %s* vehicle1.gif' % (files_wildcard))
  57. os.system('avconv -r 12 -i %s -c:v flv vehicle1.flv' % files)
  58. os.system('avconv -r 12 -i %s -c:v libvpx vehicle1.webm' % files)
  59. os.system('avconv -r 12 -i %s -c:v libtheora vehicle1.ogg' % files)
  60. os.system('avconv -r 12 -i %s -c:v flv vehicle1.flv' % files)
  61. os.system('avconv -r 12 -i %s -c:v libx264 -s:v 1000x520 vehicle1.mp4' % files)
  62. from scitools.std import movie
  63. movie(files_wildcard, encoder='html', output_file='vehicle1')
  64. raw_input()