Asked • 03/18/19

Java method to find the rectangle that is the intersection of two rectangles using only left bottom point, width and height?

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 Expert Answer

By:

Jessica Y. answered • 09/30/20

Tutor
5.0 (392)

Back-End Java Developer, AP CSA Teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.