Rectangle Overlap
Two rectangles overlap if one’s maximum x-coordinate is greater than the other’s minimum x-coordinate and the maximum y-coordinate is greater than the other’s minimum y-coordinate. It is also necessary to ensure the minimum x and y of one rectangle is less than the maximum x and y of the other.
Python solution:
|
|
In this function, max(rec1[0], rec2[0]) < min(rec1[2], rec2[2])
checks the x-coordinates, and max(rec1[1], rec2[1]) < min(rec1[3], rec2[3])
checks the y-coordinates.
This solution is efficient with a time complexity of O(1), as it performs a constant number of operations, and a space complexity of O(1), as it uses a constant amount of space.