Rectangle overlap

问两个长方形是否overlap

假设ABCD四个点, A,C分别是两个四边形的左上,B,D分别是两个三角形的右下的点

直接一行代码搞定  return !(Bx <= Cx || Ay <= Dy || Ax >= Dx || By >= Cy); 

leetcode 223

Code

package amazon;

//pay attention it is defined as >= or >

public class overlapRectangle {
    public static boolean check(Node topLeftA, Node topLeftB, Node bottomRightA, Node bottomRightB){
        //check if input is valid?
        if (topLeftA.x > bottomRightA.x || topLeftA.y < bottomRightA.y || topLeftB.x > bottomRightB.x || topLeftB.y < bottomRightB.y){
            System.out.println("Input not valid");
        }

        //if one rectangle is on the left/right of the other
        if (topLeftB.x >= bottomRightA.x || topLeftA.x >= bottomRightB.x){
            return false;
        }

        //if one rectangle is above/below the other
        if (bottomRightB.y >= topLeftA.y || bottomRightA.y >= topLeftB.y){
            return false;
        }

        //non overlapping condition
        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){
        //overlapping condition
//        Node A = new Node(-3,4);
//        Node B = new Node(3,0);
//        Node C = new Node(0,2);
//        Node D = new Node(9, -1);

        //non-overlapping
//        Node A = new Node(-6,-3);
//        Node B = new Node(-1,-3);
//        Node C = new Node(1,5);
//        Node D = new Node(3, 2);

        //one in the other
        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));
    }
}