wheel_on_inclined_plane.py 2.9 KB

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