Program to check two spheres can ever meet by accelerating or not in a 3D space in Python

Suppose there are two spheres with radius values r1 and r2. They are at coordinates (x1, y1, z1) and (x2, y2, z2). Their acceleration values are given as (ax1, ay1, az1) and (ax2, ay2, az2). We need to check whether these two spheres will ever meet in 3D space if they move with given accelerations.

So, if the input is like r1 = 1, r2 = 2, pos1 = (0, 0, 0), acc1 = (100, 0, 0), pos2 = (4, 0, 0), acc2 = (0, 0, 0), then the output will be True, because the second sphere has no acceleration and remains stationary, but the first sphere moves in the x direction, so they will collide.

Algorithm

To solve this problem, we follow these steps ?

  • Calculate relative position: px = pos1[0] - pos2[0], py = pos1[1] - pos2[1], pz = pos1[2] - pos2[2]
  • Calculate relative acceleration: ax = acc1[0] - acc2[0], ay = acc1[1] - acc2[1], az = acc1[2] - acc2[2]
  • Calculate da = ax² + ay² + az² (squared magnitude of relative acceleration)
  • Calculate dp = px² + py² + pz² (squared distance between centers)
  • Calculate co = ax × px + ay × py + az × pz (dot product)
  • Find the time when spheres are closest: x = -co / da (if da ? 0)
  • Ensure x ? 0 (we only consider future time)
  • Calculate minimum distance: dis = ?(da × x² + 2 × co × x + dp)
  • If dis ? r1 + r2, spheres will meet

Example

Let us see the following implementation to get better understanding ?

import math

def solve(r1, r2, pos1, acc1, pos2, acc2):
    # Calculate relative position
    px, py, pz = pos1[0] - pos2[0], pos1[1] - pos2[1], pos1[2] - pos2[2]
    
    # Calculate relative acceleration
    ax, ay, az = acc1[0] - acc2[0], acc1[1] - acc2[1], acc1[2] - acc2[2]
    
    # Calculate coefficients
    da = (ax * ax + ay * ay + az * az)
    dp = (px * px + py * py + pz * pz)
    co = (ax * px + ay * py + az * pz)

    x = 0.0
    if da != 0:
        x = -co / da
    
    # Consider only future time (x >= 0)
    x = max(x, 0)
    
    # Calculate minimum distance
    dis = math.sqrt(da * x * x + 2 * co * x + dp)
    
    # Check if spheres can meet
    return dis <= r1 + r2

# Test case 1
r1 = 1
r2 = 2
pos1 = (0, 0, 0)
acc1 = (100, 0, 0)
pos2 = (4, 0, 0)
acc2 = (0, 0, 0)
print(f"Test 1: {solve(r1, r2, pos1, acc1, pos2, acc2)}")

# Test case 2: Spheres moving away from each other
pos1 = (0, 0, 0)
acc1 = (-10, 0, 0)
pos2 = (10, 0, 0)
acc2 = (10, 0, 0)
print(f"Test 2: {solve(r1, r2, pos1, acc1, pos2, acc2)}")

The output of the above code is ?

Test 1: True
Test 2: False

How It Works

The algorithm uses physics principles to find the minimum distance between two accelerating spheres:

  • Relative motion: We consider one sphere stationary and calculate the other's relative motion
  • Quadratic equation: The distance function is quadratic in time, so we find its minimum
  • Future time only: We only consider t ? 0 since we can't change the past
  • Collision condition: Spheres meet if minimum distance ? sum of their radii

Conclusion

This algorithm efficiently determines if two accelerating spheres will collide by calculating their minimum distance over time. The solution works by transforming the problem into relative motion and finding the closest approach point using calculus.

Updated on: 2026-03-26T15:09:21+05:30

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements