Question: You are given and axis-aligned rectangles. You had to determine if these rectangles overlap each other or not.
Rectangle 1:p1 (x, y), P2 (y)
Rectangle 2:p3 (x, y), P4 (y)
This question gives the coordinates of two rectangles and asks if the two rectangles have coincident parts. This is how you draw a diagram:
The problem seems a bit complicated, and if you find a complex set of conditional expressions, you'll have a lot of effort. There is an easy way to try to look at the problem from the opposite side.
Assume that two rectangles are somewhat (P1, P2) and dots (P3, P4). When the two cuboid has overlapping parts, there must be one corner of the box inside the other box. Look at the following figure:
Now it seems complicated to look at the problem again, should it be represented by 8 conditional expressions? Can you simplify the problem?
On the contrary, there are 8 balls of different colors in the bag, what is the probability of not taking the black ball? We calculate the probability of taking out the black ball first, and then we can reduce it by 1.
Similarly, how can two rectangles not overlap? The answer is simple: When a rectangle is around the other rectangle.
The corresponding conditional expression:
! (P2.y < P3.Y | | P1.y > P4.y | | p2.x < p3.x | | p1.x > p4.x)
Simplify it with de Morgan's law:
(p2.y <= p3.y && p1.y >= p4.y && p2.x >= p3.x && p1.x <= p4.x)
Is it more neat?
Check if two rectangles have overlapping parts