# 2D AABB Contains

This topic is 2344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Whats the fastest way to test containment between two 2D AABBs?

Also I'm testing for intersections like this:

 return (Abs(Corners[0].X - B.GetCorners[0].X) * 2 < (Corners[1].X + B.GetCorners[1].X)) && (Math.Abs(Corners[0].Y - B.GetCorners[0].Y) * 2 < (Corners[1].Y + B.GetCorners[1].Y)); 

if I always need to check for intersections as well as containment is there a better method I could use to check both at the same time more cheaply?

##### Share on other sites
You could return the area of intersection rather than a simple boolean result; this would allow you to easily check if one AABB contained the other (you would check to see if the area of intersection was equal to the area of the smaller AABB). To get the area of intersection of two AABBs you can use the following algorithm:

1. Subtract the distance between the leftmost part and the rightmost part of the two AABBs from the sum of the widths of the two AABBs. If this number is negative, set it to zero.
2. Repeat step 1, but for the y-axis instead of the x-axis.
3. Multiply the results from step 1 and step 2 to get the area of intersection.

##### Share on other sites
[source lang="cpp"]return A.x1< = B.x2 && A.x2 > =B.x1 && A.y1< =B.y2 && A.y2> =B.y1;[/source] Edited by Ashaman73

##### Share on other sites
Thanks for the replies.

The area of intersection sum is really useful.

I found some good stuff in a book and I think the fastest method when using min/max is separating axis:

 if (Max.X < B.Min.X || Min.X > B.Max.X) return false; if (Max.Y < B.Min.X || Min.Y > B.Max.Y) return false; return true;  Edited by lukesmith123

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633768
• Total Posts
3013753
×