Given two axis-aligned rectangles A and B. Write a function to determine if the two rectangles overlap.
Read full article from Determine If Two Rectangles Overlap | LeetCode
A much easier and better approach would be to think from the opposite. How about asking yourself how the two rectangles cannot overlap each other? Two rectangles do not overlap when one is above/below, or to the left/right of the other rectangle.
The condition’s expression is:
! ( P2.y < P3.y || P1.y > P4.y || P2.x < P3.x || P1.x > P4.x )
Using De Morgan’s law, we can further simplify the above expression to:
Given a list of rectangles, how would you determine the set of overlapping rectangles efficiently? Why would this be useful?
( P2.y ≤ P3.y && P1.y ≥ P4.y && P2.x ≥ P3.x && P1.x ≤ P4.x )
Read full article from Determine If Two Rectangles Overlap | LeetCode
No comments:
Post a Comment