/**
 * Point Implementation
 * CSCI 162
 * Professor Killian
 * Fall 2018
 */
public class Point implements Cloneable, Comparable<Point> {

    private int x;
    private int y;

    public Point () {
        this (0, 0);
    }

    public Point (int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX () {
        return this.x;
    }

    public int getY () {
        return this.y;
    }

    public void setLocation (int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void translate (int dx, int dy) {
        this.x += dx;
        this.y += dy;
    }

    public double distance (Point p) {
        int dx = this.x - p.x;
        int dy = this.y - p.y;
        return Math.sqrt (dx * dx + dy * dy);
    }

    public double distanceFromOrigin () {
        return distance (new Point ());
    }

    public String toString () {
        return "(" + this.x + ", " + this.y + ")";
    }

    public boolean equals (Object obj) {
        if (!(obj instanceof Point)) {
            return false;
        }
        Point p = (Point)obj;
        return (this.x == p.x) && (this.y == p.y);
    }

    public Point clone () {
        Point p = null;
        try {
            p = (Point)super.clone();
            // Note: For all non-primitive members, we would need to clone each one.
            //       If we had a String member called name, we'd need to say:
            //         p.name = (String) this.name.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException ("Point doesn't implement the Cloneable interface");
        }
        return p;
    }

    public int compareTo (Point p) {
        // this trick only works with integers
        if (this.x != p.x) {
            return this.x - p.x;
        } else {
            return this.y - p.y;
        }
    }
}
