Jump to content
  • Advertisement

Archived

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

kelaklub

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Off the top of my head:


// returns true if rectangle 1 is inside rectangle 2
bool 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!