001package squidpony.squidmath;
002
003/**
004 * Generic three dimensional coordinate class.
005 * Not cached in a pool because it is rarely used internally.
006 * @author Lewis Potter
007 * @author Eben Howard - http://squidpony.com - howard@squidpony.com
008 */
009public class Coord3D extends Coord {
010
011        public int z;
012
013        private static final long serialVersionUID = 1835370798982845336L;
014
015    /**
016     * Creates a three dimensional coordinate with the given location.
017     *
018     * @param x
019     * @param y
020     * @param z
021     */
022    public Coord3D(int x, int y, int z) {
023        super(x, y);
024        this.z = z;
025    }
026
027    public static Coord3D get(int x, int y, int z)
028    {
029        return new Coord3D(x, y, z);
030    }
031    /**
032     * Returns the linear distance between this coordinate point and the
033     * provided one.
034     *
035     * @param other
036     * @return
037     */
038    public double distance(Coord3D other) {
039        return Math.sqrt(squareDistance(other));
040    }
041
042    /**
043     * Returns the square of the linear distance between this coordinate point
044     * and the provided one.
045     *
046     * @param other
047     * @return
048     */
049    public double squareDistance(Coord3D other) {
050        double dx = x - other.x;
051        double dy = y - other.y;
052        double dz = z - other.z;
053        return dx * dx + dy * dy + dz * dz;
054    }
055
056    /**
057     * Returns the Manhattan distance between this point and the provided one.
058     * The Manhattan distance is the distance between each point on each
059     * separate axis all added together.
060     *
061     * @param other
062     * @return
063     */
064    public int manhattanDistance(Coord3D other) {
065        int distance = Math.abs(x - other.x);
066        distance += Math.abs(y - other.y);
067        distance += Math.abs(z - other.z);
068        return distance;
069    }
070
071    /**
072     * Returns the largest difference between the two points along any one axis.
073     *
074     * @param other
075     * @return
076     */
077    public int maxAxisDistance(Coord3D other) {
078        return Math.max(Math.max(Math.abs(x - other.x), Math.abs(y - other.y)), Math.abs(z - other.z));
079    }
080
081    @Override
082    public int hashCode() {
083        int hash = 5;
084        hash = 73 * hash + x;
085        hash = 73 * hash + y;
086        hash = 73 * hash + z;
087        return hash;
088    }
089
090    @Override
091    public boolean equals(Object o) {
092        if (o instanceof Coord3D) {
093            Coord3D other = (Coord3D) o;
094            return x == other.x && y == other.y && z == other.z;
095        } else {
096            return false;
097        }
098    }
099
100    @Override
101    public String toString() {
102        return "(" + x + "," + y + "," + z + ")";
103    }
104}