Simulation and Modeling (CSCI 3010U)
Faculty of Science, Ontario Tech University
http://vclab.science.ontariotechu.ca
Discuss in class
Consider the following piece of code that pretends to detect if two cirles are colliding. Complete this code to make it work.
import matplotlib.pyplot as plt
import numpy as np
def plot_circles(circle1, circle2, collision):
fig, ax = plt.subplots(figsize=(6, 6))
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)
ax.set_aspect('equal')
# Draw circles
circle1_patch = plt.Circle((circle1['x'], circle1['y']), circle1['radius'], color='red' if collision else 'green', alpha=0.5, label='Circle 1')
circle2_patch = plt.Circle((circle2['x'], circle2['y']), circle2['radius'], color='red' if collision else 'green', alpha=0.5, label='Circle 2')
ax.add_patch(circle1_patch)
ax.add_patch(circle2_patch)
# Draw center points
ax.scatter([circle1['x'], circle2['x']], [circle1['y'], circle2['y']], color='black', marker='x', label='Centers')
# Collision state text
state_text = 'Collision!' if collision else 'No Collision'
ax.set_title(state_text, fontsize=14, fontweight='bold', color='red' if collision else 'black')
plt.legend()
plt.grid(True)
plt.show()
def check_circle_collision(circle1, circle2):
# TO DO
return False
# Define circles for collision case
circle1_collision = {'x': 30, 'y': 50, 'radius': 20}
circle2_collision = {'x': 50, 'y': 50, 'radius': 20}
collision = check_circle_collision(circle1_collision, circle2_collision)
plot_circles(circle1_collision, circle2_collision, collision)
# Define circles for non-collision case
circle1_no_collision = {'x': 20, 'y': 50, 'radius': 15}
circle2_no_collision = {'x': 70, 'y': 50, 'radius': 15}
no_collision = check_circle_collision(circle1_no_collision, circle2_no_collision)
plot_circles(circle1_no_collision, circle2_no_collision, no_collision)Now complete the following code that detects whether or not two convex polygons are colliding with each other.
# Collision detection in polygons - 2D case
# Faisal Z. Qureshi
# faisal.qureshi@ontariotechu.ca
#
# Adapted from ChatGPT
import numpy as np
import matplotlib.pyplot as plt
def polygons_collide(poly1, poly2):
"""Check for collision between two convex polygons using SAT."""
# TO DO
return False
def plot_polygons(poly1, poly2, collision):
"""Visualize two polygons and indicate collision status."""
fig, ax = plt.subplots(figsize=(6, 6))
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)
ax.set_aspect('equal')
# Convert polygon arrays to lists for plotting
poly1 = np.vstack([poly1, poly1[0]]) # Close the shape
poly2 = np.vstack([poly2, poly2[0]]) # Close the shape
# Draw polygons
ax.plot(poly1[:, 0], poly1[:, 1], 'b-', linewidth=2, label="Polygon 1")
ax.fill(poly1[:, 0], poly1[:, 1], 'blue', alpha=0.3)
ax.plot(poly2[:, 0], poly2[:, 1], 'r-', linewidth=2, label="Polygon 2")
ax.fill(poly2[:, 0], poly2[:, 1], 'red', alpha=0.3)
# Collision state text
state_text = "Collision!" if collision else "No Collision"
ax.set_title(state_text, fontsize=14, fontweight="bold", color="green" if collision else "red")
plt.legend()
plt.grid(True)
plt.show()
# Example polygons
polygon1 = np.array([[10, 10], [30, 10], [30, 30]]) #, [10, 30]]) # Square
polygon2 = np.array([[25, 25], [45, 25], [45, 45], [25, 45]]) # Overlapping Square
polygon3 = np.array([[50, 50], [70, 50], [70, 70], [50, 70]]) # Non-overlapping Square
# Check and plot collision case
collision1 = polygons_collide(polygon1, polygon2)
plot_polygons(polygon1, polygon2, collision1)
# Check and plot non-collision case
collision2 = polygons_collide(polygon1, polygon3)
plot_polygons(polygon1, polygon3, collision2)Via course Canvas.