Rectangle overlap
问两个长方形是否overlap
假设ABCD四个点, A,C分别是两个四边形的左上,B,D分别是两个三角形的右下的点
直接一行代码搞定 return !(Bx <= Cx || Ay <= Dy || Ax >= Dx || By >= Cy);
leetcode 223
Code
package amazon;
public class overlapRectangle {
public static boolean check(Node topLeftA, Node topLeftB, Node bottomRightA, Node bottomRightB){
if (topLeftA.x > bottomRightA.x || topLeftA.y < bottomRightA.y || topLeftB.x > bottomRightB.x || topLeftB.y < bottomRightB.y){
System.out.println("Input not valid");
}
if (topLeftB.x >= bottomRightA.x || topLeftA.x >= bottomRightB.x){
return false;
}
if (bottomRightB.y >= topLeftA.y || bottomRightA.y >= topLeftB.y){
return false;
}
return true;
}
public static class Node{
double x;
double y;
public Node(double x, double y){
this.x = x;
this.y = y;
}
}
public static void main(String[] args){
Node A = new Node(-3,4);
Node B = new Node(0,0);
Node C = new Node(-5,5);
Node D = new Node(3, -2);
System.out.println(check(A,C,B,D));
}
}