0

I am trying to test if a circle intersects a square. The best way I have figured out how to do that is to find if there is an x such that: x is between the minimum and maximum x values of the square;

abs(x-centerOfCircle) <= radiusOfCircle

The problem is I can't figure out how to translate this into code, can anyone help?

Barmar
  • 741,623
  • 53
  • 500
  • 612
LarrySellers
  • 60
  • 3
  • 12
  • @0x499602D2 His problem is how to get `x`, since he needs to find out if there's any `x` that fits the equation. – Barmar Nov 01 '14 at 00:38
  • 1
    You don't do it by testing whether there's any such `x`. You do it with an algorithm that works with the coordinates of the square's corners. I don't know the algorithm offhand, but I'm sure you can find it by googling. – Barmar Nov 01 '14 at 00:41

1 Answers1

3

I would convert your problem to detecting if circle's center is inside rounded square defined as outline of your original square by radius of your circle.

Then intersection test is quite trivial do following simple sub-tests:

  • is circle center inside any of 4 circles (at the corners of sqare) ?
  • is circle center inside green rectangle
  • is circle center inside blue rectangle

If any of these 3 tests are TRUE, your circle intersect your square.

enter image description here

Anonymous
  • 2,122
  • 19
  • 26