wheel_on_inclined_plane.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from pysketcher import *
  2. def inclined_plane():
  3. theta = 30.
  4. L = 10.
  5. a = 1.
  6. xmin = 0
  7. ymin = -3
  8. drawing_tool.set_coordinate_system(xmin=xmin, xmax=xmin+1.5*L,
  9. ymin=ymin, ymax=ymin+L,
  10. #axis=True,
  11. instruction_file='tmp_mpl.py'
  12. )
  13. #drawing_tool.set_grid(True)
  14. fontsize = 18
  15. from math import tan, radians
  16. B = point(a+L, 0)
  17. A = point(a, tan(radians(theta))*L)
  18. wall = Wall(x=[A[0], B[0]], y=[A[1], B[1]], thickness=-0.25,
  19. transparent=False)
  20. angle = Arc_wText(r'$\theta$', center=B, radius=3,
  21. start_angle=180-theta, arc_angle=theta,
  22. fontsize=fontsize)
  23. angle.set_linecolor('black')
  24. angle.set_linewidth(1)
  25. ground = Line((B[0]-L/10., 0), (B[0]-L/2.,0))
  26. ground.set_linecolor('black')
  27. ground.set_linestyle('dashed')
  28. ground.set_linewidth(1)
  29. r = 1 # radius of wheel
  30. help_line = Line(A, B)
  31. x = a + 3*L/10.; y = help_line(x=x)
  32. contact = point(x, y)
  33. normal_vec = point(sin(radians(theta)), cos(radians(theta)))
  34. c = contact + r*normal_vec
  35. outer_wheel = Circle(c, r)
  36. outer_wheel.set_linecolor('blue')
  37. outer_wheel.set_filled_curves('blue')
  38. hole = Circle(c, r/2.)
  39. hole.set_linecolor('blue')
  40. hole.set_filled_curves('white')
  41. wheel = Composition({'outer': outer_wheel, 'inner': hole})
  42. wheel.set_shadow(4)
  43. drawing_tool.set_linecolor('black')
  44. N = Force(contact - 2*r*normal_vec, contact, r'$N$', text_pos='start')
  45. #text_alignment='left')
  46. mg = Gravity(c, 3*r, text='$Mg$')
  47. x_const = Line(contact, contact + point(0,4))
  48. x_const.set_linestyle('dotted')
  49. x_const.rotate(-theta, contact)
  50. # or x_const = Line(contact-2*r*normal_vec, contact+4*r*normal_vec).set_linestyle('dotted')
  51. x_axis = Axis(start=contact+ 3*r*normal_vec, length=4*r,
  52. label='$x$', rotation_angle=-theta)
  53. body = Composition({'wheel': wheel, 'N': N, 'mg': mg})
  54. fixed = Composition({'angle': angle, 'inclined wall': wall,
  55. 'wheel': wheel, 'ground': ground,
  56. 'x start': x_const, 'x axis': x_axis})
  57. fig = Composition({'body': body, 'fixed elements': fixed})
  58. fig.draw()
  59. drawing_tool.savefig('tmp.png')
  60. drawing_tool.display()
  61. import time
  62. time.sleep(1)
  63. tangent_vec = point(normal_vec[1], -normal_vec[0])
  64. import numpy
  65. time_points = numpy.linspace(0, 1, 31)
  66. def position(t):
  67. """Position of center point of wheel."""
  68. return c + 7*t**2*tangent_vec
  69. def move(t, fig, dt=None):
  70. x = position(t)
  71. x0 = position(t-dt)
  72. displacement = x - x0
  73. fig['body'].translate(displacement)
  74. animate(fig, time_points, move, pause_per_frame=0,
  75. dt=time_points[1]-time_points[0])
  76. print str(fig)
  77. print repr(fig)
  78. inclined_plane()
  79. raw_input()