public class Rectangle { private Point p1; private Point p2; public Rectangle() { p1 = new Point(0, 0); p2 = new Point(50, 50); } public Rectangle(int x1, int y1, int x2, int y2) { p1 = new Point(x1, y1); p2 = new Point(x2, y2); } public Rectangle(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; } public int getArea() { int width = Math.abs(p1.getX() - p2.getX()); int height = Math.abs(p1.getY() - p2.getY()); return width * height; } public boolean equals(Rectangle rect) { return this.p1.getX() == rect.p1.getX() && this.p1.getY() == rect.p1.getY() && this.p2.getX() == rect.p2.getX() && this.p2.getY() == rect.p2.getY(); } public static boolean checkOverlapped(Rectangle r1, Rectangle r2) { return checkPointInsideRectangle(r1.p1, r2) || checkPointInsideRectangle(r1.p2, r2) || checkPointInsideRectangle(r2.p1, r1) || checkPointInsideRectangle(r2.p2, r1); } private static boolean checkPointInsideRectangle(Point pnt, Rectangle rect) { int x = pnt.getX(); int y = pnt.getY(); int x1 = rect.p1.getX(); int y1 = rect.p1.getY(); int x2 = rect.p2.getX(); int y2 = rect.p2.getY(); if(x1 > x2) {int temp = x1; x1 = x2; x2 = temp;} if(y1 > y2) {int temp = y1; y1 = y2; y2 = temp;} return (x >= x1 && x <= x2 && y >= y1 && y <= y2); } }