Answers and Comments
This is not an answer, but I disagree with CommonInterview that "the actual
test needed is to check if any of Rect1 corners are inside Rect2 and vice
versa." For example, suppose that two rectangles are arranged like a cross.
The rectangles overlap, but none of the corners lie withing the other
rectangle.
test needed is to check if any of Rect1 corners are inside Rect2 and vice
versa." For example, suppose that two rectangles are arranged like a cross.
The rectangles overlap, but none of the corners lie withing the other
rectangle.
Saved Stories
- What are the advantages of the demat account?
- Tell us about the last time you lost your temper? Did you take personal accountability for this situation?
- What is GAAP? Name at least three of the principles
- Can a Silverlight application create or change Office Documents via Open XML?
- How would you sort a linked list?
Sponsored Categories
The key to notice is that the actual test needed is to check if any of Rect1 corners are inside Rect2 and vice versa. In other words those two rectangle will not overlap if rectangle Rect1 is above Rect2 or below Rect2 or left from Rect2 or right from Rect2.
If any of those conditions are true rectangle don’t overlap… Just four checks !
bool IsOverlap(Rect R1, Rect R2)
{
return ((R1.LR.y <= R2.UL.y)
(R1.LR.x >= R2.UL.x) &&
(R1.UL.x <= R2.LR.x) &&
(R1.UL.y >= R2.LR.y) &&
)}