bouncingball 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #两个刚性小球在指定范围内碰撞,1个有轨迹,1个没有
  2. from stdpackage import stddraw
  3. stddraw.setXscale(-1.0,1.0)
  4. stddraw.setYscale(-1.0,1.0)
  5. DT = 20
  6. RADIUS = 0.1
  7. r1x = 0.480
  8. r1y = 0.860
  9. v1x = 0.015
  10. v1y = 0.023
  11. position1 = [(r1x,r1y)]
  12. r2x = 0.080
  13. r2y = -0.360
  14. v2x = 0.03
  15. v2y = 0.02
  16. position2 = [(r2x,r2y)]
  17. changepoint = []
  18. while True:
  19. if (abs(r1x+v1x-r2x-v2x)<2*RADIUS and abs(r1y+v1y-r2y-v2y)<2*RADIUS):
  20. print(r1x,v1x,r2x,v2x,'change')
  21. a = v1x
  22. v1x = v2x
  23. v2x = a
  24. changepoint.append((r1x,r1y,r2x,r2y))
  25. # stddraw.line(r1x,r1y,r2x,r2y)
  26. # stddraw.show()
  27. a = v1y
  28. v1y = v2y
  29. v2y = a
  30. # break
  31. if abs(r1x+v1x)+RADIUS>1.0: v1x = -v1x
  32. if abs(r1y + v1y)+RADIUS >1.0:v1y = -v1y
  33. if abs(r2x+v2x)+RADIUS>1.0: v2x = -v2x
  34. if abs(r2y + v2y)+RADIUS >1.0:v2y = -v2y
  35. r1x = r1x+v1x
  36. r1y = r1y + v1y
  37. r2x = r2x+v2x
  38. r2y = r2y + v2y
  39. position1.append((r1x,r1y))
  40. stddraw.clear(stddraw.LIGHT_GRAY)
  41. stddraw.setPenColor(stddraw.GRAY)
  42. for x,y in position1[-50:]:
  43. print(x,y)
  44. stddraw.circle(x,y,RADIUS)
  45. stddraw.setPenColor(stddraw.BLACK)
  46. stddraw.filledCircle(r1x,r1y,RADIUS)
  47. stddraw.filledCircle(r2x,r2y,RADIUS)
  48. stddraw.show(DT)