/*******************************************************************************
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 Point3D class represents a point in 3-dimensional space.
 * A Point3D object has an x coordinate, a y coordinate, and a z coordinate.
 *
 * Bugs:    none known
 **/
public class Point3D extends Point {

    ////////////////////////////////////////
    // Data Members
    ////////////////////////////////////////
    
    // x and y are inherited from the Point class
    protected double z;    // the z coordinate
    
    ////////////////////////////////////////
    // Constructors
    ////////////////////////////////////////
    
    /**
     * Constructs a 3-dimensional point at the given coordinates.
     * @param x the x coordinate
     * @param y the y coordinate
     * @param z the z coordinate
     **/
    public Point3D(double x, double y, double z) {
        super(x, y);        // Note the call to super, i.e. Point's constructor
        this.z = z;
    }
    
    /**
     * Constructs a 3-dimensional point at the origin (0, 0, 0)
     **/
    public Point3D() {
        this(0, 0, 0);
    }
    
    ////////////////////////////////////////
    // Public Methods
    //   Note: getX, getY, setX, and setY are inherited from Point
    ////////////////////////////////////////

    /**
     * Returns the z coordinate.
     * @return the z coordinate
     **/
    public double getZ() { return z; }

    /**
     * Sets the z coordinate to the given value.
     * @param newZ the new z coordinate
     **/
    public void setZ(double newZ) { z = newZ; }
    
    /**
     * Returns the distance to the given coordinates.
     * @param toX the x coordinate
     * @param toY the y coordinate
     * @param toZ the z coordinate
     * @return the Cartesian distance from this point to the given coordinates
     **/
    public double distance(double toX, double toY, double toZ) {
        double xDiff = x - toX;
        double yDiff = y - toY;
        double zDiff = z - toZ;
        return Math.sqrt(xDiff * xDiff + yDiff * yDiff + zDiff * zDiff);    
    }
    
    /**
     * Returns the distance to the given 3-dimensional point.
     * @param p point (3-dimensional) to calculate distance to
     * @return the Cartesian distance from this point to the given point
     **/
    public double distance(Point3D p) {
        return distance(p.getX(), p.getY(), p.getZ());
    }
    
    /**
     * Returns a string representation of the point.
     * @return the string "(<x coord>, <y coord>, <z coord>)"
     **/
    public String toString() {
        return "(" + x + ", " + y + ", " + z + ")";
    }
    
    /**
     * Determines if the given 3-dimensional point is equal to this point.  
     * Two points are considered equal if their coordinates are the same.
     * @param p the (3-dimensional) point to test equality with
     * @return true if the point has the same coordinates as this point, false
     *         otherwise
     **/
    public boolean equals(Point3D p) {
        return (super.equals(p) && (z == p.getZ()));  // Note call to super
    }
    
    /**
     * Returns a new 3-dimensional point with the same coordinates as this 
     * point.
     * @return new 3-dimensional point with same coordinates
     **/
    public Object clone() {
        return new Point3D(x, y, z);
    }
}
