# 2D Circle and rectangle intersection tests

1. Sep 25, 2011

### TannerB

What I'm looking for is an algorithm to find the details on the intersection of of a circle and rectangle in two dimensional Euclidean space.

The information I need to find is straightforward enough; all I need is to know whether the rectangle and circle are not intersecting, partially intersecting, or fully intersecting.

So far what I have for finding if the rectangle is fully contained in the square is this:

1. Do a bounds check on the circle center
2. Is the center within the rectangle on the X axis and Y axis?
3. If so, is the distance from the bottom left corner of the rectangle to the center, along with the top right corner to the center greater then the radius of the circle?
4. So, if these three conditions are true, then the rectangle is within the circle

If the first condition, the bounds check is true, but the others are not can I safely assume that there is a partial intersection going on? I can see this will catch the case of the circle being fully in the square as well. I also know if one corner is in, but the other is not then it is a partial intersection.

Are there any other cases I should be worrying about?

As for the case where non

2. Sep 25, 2011

### Studiot

Good evening, Tanner and welcome to Physics Forums.

It would be a good idea to edit you post to better explain/describe exactly what you mean.

In particular what do you mean by the intersection of a rectangle and a circle.
Also where does the square enter the game?

3. Sep 25, 2011

### TannerB

Hello :)

Sorry rereading my post I realize it was more of me talking to myself then a proper question. What I'm trying to do is figure out the level of intersection between a rectangle and circle: fully intersected, partially intersected or no intersection at all. I've attached a picture showing what some cases will look like. It's for a programming project of mine that I am working on.

Here is some code that I've come up with since my first post. What it does is it checks the distance between each of the corners of the rectangle and the center of the circle. If the distance of each corner is less then the radius, I know it is fully intersected, if atleast one corner is less then the radius, it is partially intersected, otherwise there is no intersection.

I guess my question now could be changed to 'Is there a more efficient way of doing this?'

Code (Text):

public static int findCrnDist(float circleX, float circleY, float radius, float rectX, float rectY, float width, float height)
{
boolean inBl = false, inBr = false, inTl = false, inTr = false;

double disBotLeft  = Math.pow(circleX - rectX, 2) + Math.pow(circleY - rectY, 2);
double disTopLeft = Math.pow(circleX - rectX, 2) + Math.pow(circleY - (rectY + height), 2);
double disTopRight = Math.pow(circleX - (rectX + width), 2) + Math.pow(circleY - (rectY + height), 2);
double disBotRight = Math.pow(circleX - (rectX + width), 2) + Math.pow(circleY - rectY, 2);

if(inTr && inBl && inTl && inBr) return 2; //full
else if(inTr || inBl || inTl || inBr) return 1; //partial
else return 0; //none
}
Edit: The reason I'm not finding the root for the distance is because it is too expensive of an operation, checking against radius*radius is much more efficient.

#### Attached Files:

• ###### box circ.png
File size:
10.8 KB
Views:
403
4. Sep 25, 2011

### Studiot

With respect, your problem will be easier if properly stated.

I understand this bit, but what about fully specifying the conditions?

Is the rectangle always smaller than the circle?

Are the sides of the rectangle always parallel to the axes?

and so on.

This is really a programming problem and belongs in the computer section.

5. Sep 25, 2011

### TannerB

Sorry about the confusing way I put it! For the conditions, no, the rectangle will not always be smaller then the circle. The rectangle will always be 'axis-aligned', never rotated.

If a mod could move it to the programming section that would be good.