Practical Example in Geometry:Finding the Intersection of Two Circles

We solve a common geometric problem: determining the intersection points of two circles.

This has applications in fields like $computer$ $graphics$, $robotics$, and $navigation$.


Problem Statement

Two circles are defined by:

  1. Circle 1: Center $(x_1, y_1)$, radius $r_1$.
  2. Circle 2: Center $(x_2, y_2)$, radius $r_2$.

The task is to find the intersection points of these circles.

If they intersect, there can be two points, one point (tangent), or no intersection.


Python Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import numpy as np
import matplotlib.pyplot as plt

def find_circle_intersection(x1, y1, r1, x2, y2, r2):
# Distance between circle centers
d = np.sqrt((x2 - x1)**2 + (y2 - y1)**2)

# Check for no intersection or containment
if d > r1 + r2 or d < abs(r1 - r2):
return None, None # No intersection

# Check for tangency
if d == 0 and r1 == r2:
return "Infinite", None # Infinite intersections (identical circles)

# Calculate intersection points
a = (r1**2 - r2**2 + d**2) / (2 * d)
h = np.sqrt(r1**2 - a**2)

# Midpoint between circle centers
x3 = x1 + a * (x2 - x1) / d
y3 = y1 + a * (y2 - y1) / d

# Offset for intersection points
x_intersect1 = x3 + h * (y2 - y1) / d
y_intersect1 = y3 - h * (x2 - x1) / d

x_intersect2 = x3 - h * (y2 - y1) / d
y_intersect2 = y3 + h * (x2 - x1) / d

return (x_intersect1, y_intersect1), (x_intersect2, y_intersect2)

# Example: Define two circles
x1, y1, r1 = 0, 0, 5
x2, y2, r2 = 6, 0, 5

# Find intersection points
intersection1, intersection2 = find_circle_intersection(x1, y1, r1, x2, y2, r2)

# Visualization
circle1 = plt.Circle((x1, y1), r1, color='blue', fill=False, label='Circle 1')
circle2 = plt.Circle((x2, y2), r2, color='red', fill=False, label='Circle 2')

fig, ax = plt.subplots(figsize=(8, 8))
ax.add_artist(circle1)
ax.add_artist(circle2)
plt.scatter([x1, x2], [y1, y2], color='black', label='Centers') # Centers of circles

# Plot intersection points if they exist
if intersection1 and intersection2:
plt.scatter([intersection1[0], intersection2[0]], [intersection1[1], intersection2[1]],
color='green', label='Intersection Points')
elif intersection1 == "Infinite":
plt.text(0, 0, "Infinite Intersections (Identical Circles)", color="purple")

# Adjust plot
plt.axhline(0, color='gray', linestyle='--', linewidth=0.5)
plt.axvline(0, color='gray', linestyle='--', linewidth=0.5)
plt.xlim(-10, 15)
plt.ylim(-10, 10)
plt.gca().set_aspect('equal', adjustable='box')
plt.legend()
plt.title("Intersection of Two Circles")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid()
plt.show()

Explanation of Code

  1. Distance Check:

    • The distance $( d )$ between the circle centers determines whether they intersect:
      • $( d > r_1 + r_2 )$: No intersection (too far apart).
      • $( d < |r_1 - r_2| )$: No intersection (one circle is contained within the other).
      • $( d = 0 )$ and $( r_1 = r_2 )$: Infinite intersections (identical circles).
  2. Intersection Calculation:

    • The formula for $( a )$ calculates the distance from the first circle’s center to the midpoint between intersection points.
    • $( h )$ is the perpendicular distance from the midpoint to the intersection points.
  3. Visualization:

    • The circles are drawn with plt.Circle.
    • Intersection points are plotted in green, and the centers are marked in black.

Key Outputs

  1. Intersection Points:

    • The two green points indicate where the circles intersect.
    • If no points exist, the code returns None.
  2. Graph:

    • The visualization shows the circles, their centers, and their intersection points clearly.

This example can be extended to solve 3D sphere intersection problems or optimized for large datasets of circles.