Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 843161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:04:50+00:00 2026-05-15T06:04:50+00:00

Lets say I’ve got two squares and I know their positions, a red and

  • 0

Lets say I’ve got two squares and I know their positions, a red and blue square:

redTopX;
redTopY;
redBotX;
redBotY;
blueTopX;
blueTopY;
blueBotX;
blueBotY;

Now, I want to check if square blue resides (partly) within (or around) square red. This can happen in a lot of situations, as you can see in this image I created to illustrate my situation better:
alt text http://www.feedpostal.com/etc/ranges.gif

Note that there’s always only one blue and one red square, I just added multiple so I didn’t have to redraw 18 times.

My original logic was simple, I’d check all corners of square blue and see if any of them are inside square red:

if (
((redTopX >= blueTopX) && (redTopY >= blueTopY) && (redTopX <= blueBotX) && (redTopY <= blueBotY)) || //top left
((redBotX >= blueTopX) && (redTopY >= blueTopY) && (redBotX <= blueBotX) && (redTopY <= blueBotY)) || //top right
((redTopX >= blueTopX) && (redBotY >= blueTopY) && (redTopX <= blueBotX) && (redBotY <= blueBotY)) || //bottom left
((redBotX >= blueTopX) && (redBotY >= blueTopY) && (redBotX <= blueBotX) && (redBotY <= blueBotY)) //bottom right
) {
    //blue resides in red
}

Unfortunately, there are a couple of flaws in this logic. For example, what if red surrounds blue (like in situation 1)?

I thought this would be pretty easy but am having trouble coming up with a good way of covering all these situations.. can anyone help me out here?

Regards,
Tom

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:04:51+00:00Added an answer on May 15, 2026 at 6:04 am

    A test that checks whether red rectangle resides completely outside the blue rectangle looks as follows

    bool outside = 
      redBotX > blueTopX || redTopX < blueBotX || 
      redBotY > blueTopY || redTopY < blueBotY;
    

    Now, the negative of that will tell you whether red rectangle intersects the blue rectangle

    bool intersects =
      !(redBotX > blueTopX || redTopX < blueBotX || 
        redBotY > blueTopY || redTopY < blueBotY);
    

    If you wish, you can apply the De Morgan rule and rewrite it as

    bool intersects =
      redBotX <= blueTopX && redTopX >= blueBotX && 
      redBotY <= blueTopY && redTopY >= blueBotY;
    

    Of course, the above tests assume that the coordinates are “normalized*, i.e.

    assert(redBotX <= redTopX && redBotY <= redTopY);
    assert(blueBotX <= blueTopX && blueBotY <= blueTopY);
    

    Also, the comparisons might be strict or non-strict depending on whether you consider touching rectangles as intersecting or not.

    EDIT: I see that you use a different convention for rectangle coordinates: Top is the lower coordinate and Bot is the higher one, i.e.

    assert(redTopX <= redBotX && redTopY <= redBotY);
    assert(blueTopX <= blueBotX && blueTopY <= blueBotY);
    

    To handle this case you just need to swap the Bot and Top coordinates in all conditions. For example, the last one will now look as follows

    bool intersects =
      redTopX <= blueBotX && redBotX >= blueTopX && 
      redTopY <= blueBotY && redBotY >= blueTopY;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I've got two interchangeable pieces of code and I want to figure
Lets say you have a list of items: <ul id=ImportantNumbers> <li id=one></li> <li id=two></li>
Lets say I have a custom control called FooBox. It is just a square
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
Lets say that you have websites www.xyz.com and www.abc.com. Lets say that a user
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Lets say I have several web sites on my web server, all as applications
Lets say on MySQL database (if it matters).
Lets say I have an array like this: string [] Filelist = ... I
Lets say I have a single object of type Car which I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.