Finding and Visualizing Line-Circle Intersections with Python

Let’s solve s geometry problem with $Python$ and visualize the result with a graph.

Here’s the problem:

Problem: Line-Circle Intersection

Given a circle and a line in a 2D plane, find the intersection points of the line and the circle, and then plot them.

Setup

  1. Circle: Defined by its center $(x_c, y_c)$ and radius $(r)$.
  2. Line: Defined by the linear equation $(y = mx + b)$, where $(m)$ is the slope, and $(b)$ is the y-intercept.

Solution Outline

To find the intersection points:

  1. Substitute the line equation $(y = mx + b)$ into the circle equation $(x - x_c)^2 + (y - y_c)^2 = r^2$.
  2. Simplify to a quadratic equation in terms of $(x)$.
  3. Solve the quadratic equation:
    • If there are no real solutions, the line does not intersect the circle.
    • One real solution indicates a tangent (one intersection point).
    • Two real solutions indicate two intersection points.
  4. Plot the circle, line, and intersection points.

Python Code

Here’s the $Python$ code for finding the intersection points and plotting them.

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
import numpy as np
import matplotlib.pyplot as plt

def find_line_circle_intersections(xc, yc, r, m, b):
# Solve for x in the quadratic equation
A = 1 + m**2
B = -2 * xc + 2 * m * (b - yc)
C = xc**2 + (b - yc)**2 - r**2

discriminant = B**2 - 4 * A * C

# If discriminant is negative, no real intersection
if discriminant < 0:
return "No intersection"

# Calculate the x-coordinates of intersection points
x1 = (-B + np.sqrt(discriminant)) / (2 * A)
x2 = (-B - np.sqrt(discriminant)) / (2 * A)

# Calculate the corresponding y-coordinates
y1 = m * x1 + b
y2 = m * x2 + b

return [(x1, y1), (x2, y2)] if discriminant > 0 else [(x1, y1)]

# Define the circle and line parameters
xc, yc, r = 0, 0, 5 # Circle with center (0, 0) and radius 5
m, b = 1, 1 # Line with slope 1 and y-intercept 1

# Find the intersection points
intersections = find_line_circle_intersections(xc, yc, r, m, b)

# Set up the plot
theta = np.linspace(0, 2 * np.pi, 100)
x_circle = xc + r * np.cos(theta)
y_circle = yc + r * np.sin(theta)

# Plot the circle
plt.plot(x_circle, y_circle, label='Circle', color='blue')

# Plot the line
x_vals = np.linspace(-r-2, r+2, 100)
y_vals = m * x_vals + b
plt.plot(x_vals, y_vals, label='Line', color='red')

# Plot the intersection points, if any
if intersections != "No intersection":
for (x, y) in intersections:
plt.plot(x, y, 'go') # Green dots for intersection points
plt.text(x, y, f'({x:.2f}, {y:.2f})', ha='right')

# Add labels and title
plt.xlabel('x')
plt.ylabel('y')
plt.axhline(0, color='black',linewidth=0.5)
plt.axvline(0, color='black',linewidth=0.5)
plt.title('Intersection of a Circle and a Line')
plt.legend()
plt.grid(True)
plt.axis('equal')
plt.show()

Explanation of the Code

  1. Intersection Calculation:
    • The equation of the circle $(x - x_c)^2 + (y - y_c)^2 = r^2$ and the line equation $y = mx + b$ are combined.
    • Solving the resulting quadratic equation allows us to find the intersection points (if they exist).
  2. Plotting:
    • The circle is drawn by parameterizing it with $(\theta)$.
    • The line is plotted using a range of $(x)$ values.
    • The intersection points (if any) are marked with green dots and labeled with their coordinates.

Visualization

The output graph displays:

  • The circle and line in blue and red, respectively.
  • Green dots indicate the intersection points with coordinates labeled for easy interpretation.

This graph illustrates the intersection of a circle and a line in a 2D plane.

Key Elements:

  1. Circle (blue line): Centered at the origin $(0, 0)$ with a radius of $5$.
  2. Line (red line): Represented by the equation $(y = x + 1)$, which has a slope of $1$ and a y-intercept of $1$.

Intersection Points:

The green dots mark the two intersection points where the line crosses the circle:

  • Point 1: Located at approximately $(-4.00, -3.00)$.
  • Point 2: Located at approximately $(3.00, 4.00)$.

Each point is labeled with its coordinates, making it easy to see the exact locations of intersection.

This visualization effectively shows where the line and circle meet, providing a clear solution to the problem of finding their intersection points.