Does a circle overlap a square?

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 7K views
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