2D Circle and rectangle intersection tests

In summary: Hello :)I'm sorry, but I don't think that is how this question should be asked. This is a question about finding the level of intersection between a rectangle and circle, not about programming. You should post in the programming section. Goodbye.
  • #1
TannerB
3
0
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
 
Physics news on Phys.org
  • #2
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
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:
	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(disBotLeft <= radius*radius)  inBl = true;
		if(disTopRight <= radius*radius) inTr = true;
		if(disTopLeft <= radius*radius)  inTl = true;
		if(disBotRight <= radius*radius) inBr = true;
		
		
		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.
 

Attachments

  • box circ.png
    box circ.png
    26.1 KB · Views: 885
  • #4
With respect, your problem will be easier if properly stated.

What I'm trying to do is figure out the level of intersection between a rectangle and circle:

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
Studiot said:
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.

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.
 

Related to 2D Circle and rectangle intersection tests

1. What is a 2D circle and rectangle intersection test?

A 2D circle and rectangle intersection test is a mathematical calculation used to determine if a circle and a rectangle overlap or intersect with each other in a two-dimensional space. This test is commonly used in computer graphics and game development to detect collisions between objects.

2. How does a 2D circle and rectangle intersection test work?

The test works by comparing the position and size of the circle and rectangle. It uses the properties of circles and rectangles, such as their center points, radius, and edges, to determine if they are overlapping or intersecting. If any part of the circle falls within the boundaries of the rectangle, then they are considered to be intersecting.

3. Why is a 2D circle and rectangle intersection test important?

This test is important because it allows for efficient collision detection in two-dimensional spaces. It is used in various applications, such as video games and simulations, to ensure that objects within the game or simulation interact realistically with each other.

4. What are some common algorithms used for 2D circle and rectangle intersection tests?

Some common algorithms used for 2D circle and rectangle intersection tests include the Separating Axis Theorem, the Minkowski Sum method, and the SAT method. These algorithms use different approaches to determine if an intersection occurs and have varying levels of efficiency and accuracy.

5. Are there any limitations to 2D circle and rectangle intersection tests?

Yes, there are some limitations to this type of intersection test. For example, it may not accurately detect intersections between circles and rectangles that are rotated at different angles. Additionally, it may not work for complex shapes or shapes with curved edges. In these cases, more advanced algorithms may be needed to accurately detect intersections.

Similar threads

  • Differential Geometry
Replies
7
Views
4K
Replies
4
Views
922
  • General Math
Replies
1
Views
2K
Replies
6
Views
3K
Replies
4
Views
4K
Replies
23
Views
2K
  • Differential Geometry
Replies
12
Views
6K
  • Precalculus Mathematics Homework Help
Replies
5
Views
1K
  • Calculus and Beyond Homework Help
Replies
8
Views
3K
  • DIY Projects
Replies
8
Views
430
Back
Top