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 9217303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:36:22+00:00 2026-06-18T02:36:22+00:00

I have found the solution but wanted to ensure my logic is the most

  • 0

I have found the solution but wanted to ensure my logic is the most efficient. I feel that there is a better way. I have the (x,y) coordinate of the bottom left corner, height and width of 2 rectangles, and i need to return a third rectangle that is their intersection. I do not want to post the code as i feel it is cheating.

  1. I figure out which is furthest left and highest on the graph.
  2. I check if one completely overlaps the other, and reverse to see if the other completely overlaps the first on the X axis.
  3. I check for partial intersection on the X axis.
  4. I basically repeat steps 2 and 3 for the Y axis.
  5. I do some math and get the points of the rectangle based on those conditions.

I may be over thinking this and writing inefficient code. I already turned in a working program but would like to find the best way for my own knowledge. If someone could either agree or point me in the right direction, that would be great!

  • 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-06-18T02:36:23+00:00Added an answer on June 18, 2026 at 2:36 am

    Why not use JDK API to do this for you?

    Rectangle rect1 = new Rectangle(100, 100, 200, 240);
    Rectangle rect2 = new Rectangle(120, 80, 80, 120);
    Rectangle intersection = rect1.intersection(rect2);
    

    To use java.awt.Rectangle class, the parameters of the constructor are: x, y, width, height, in which x, y are the top-left corner of the rectangle. You can easily convert the bottom-left point to top-left.


    I recommend the above, but if you really want to do it yourself, you can follow the steps below:

    say (x1, y1), (x2, y2) are bottom-left and bottom-right corners of Rect1 respectively,
    (x3, y3), (x4, y4) are those of Rect2.

    • find the larger one of x1, x3 and the smaller one of x2, x4, say xL,
      xR respectively

      • if xL >= xR, then return no intersection else
    • find the larger one of y1, y3 and the smaller one of y2, y4, say yT,
      yB respectively

      • if yT >= yB, then return no intersection else
      • return (xL, yB, xR-xL, yB-yT).

    A more Java-like pseudo code:

    // Two rectangles, assume the class name is `Rect`
    Rect r1 = new Rect(x1, y2, w1, h1);
    Rect r2 = new Rect(x3, y4, w2, h2);
    
    // get the coordinates of other points needed later:
    int x2 = x1 + w1;
    int x4 = x3 + w2;
    int y1 = y2 - h1;
    int y3 = y4 - h2;
    
    // find intersection:
    int xL = Math.max(x1, x3);
    int xR = Math.min(x2, x4);
    if (xR <= xL)
        return null;
    else {
        int yT = Math.max(y1, y3);
        int yB = Math.min(y2, y4);
        if (yB <= yT)
            return null;
        else
            return new Rect(xL, yB, xR-xL, yB-yT);
    }
    

    As you see, if your rectangle was originally defined by two diagonal corners, it will be easier, you only need to do the // find intersection part.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I apologize for the newb question but have not found a solution online. I
I have looked thoroughly, but I have not found the solution (or perhaps I
I have found some similar Que's on SO but had not find the solution.
It looks like a problem which could have simple solution, but I haven't found
Hi i have been looking for some solutions but i have found nothing... Is
I have found similar problems on the web, but none of the posted solutions
I have Googled a lot and found a lot of solutions, but none of
I searched for an implementation of std::map runtime ordering and have found this solution:
I have searched the web and have found no definitive solution, so here goes:
I have searched for hours now and haven't found a solution for my problem.

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.