/**
 * A representation of a point in N-dimensional space. In the current 
 * implementation N == 3.
 */

public class Point {
    private static final int MAX_DIM = 3;
    private int[] d;

    /* Representation Invariant: MAX_DIM == d.length */
	

    /** Create a new point given a string with MAX_DIM integers on it 
     * @param s A string containing MAX_DIM integers
     */
    public Point(String s) {
	d = new int[MAX_DIM];
	
	int prev = 0;
	int end = s.indexOf(' ', prev);
	int i = 0;
	while(end != -1) {
	    d[i] = Integer.parseInt(s.substring(prev, end));
	    prev = end + 1;
	    i++;
	    end = s.indexOf(' ', prev);
	}
	d[i] = Integer.parseInt(s.substring(prev));
    }
	
	
    /** Create a point in 3 dimensional space given three integers
     * Requires: MAX_DIM == 3
     * @param x
     * @param y
     * @param z
     */
    /* Note that we would need to create more constructors, or a constructor 
     * that takes an array as an argument if we wanted to support Points in 
     * different numbers of dimensions.
     */
    public Point(int x, int y, int z) { 
	d = new int[3];
	d[0] = x;
	d[1] = y;
	d[2] = z;
    }
	
    /**
     * Return the value in the ith dimension of this point
     * Requires: 0 <= i < MAX_DIM
     * @param i the dimension
     * @return the value of dimension i
     */
    public double get(int i) {
	return d[i];
    }	
	
    /**
     * Calculate the distance of this point from point p
     * Requires: p must not be null.
     * @param p The point to get the distance from
     * @return The distance from this point to point p
     */
    public double distanceFrom(Point p) {
	double sum = 0.0;
	for(int i = 0; i < d.length; i++) {
	    sum += (d[i] - p.get(i)) * (d[i] - p.get(i));
	}
	return Math.sqrt(sum);
    }

    /**
     * Returns true if this point has the same value in dimension k as 
     * point p.
     * Requires: p must not be null, and it must have at least k dimensions
     * Requires: 0 <= k < MAX_DIM
     * @param p The point to compare.
     * @param k The dimension of interest.
     * @return true if the points are equal in dimension k.
     */
    public boolean sameDimension(Point p, int k) {
	return (this.d[k] == p.d[k]);
    }
	
    /**
     * Construct the string representation of a Point: a list of the 
     * values in for each dimension.
     * @return The string represenation
     */
    public String toString() {
	String result = "";
	for(int i = 0; i < d.length-1; i++) {
	    result += d[i] + " ";
	}
	result += d[d.length-1];
	return result;
    }

}
