Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect intersections between a circle and any other circle in the same plane?

I'm looking for an algorithm to detect if a circle intersects with any other circle in the same plane (given that there can be more than one circle in a plane).

One method I have found is to do the separating axis test. It says:

Two objects don't intersect if you can find a line that separates the two objects, i.e. a line such that all objects or points of an object are on different sides of the line.

However, I don't know how to apply this method to my case.

Can anybody help me?

like image 457
Jean-Luc Godard Avatar asked Dec 03 '11 12:12

Jean-Luc Godard


People also ask

How do you check if a circle intersects another circle?

To do this, you need to work out the radius and the centre of each circle. If the sum of the radii and the distance between the centres are equal, then the circles touch externally. If the difference between the radii and the distance between the centres are equal, then the circles touch internally.

How many intersections can 2 circles have?

Two circles may intersect in two imaginary points, a single degenerate point, or two distinct points. The intersections of two circles determine a line known as the radical line.

Can you draw two circles intersecting each other at just one point?

The common tangent which is formed by touching the top ends of both the circles. Therefore, The common tangents which can be drawn if two circles are touching externally at a single point = 3.


2 Answers

Two circles intersect if, and only if, the distance between their centers is between the sum and the difference of their radii. Given two circles (x0, y0, R0) and (x1, y1, R1), the formula is as follows:

ABS(R0 - R1) <= SQRT((x0 - x1)^2 + (y0 - y1)^2) <= (R0 + R1) 

Squaring both sides lets you avoid the slow SQRT, and stay with ints if your inputs are integers:

(R0 - R1)^2 <= (x0 - x1)^2 + (y0 - y1)^2 <= (R0 + R1)^2 

Since you need only a yes/no test, this check is faster than calculating the exact intersection points.

The above solution should work even for the "one circle inside the other" case.

like image 89
Sergey Kalinichenko Avatar answered Oct 06 '22 12:10

Sergey Kalinichenko


Assuming filled circle intersection (ie: One circle inside another is an intersection).

Where:

  • x0,y0,r0 = Center and radius of circle 0.
  • x1,y1,r1 = Center and radius of circle 1.

Code:

boolean intersects = Math.hypot(x0-x1, y0-y1) <= (r0 + r1); 
like image 23
Craigo Avatar answered Oct 06 '22 12:10

Craigo