0

Really simple question for someone who doesn't know geometry or formulae. I need to know if two circles intersect. That's it.

I have two circles: circle1 and circle2. These two circles are the same size with the same radius. They'll never be different. I need a formula to show me if these circles overlap. I don't care where. I don't care how much. I don't care about any details but true or false. Do the circles overlap or not? I just need something I can est for in Javascript.

The formula here doesn't work for me. How do I detect intersections between a circle and any other circle in the same plane? Nor does anything on Google.

I've been racking my brain and Google searching for three hours, and no formula that I find works.

Can anybody please help??

Steve G.
  • 411
  • 1
  • 7
  • 21
  • What info do you have? Seems that they overlap if the centers are within one radius of each other in distance. The line connecting the centers are the hypotenuse of a triangle. – Nick Aug 19 '21 at 02:54
  • I don't know how to calculate if the centers are within one radius of each other. I know how to calculate if the X points are within one radius or the Y points, but I don't know a formula that calculates if both are simultaneously within one radius...if that makes sense. When you say hypotenuse of a triangle, do you mean: distance = square root ( (X point of circle1 - X point of circle2)^2 + (Y point of circle1 - Y point of circle2)^2 ) – Steve G. Aug 19 '21 at 02:56
  • 1
    yes exactly! I was incorrect before though, that distance just needs to be twice the radius or less (you can draw on paper to see why) – Nick Aug 19 '21 at 03:00
  • 1
    so it seems the condition you're looking for is `square root ( (X point of circle1 - X point of circle2)^2 + (Y point of circle1 - Y point of circle2)^2 ) <= 2 * radius` – Nick Aug 19 '21 at 03:02
  • not radius *2 but radius of circle one+ radius of circle two –  Aug 19 '21 at 03:05
  • plus it is best to use absolute values everywhere otherwise you have to keep track of which one is higher Math.abs(X point of circle1 - X point of circle2)^2 + Math.abs(Y point of circle1 - Y point of circle2)^2 ) –  Aug 19 '21 at 03:07
  • Thanks @Nick! That breakdown worked. I think part of my problem was that, in Javascript, I guess ^ isn't exponential? I got weird results. I did ** instead, and that seemed to be to the power of 2. I guess the carat isn't the exponential operator in JS? – Steve G. Aug 19 '21 at 04:06
  • Thanks, @SolomonPByer. I added the absolute values. The radii are always the same, so radius 1 + radius 2 would be the same as radius1*2. But in a case where the two circles are different, adding them would work instead. – Steve G. Aug 19 '21 at 13:46

2 Answers2

1

this is a function that will return if they are overlapping

function overLapping(CircleOneX,CircleOneY,CircleOneRadius,CircleTwoX,CircleTwoY,CircleTwoRadius){
 return Math.sqrt(Math.abs(CircleOneX-CircleTwoX)+ Math.abs(CircleOneY-CircleTwoY))<(CircleOneRadius+CircleTwoRadius)
}
  • That formula is wrong. Math.sqrt((CircleOneX-CircleTwoX) ** 2 + (CircleOneY-CircleTwoY) ** 2 ) < CircleOneRadius+CircleTwoRadius – T H Jul 01 '22 at 18:14
1

Use the builtin Math.hypot function to calculate the distance between the centers of the two circles. If this is less than or equal the sum of the radii then the circles overlap.

function circleIntersect(x0, y0, r0, x1, y1, r1) {
    return Math.hypot(x0 - x1, y0 - y1) <= r0 + r1;
}

console.log(circleIntersect(0, 0, 2, 3, 0, 2));

console.log(circleIntersect(0, 0, 2, 5, 0, 2));
Cardistymo
  • 28
  • 5
RaffleBuffle
  • 5,396
  • 1
  • 9
  • 16
  • Thanks, RaffleBuffle! I didn't even know Math.hypot existed. I'm going to try to incorporate that into my code when I get a moment after work, since it's surely faster and more robust than what I've currently got. Stay tuned! – Steve G. Aug 19 '21 at 13:48