Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for colliding circles

I have some circles and I know their X, Y, and r. I want to check if ANY of them collide with ANY other...
The way to check is easy:

r1+r2 < sqrt((x1-x2) 2 +(y1-y2)2)

but do I have to check all with all? It gives me O(n2) complexity, and I want to avoid that.

like image 435
noisy cat Avatar asked Sep 17 '25 05:09

noisy cat


2 Answers

Try looking at KD-tree acc-struct. first you have to consider circles as squares way less complexity for computing intersection , than you put these squares in the modified KD-tree ,it will need some thinking but hopefully nothing too extreme ... Way kd-tree works is that it cancels out half of the possible matches based on some criteria for each tree level. Look it up on wiki. Good luck with your problem :)

like image 108
Peter Kottas Avatar answered Sep 19 '25 06:09

Peter Kottas


You can divide your space into regions, like:

  1. Compute 2D AABB - axis aligned bounding box for all the circles
  2. Divide it into four sub boxes
  3. To each of sub boxes assign circle, if circle even slightly crosses such box then it must be put into such box. This means that circle can be assigned to multiple boxes.
  4. Iterate each circle, then check to which box it was assigned, and compute collision only with circles from that box.

In 2. you can do many subdivisions, depending on your space size, also if to many circles are assigned to one box then subdivide it further.

like image 40
marcinj Avatar answered Sep 19 '25 05:09

marcinj