定义一个矩形(Rectangle)类,该类代表了一个矩形。可以定义不同的矩形,并对矩形 进行如下运算:

1、移动矩形
2、判断一个点是否在矩形内部
3、求两个矩形合并后的矩形,通过函数返回值返回代表合并后矩形的一个新建立的矩形
对象
4、求两个矩形交集,通过函数返回值返回代表两个矩形交集的一个新建立的矩形对象

同时创建一个包含 main 函数的测试类(TestRectangle)去测试矩形类,通过创建不同的 矩形对象,进行运算并在屏幕上打印出结果

class Untitled {
    public static void main(String[] args) {
        Rectangle rect1 = new Rectangle(10, 10, 20, 10);
        System.out.println(rect1);
        rect1.move(15, 30);
        System.out.println(rect1);
        Rectangle rect2 = new Rectangle(5, 10, 15, 10);
        System.out.println(rect2);
        Rectangle rect3 = rect1.combine(rect2);
        System.out.println(rect3);
        System.out.println(rect3.isInside(30, 10));
        System.out.println(rect3.isInside(100, 100));
        System.out.println(rect1.intersectsWith(rect2.move(20, 30)));
    }
}

class Rectangle
{
    public int top;
    public int left;
    public int width;
    public int height;
    public Rectangle(int t, int l, int w, int h)
    {
        this.top = t;
        this.left = l;
        this.width = w;
        this.height = h;
    }

    public Rectangle move(int x, int y)
    {
        top = x;
        left = y;
        return this;
    }

    public Boolean isInside(int x, int y)
    {
        return x > left && x < (left + width) && y > top && y < (top + height);
    }

    public Rectangle combine(Rectangle rect)
    {
        int x = left > rect.left ? rect.left : left;
        int y = top > rect.top ? rect.top : top;
        int x2 = left + width > rect.left + rect.width ? left + width : rect.left + rect.width;
        int y2 = top + height > rect.top + rect.height ? top + height : rect.top + rect.height;
        return new Rectangle(x, y, x2 - x, y2 - y);
    }

    public Rectangle intersectsWith(Rectangle rect)
    {
        int x = left > rect.left ? left : rect.left;
        int y = top > rect.top ? top : rect.top;
        int x2 = left + width > rect.left + rect.width ? rect.left + rect.width : left + width;
        int y2 = top + height > rect.top + rect.height ? rect.top + rect.height : top + height;
        if (x2 - x <= 0 || y2 - y <= 0) return null;
        return new Rectangle(x, y, x2 - x, y2 - y);
    }

    @Override
    public String toString()
    {
        return "top = " + top +
            " left = " + left +
            " width = " + width +
            " height = " + height + "\n";
    }   

}

top = 10 left = 10 width = 20 height = 10
top = 15 left = 30 width = 20 height = 10
top = 5 left = 10 width = 15 height = 10
top = 10 left = 5 width = 40 height = 20
false
false
top = 30 left = 20 width = 15 height = 5

1、移动矩阵直接改变矩阵四个点的坐标
2、点在矩阵内等价 :点的坐标(x,y)在矩阵x跨域,y跨域内
3、坐标合并
4、各跨域的交集就是新矩阵的各个坐标位置
算是数学问题吧,理解的话很容易