Check whether given circle resides in boundary maintained by two other circles in Python

Sometimes we need to check whether a given circle lies within the boundary formed by two concentric circles. This is a common geometric problem that can be solved using distance calculations and boundary conditions.

Problem Understanding

We have two concentric circles with radii r1 (outer) and r2 (inner), both centered at the origin. Given another circle with center at coordinates coord and radius r, we need to determine if this circle fits completely within the annular region (ring-shaped area) between the two concentric circles.

r1 = 4 r2 = 2 r = 1 (3, 0)

Solution Approach

The key insight is to calculate the distance from the origin to the center of the test circle, then check if the entire circle (center ± radius) falls within the allowed boundary ?

  1. Calculate distance val from origin to the test circle's center
  2. Check if val + r ? r1 (circle doesn't exceed outer boundary)
  3. Check if val - r ? r1 - r2 (circle doesn't cross inner boundary)

Implementation

from math import pow, sqrt

def check_circle_boundary(r1, r2, coord, r):
    # Calculate distance from origin to circle center
    val = sqrt(pow(coord[0], 2) + pow(coord[1], 2))
    
    # Check if circle fits within the annular region
    if val + r <= r1 and val - r >= r1 - r2:
        return True
    return False

# Test case
r1 = 4  # Outer circle radius
r2 = 2  # Inner circle radius
coord = (3, 0)  # Test circle center
r = 1   # Test circle radius

result = check_circle_boundary(r1, r2, coord, r)
print(f"Circle at {coord} with radius {r} fits in boundary: {result}")
Circle at (3, 0) with radius 1 fits in boundary: True

How It Works

For the example with r1=4, r2=2, coord=(3,0), and r=1 ?

from math import sqrt

# Calculate distance from origin to (3, 0)
distance = sqrt(3**2 + 0**2)
print(f"Distance from origin: {distance}")

# Check conditions
outer_condition = distance + 1 <= 4  # 3 + 1 = 4 ? 4 ?
inner_condition = distance - 1 >= 4 - 2  # 3 - 1 = 2 ? 2 ?

print(f"Outer boundary check: {distance} + 1 ? 4 ? {outer_condition}")
print(f"Inner boundary check: {distance} - 1 ? 2 ? {inner_condition}")
print(f"Result: {outer_condition and inner_condition}")
Distance from origin: 3.0
Outer boundary check: 3.0 + 1 ? 4 ? True
Inner boundary check: 3.0 - 1 ? 2 ? True
Result: True

Edge Cases

# Test various positions
test_cases = [
    (4, 2, (0, 0), 1),    # Center circle
    (4, 2, (4, 0), 1),    # Too far out
    (4, 2, (1, 0), 1),    # Too close to inner
    (4, 2, (2.5, 0), 0.5) # Perfect fit
]

for r1, r2, coord, r in test_cases:
    result = check_circle_boundary(r1, r2, coord, r)
    print(f"r1={r1}, r2={r2}, coord={coord}, r={r}: {result}")
r1=4, r2=2, coord=(0, 0), r=1: False
r1=4, r2=2, coord=(4, 0), r=1: False
r1=4, r2=2, coord=(1, 0), r=1: False
r1=4, r2=2, coord=(2.5, 0), r=0.5: True

Conclusion

This geometric problem is solved by calculating the distance from the origin and checking boundary conditions. The circle must be entirely within the outer radius and entirely outside the inner boundary region.

Updated on: 2026-03-25T14:34:37+05:30

216 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements