I come across this math problem which is needed to complete my application, so I'm asking for help.
Given 2 (or more, but basically for 2) rectangles with 2 known points for each: Top-left(x1, y1) and Bottom-right(x2, y2) (I can find the length with these info, if it is a need to solve the problem).
TL(x1, y1)
+-----------------+
| |
| | TL(x3, y3)
| | +---------------------------+
+-----------------+ | |
BR(x2, y2) +---------------------------+
BR(x4, y4)
Is there anyway to determine if they have intersection, in area, I mean, if any part of this rectangle lays on any part of another one?
I've searched and found out a little help, but it does not solve the problem:
There are 4 conditions in which the two rectangles will not intersect:
The left edge of one rectangle is on the right side of the right edge of the other one, means that the first one is completely laid on the right side of the second one, no intersection.
The right edge of one rectangle is on the left side of the left edge of the other one, means that the first one is completely laid on the left side of the second one, no intersection.
The top edge of one rectangle is under the bottom edge of the other one, means that the first one is completely laid under the second one, no intersection.
The bottom edge of one rectangle is above the top edge of the other one, means that the first one is completely laid above the second one, no intersection.
So I tried to reverse the conditions, which is if 4 of the above does not occur, the rectangles may intersect. But I still can find the condition (such as the picture above) in which 2 rectangles do not fulfill any condition, and still does not intersect.
Any help is highly appreciated, please show me the way to do it or algorithm or code (JS and PHP only please).
Big thank!
[x]