Advertisement
Kevin_Zhang

Untitled

Oct 11th, 2021
943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. #!/bin/python
  2. from vpython import *
  3.  
  4.  
  5. size = .25
  6. theta = pi/4
  7. init_pos = vec(-15, size, 0)
  8. init_v = vec(20*cos(theta), 20*sin(theta), 0)
  9.  
  10. dt = 1e-4
  11. C_drag = 0.9
  12. g = 9.8
  13.  
  14. bgc = vec(59/256, 114/256, 165/256)
  15.  
  16. scene = canvas(center = vec(0,5,0), width=600, background=bgc)
  17. floor = box(length=50, height=0.01, width=4, color=color.blue)
  18. ball = sphere(radius = size, color=color.red, make_trail = True, trail_radius = 0.05) # the ball
  19.  
  20. table = graph(width = 300, align = 'left')
  21. fv = gcurve(graph = table, color=color.blue, width=4)
  22.  
  23. a1 = arrow(color = color.green, shaftwidth = 0.1)
  24. a1.axis = vec(1, -1, 0)
  25. ball.pos = init_pos
  26. ball.v = init_v
  27.  
  28. t = 0
  29. cnt_g = 0
  30. pcnt = 0
  31. sum_dis = 0
  32. max_h = 0
  33.  
  34. def hypot(a, b):
  35.     return (a*a+b*b)**.5
  36.  
  37. def movetheball(dt, ball):
  38.     global sum_dis
  39.     global max_h
  40.     max_h = max(max_h, ball.pos.y)
  41.     ball.pos += ball.v*dt
  42.     sum_dis += mag(ball.v) * dt
  43.     ball.v += vec(0, -g, 0) * dt - C_drag*ball.v*dt
  44.     if ball.pos.y <= size and ball.v.y < 0: # new: check if ball hits the ground
  45.         ball.v.y *= -1
  46.         return 1;
  47.     return 0;
  48.  
  49. def putarrow():
  50.     a1.axis = ball.v / 5
  51.     a1.pos = ball.pos - a1.axis
  52.     a1.axis += vec(0, 0, 1)
  53.     a1.pos += vec(0, 0, 1)
  54.  
  55. while cnt_g < 3:
  56.     rate(1/dt)
  57.     t += dt
  58.     pcnt += 1
  59.     cnt_g += movetheball(dt, ball)
  60.     if pcnt == 1000:
  61.         fv.plot( pos=(t, hypot(ball.v.x, ball.v.y)) )
  62.         pcnt = 0
  63.     putarrow()
  64.  
  65.  
  66. show_pos = vec(-10, 14, 0)
  67. diff_pos = vec(0, 2, 0)
  68. text(text="Displacement:" + str(mag(ball.pos)), pos = show_pos)
  69. text(text="Distance Traveled:" + str(sum_dis), pos = show_pos-diff_pos)
  70. text(text="Maximum Height:" + str(max_h), pos = show_pos-diff_pos*2)
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement