You are given a list of circular objects on a 2D plane. Each object is represented as [x, y, r], where (x, y) is the center and r is the radius. Detect all pairs that collide.
Two objects collide if the Euclidean distance between their centers is strictly less than the sum of their radii.
Return a list of index pairs [i, j] (with i < j) for all colliding pairs, sorted in ascending order by i, then by j. Use a linear scan that checks every pair of objects; at the sizes below that is fast enough.
Function Signature
def detect_collisions(objects: list[list[int]]) -> list[list[int]]:
Rules
-
Indices are the positions of the objects in the input list, starting at
0
.
-
Circles that touch (center distance exactly equal to the radius sum) do not collide.
-
A circle that lies inside another counts as colliding, because its center distance is less than the radius sum.
-
Each colliding pair appears once, as
[i, j]
with
i < j
. Return
[]
when no pair collides.
Constraints
-
1 <= len(objects) <= 1000
-
len(objects[i]) == 3
-
-10^4 <= x, y <= 10^4
-
1 <= r <= 10^4
Examples
Input: objects = [[0,0,5],[3,0,3],[10,10,2],[11,11,2]]
Output: [[0,1],[2,3]]
Objects 0 and 1 collide: their centers are 3 apart and the radius sum is 8. Objects 2 and 3 collide: their centers are about 1.41 apart and the radius sum is 4. Every pair across the two groups is more than 12 apart, with a radius sum of at most 7.
Input: objects = [[0,0,1],[5,0,1],[10,0,1]]
Output: []
The centers are at least 5 units apart, but every radius sum is 2, so no pair collides.
Input: objects = [[0,0,2],[4,0,2],[1,0,1]]
Output: [[0,2]]
Objects 0 and 1 are 4 apart with a radius sum of 4, so they only touch. Objects 1 and 2 are 3 apart with a radius sum of 3, which is also a touch. Objects 0 and 2 are 1 apart with a radius sum of 3, so they collide (object 2 sits inside object 0).