package point;

public class Point implements Comparable, Cloneable {
	public static double DEFAULT_X = 0;
	public static double DEFAULT_Y = 0;
	public static final Point origin = new Point(DEFAULT_X, DEFAULT_Y);
	
	private double x;
	private double y;
	
	Point() {
		this(DEFAULT_X, DEFAULT_Y);
	}
	
	Point(double x, double y) {
		this.x = x;
		this.y = y;
	}
	
	public double getX() { return x; }
	public double getY() { return y; }
	
	public void setX(double x) { this.x = x; }
	public void setY(double y) { this.y = y; }
	
	public double distance(Point p) {
		double xDiff = x - p.getX();
		double yDiff = y - p.getY();
		return Math.sqrt((xDiff * xDiff) + (yDiff * yDiff));
	}
	
	static class NotAPointException extends ClassCastException {
		NotAPointException() {
			super();
		}
		NotAPointException(String s) {
			super(s);
		}
	}
	
	public boolean equals(Object o) {
		if (!(o instanceof Point)) {
			String err = ("Cannot test for equality; " + o + " is not a Point.");
			NotAPointException n = new NotAPointException(err);
			throw n;
		}
		return (((Point) o).x == x && ((Point) o).y == y);
	}
	
	public String toString() {
		return ("(" + x + ", " + y + ")");
	}

	public int compareTo(Object o) {
		if (!(o instanceof Point)) {
			String err = ("Cannot compare; " + o + " is not a Point.");
			NotAPointException n = new NotAPointException(err);
			throw n;
		}
		double myDist = distance(origin);
		double dist = ((Point) o).distance(origin);
		if (myDist > dist) {
			return 1;
		} else if (dist > myDist) {
			return (int) (Math.min(dist-myDist, -1));			
		}
		return 0;
	}
	
	public Point clone() {
		return (new Point(x,y));
	}
}