/*******************************************************************************
Author:              Rebecca Hasti, hasti@cs.wisc.edu, 
			 copyright 2000, 2001, all rights reserved
Compiler:            Metrowerks CodeWarrior (JDK 1.2)
Platform:            Windows (NT 4.0 or 95 or 2000)
*******************************************************************************/

/**
 * An object of the Point class represents a point in the xy plane.
 * A point has an x coordinate and a y coordinate.
 *
 * Bugs:    none known
 **/
public class Point {

    ////////////////////////////////////////
    // Data Members
    //   Note: protected so they are accessible by subclasses
    ////////////////////////////////////////
    
    protected double x;        // the x coordinate
    protected double y;        // the y coordinate
    
    ////////////////////////////////////////
    // Constructors
    //   Note: overloaded
    ////////////////////////////////////////
    
    /**
     * Constructs a point with the given x and y coordinates.
     * @param x the x coordinate 
     * @param y the y coordinate
     **/
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    
    /**
     * Constructs a point at the origin (0, 0).
     **/
    public Point() {
        this(0, 0);
    }
    
    ////////////////////////////////////////
    // Accessor Methods
    ////////////////////////////////////////
    
    /**
     * Returns the x coordinate.
     * @return the x coordinate
     **/
    public double getX() { return x; }
    
    /**
     * Returns the y coordinate.
     * @return the y coordinate
     **/
    public double getY() { return y; }
    
    ////////////////////////////////////////
    // Mutator Methods
    ////////////////////////////////////////
    
    /**
     * Sets the x coordinate to the given value.
     * @param newX the new x coordinate
     **/
    public void setX(double newX) { x = newX; }
    
    /**
     * Sets the y coordinate to the given value.
     * @param newY the new y coordinate
     **/
    public void setY(double newY) { y = newY; }
    
    ////////////////////////////////////////
    // Other Public Methods
    //   Note: distance is overloaded;
    //         toString and clone are overridden
    ////////////////////////////////////////
    
    /**
     * Returns the distance to the given point.
     * @param p point to calculate distance to
     * @return the Cartesian distance from this point to the given point
     **/
    public double distance(Point p) {
        return distance(p.getX(), p.getY());
    }
    
    /**
     * Returns the distance to the given coordinates.
     * @param toX the x coordinate
     * @param toY the y coordinate
     * @return the Cartesian distance from this point to the given coordinates
     **/
    public double distance(double toX, double toY) {
        double xDiff = x - toX;
        double yDiff = y - toY;
        return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
    }
    
    /**
     * Returns a string representation of the point.
     * @return the string "(<x coord>, <y coord>)"
     **/
    public String toString() {
        return "(" + x + ", " + y + ")";
    }
    
    /**
     * Determines if the given point is equal to this point.  Two points are
     * considered equal if their x and y coordinates are the same.
     * @param p the point to test equality with
     * @return true if the point has the same coordinates as this point, false
     *         otherwise
     **/
    public boolean equals(Point p) {
        return (x == p.getX()) && (y == p.getY());
    }
    
    /**
     * Returns a new point with the same coordinates as this point.
     * @return new point with same coordinates
     **/
    public Object clone() {
        return new Point(x, y);
    }
}
