Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- square = [0, 0, 0, 0, 10, 0, 10, 10, 0, 10, 0, 0, 0, 0, 0]
- obj_1st = [
- (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),
- (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),
- (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)
- ]
- obj_2nd = [
- (2.0, 3.0, 0, 3.0, 4.0, 0, 4.0, 3.0, 0, 2.0, 3.0, 0),
- (2.0, 6.0, 0, 3.0, 7.0, 0, 4.0, 6.0, 0, 2.0, 6.0, 0),
- (7.0, 3.0, 0, 8.0, 4.0, 0, 9.0, 3.0, 0, 7.0, 3.0, 0)
- ]
- def get_object_coords(obj):
- x = obj[0::3]
- y = obj[1::3]
- return x, y
- def check_collision(obj1, obj2):
- x1, y1 = get_object_coords(obj1)
- x2, y2 = get_object_coords(obj2)
- for i in range(len(x1)):
- for j in range(len(x2)):
- if np.sqrt((x1[i] - x2[j])**2 + (y1[i] - y2[j])**2) < 1:
- return True
- return False
- def move_objects(obj_1st, obj_2nd):
- for i, obj1 in enumerate(obj_1st):
- for j, obj2 in enumerate(obj_2nd):
- if check_collision(obj1, obj2):
- dx = np.random.uniform(-1, 1)
- dy = np.random.uniform(-1, 1)
- obj_1st[i] = tuple(x + dx for x in obj1)
- obj_2nd[j] = tuple(x + dy for x in obj2)
- return obj_1st, obj_2nd
- obj_1st, obj_2nd = move_objects(obj_1st, obj_2nd)
- def plot_lines(coords):
- x = coords[0::3]
- y = coords[1::3]
- plt.plot(x, y)
- import matplotlib.pyplot as plt
- plot_lines(square)
- for obj in obj_1st:
- plot_lines(obj)
- for obj in obj_2nd:
- plot_lines(obj)
- plt.axis('equal')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement