Given two rectangles and their top-left and bottom-right points. Check if the two rectangles overlap.
/*
time: O(1), space:O(1)
*/
public class OverLap {
public class Node {
double x;
double y;
public Node(double x, double y) {
this.x = x;
this.y = y;
}
}
public boolean check(Node topLeftA, Node topLeftB,
Node bottomRightA, Node bottomRightB) {
if (bottomRightA.y >= topLeftB.y ||
bottomRightB.y >= topLeftA.y) {
return false;
}
if (topLeftA.x >= bottomRightB.x ||
topLeftB.x >= bottomRightA.x) {
return false;
}
return true;
}
}
No comments:
Post a Comment