package oving1;

public class Rectangle {
	int x1;
	int y1;
	int x2;
	int y2;

	public Rectangle(int x1, int y1, int x2, int y2) {
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
	}

	int getMinX() {
		return Math.min(x1, x2);
	}

	int getMinY() {
		return Math.min(y1, y2);
	}

	int getMaxX() {
		return Math.max(x1, x2);
	}

	int getMaxY() {
		return Math.max(y1, y2);
	}

	int getWidth() {
		return getMaxX() - getMinX();
	}

	int getHeight() {
		return getMaxY() - getMinY();
	}

	boolean isEmpty() {
		return getWidth() == 0 || getHeight() == 0;
	}

	boolean contains(int x, int y) {
		return x >= getMinX() && x <= getMaxX() && y >= getMinY() && y <= getMaxY();
	}

	boolean contains(Rectangle rect) {
		return contains(rect.x1, rect.y1) && contains(rect.x2, rect.y1) && contains(rect.x1, rect.y2)
				&& contains(rect.x2, rect.y2);
	}

	boolean add(int x, int y) {
		int px1 = x1;
		int px2 = x2;
		int py1 = y1;
		int py2 = y2;
		x1 = Math.min(x, x1);
		x2 = Math.max(x, x2);
		y1 = Math.min(y, y1);
		y2 = Math.max(y, y2);
		return px1 == x1 && px2 == x2 && py1 == y1 && py2 == y2;
	}

	boolean add(Rectangle rect) {
		return add(rect.x1, rect.y1) || add(rect.x2, rect.y1) || add(rect.x1, rect.y2) || add(rect.x2, rect.y2);
	}

	Rectangle union(Rectangle rect) {
		int px1 = x1;
		int px2 = x2;
		int py1 = y1;
		int py2 = y2;
		add(rect);
		Rectangle r = new Rectangle(x1, y1, x2, y2);
		x1 = px1;
		x2 = px2;
		y1 = py1;
		y2 = py2;
		return r;
	}

	@Override
	public String toString() {
		return "";
	}
}
