I am facing this math problem that is needed to complete my application, so I ask for help.
For two (or more, but mostly 2) rectangles with two known points for each: Top left (x1, y1) and Bottom right (x2, y2) (I can find the length with this information if necessary to solve the problem )
TL(x1, y1) +-----------------+ | | | | TL(x3, y3) | | +---------------------------+ +-----------------+ | | BR(x2, y2) +---------------------------+ BR(x4, y4)
In any case, to determine if they have an intersection, in a region, I mean, if any part of this rectangle lies on any part of another?
I searched and helped a little, but this does not solve the problem:
There are 4 conditions in which two rectangles will not intersect:
The left edge of one rectangle is to the right of the right edge of the other, means that the first is completely laid on the right side of the second, without intersection.
The right edge of one rectangle is to the left of the left edge of the other, which means that the first is completely laid on the left side of the second, without intersection.
The upper edge of one rectangle is under the lower edge of the other, meaning that the first is completely laid out under the second, without intersection.
The lower edge of one rectangle is above the upper edge of the other, which means that the first lies completely over the second, without intersection.
So, I tried to change the conditions, which means that 4 of the above does not happen, the rectangles can intersect. But I can still find a condition (for example, the image above) in which 2 rectangles do not fulfill any conditions and still do not intersect.
Any help is much appreciated, please show me a way to do this, either an algorithm or code (JS and PHP only).
Thank you very much!
[x]
source share