Does a circle overlap a square?

AI Thread Summary
To determine if a circle overlaps a square, calculate the shortest distance from the circle's center to the square's boundary and compare it with the circle's radius. If the distance exceeds the sum of the radius and half the square's diagonal, there is no overlap. If the distance is less than the square's side length, an overlap is confirmed. In cases where the distance falls between these two conditions, further analysis is needed, potentially involving checking specific points on the circle. This problem is commonly addressed in game programming, and researching "collision detection" may provide additional insights.
TDC-Studio
Messages
1
Reaction score
0
Hello,

We are trying to calculate if a circle, of a given x, y co-ordinate and radius, will overlap a square, of a given x, y co-ordinate and length.

We think we can identify points around the circumference and see if these points, with an x and y value, will be within the square. However this requires numerous calculations and we want to achieve a definitive yes or no in one calculation.

Thank you for giving this some thought.
 
Physics news on Phys.org
Welcome to Physics Forums,

HINT: Try calculating the shortest distance from the centre of the circle to the boundary of the square. How does this compare with the radius of the circle?

As an aside, all homework or textbook style questions should be posted in the Homework forum. I'll move this thread there now.
 
let c be the center of the circle
let s be the center of the square.
let r be the radius of the circle
let x be the length of a side of the square.
let d be half the diagonal of the square.

if |c-s| > r + d you can rule out a collision.
if |c-s| < x you certainly have a collision.
if x < |c-s| < r + d you *might* have a collision.

The last one is a bit tricky. If you point a vector from c towards s, you can use that to get the quarter of the circle which points towards the square, and check each of those points individually. It sucks, I know, but at least you only check 1/4 of the total points.

Since both the circle and the square are very simple shapes, there might be some trig that can figure this out without checking individual points on the circle. If it is, it eludes me at the moment.

You might want to google "collision detection" and read up. This is a very common type of problem in game programming.

k
 
Back
Top