#### Archived

This topic is now archived and is closed to further replies.

# Rectangle within Rectangle

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

## Recommended Posts

I have the following code to tell me if all the sides of one rectangle intersect with all the sides of another rectangle. But is there a way to know if one rectangle lies completely within another without touching any of its sides.
bool rectangleWithinRectangle_2dIntersection(Point2d pointRectangle_1_cornerMin, Point2d pointRectangle_1_cornerMax,
Point2d pointRectangle_2_cornerMin, Point2d pointRectangle_2_cornerMax)
{
// RETURN true IF ONE RECTANGLE LIES WITHIN ANOTHER RECTANGLE

bool returnValue = false;

// IF NOT POSITIVE THEN THERE IS AN INTERSECTION

if ((pointRectangle_2_cornerMin.x - pointRectangle_1_cornerMax.x) <= 0.0)
{
if ((pointRectangle_2_cornerMin.y - pointRectangle_1_cornerMax.y) <= 0.0)
{
if ((pointRectangle_2_cornerMax.x - pointRectangle_1_cornerMin.x) <= 0.0)
{
if ((pointRectangle_2_cornerMax.y - pointRectangle_1_cornerMin.y) <= 0.0)
{
return true;
}
}
}
}

return returnValue;
}


##### Share on other sites
Off the top of my head:

// returns true if rectangle 1 is inside rectangle 2bool RectangleInsideRectangle(Point2d pointRectangle_1_cornerMin,                              Point2d pointRectangle_1_cornerMax,                                                                            Point2d pointRectangle_2_cornerMin,                               Point2d pointRectangle_2_cornerMax){    return ( ( pointRectangle_1_cornerMax.x < pointRectangle_2_cornerMax.x )        && ( pointRectangle_1_cornerMax.y < pointRectangle_2_cornerMax.y )        && ( pointRectangle_1_cornerMin.x > pointRectangle_2_cornerMin.x )        && ( pointRectangle_1_cornerMin.y > pointRectangle_2_cornerMin.y ) );}

##### Share on other sites
No way. Is that really all you need to do? I was thinking like convex hulls and stuff like that.

##### Share on other sites
Please just one more. I don''t want any code, but could someone tell me how to tell if a circle is inside a rectangle, without touching any of the sides of the rectangle?

##### Share on other sites
Put a rectangle around the centre of the circle with sides the same length of the diameter of the circle and do normal rect to rect checks.

The above method (and all the methods so far) have assumed the rectangle is axis-aligned though, I notice.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

##### Share on other sites
What if the rectangle is not axis aligned, but rotated about its center point for some angle, and there happens to be a circle inside this rectangle. Is there a way to make sure the circle is not touching any of the sides of the rectangle?

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 9
• 15
• ### Forum Statistics

• Total Topics
633392
• Total Posts
3011639
• ### Who's Online (See full list)

There are no registered users currently online

×