Advertisement
bobbuban

Untitled

May 7th, 2024
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import numpy as np
  2.  
  3. square = [0, 0, 0, 0, 10, 0, 10, 10, 0, 10, 0, 0, 0, 0, 0]
  4.  
  5. obj_1st = [
  6. (1.0, 8.0, 0, 3.0, 8.0, 0, 3.0, 6.0, 0, 1.0, 6.0, 0, 1.0, 8.0, 0),
  7. (6.0, 7.0, 0, 8.0, 7.0, 0, 8.0, 5.0, 0, 6.0, 5.0, 0, 6.0, 7.0, 0),
  8. (3.0, 4.0, 0, 5.0, 4.0, 0, 5.0, 2.0, 0, 3.0, 2.0, 0, 3.0, 4.0, 0)
  9. ]
  10.  
  11. obj_2nd = [
  12. (2.0, 3.0, 0, 3.0, 4.0, 0, 4.0, 3.0, 0, 2.0, 3.0, 0),
  13. (2.0, 6.0, 0, 3.0, 7.0, 0, 4.0, 6.0, 0, 2.0, 6.0, 0),
  14. (7.0, 3.0, 0, 8.0, 4.0, 0, 9.0, 3.0, 0, 7.0, 3.0, 0)
  15. ]
  16.  
  17. def get_object_coords(obj):
  18. x = obj[0::3]
  19. y = obj[1::3]
  20. return x, y
  21.  
  22. def check_collision(obj1, obj2):
  23. x1, y1 = get_object_coords(obj1)
  24. x2, y2 = get_object_coords(obj2)
  25. for i in range(len(x1)):
  26. for j in range(len(x2)):
  27. if np.sqrt((x1[i] - x2[j])**2 + (y1[i] - y2[j])**2) < 1:
  28. return True
  29. return False
  30.  
  31. def move_objects(obj_1st, obj_2nd):
  32. for i, obj1 in enumerate(obj_1st):
  33. for j, obj2 in enumerate(obj_2nd):
  34. if check_collision(obj1, obj2):
  35. dx = np.random.uniform(-1, 1)
  36. dy = np.random.uniform(-1, 1)
  37. obj_1st[i] = tuple(x + dx for x in obj1)
  38. obj_2nd[j] = tuple(x + dy for x in obj2)
  39. return obj_1st, obj_2nd
  40.  
  41. obj_1st, obj_2nd = move_objects(obj_1st, obj_2nd)
  42.  
  43. def plot_lines(coords):
  44. x = coords[0::3]
  45. y = coords[1::3]
  46. plt.plot(x, y)
  47.  
  48. import matplotlib.pyplot as plt
  49.  
  50. plot_lines(square)
  51.  
  52. for obj in obj_1st:
  53. plot_lines(obj)
  54.  
  55. for obj in obj_2nd:
  56. plot_lines(obj)
  57.  
  58. plt.axis('equal')
  59. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement