001package squidpony.squidmath;
002
003import java.io.Serializable;
004
005/**
006 * Coord using double values for x and y instead of int. Not pooled.
007 * Created by Tommy Ettinger on 8/12/2015.
008 */
009public class CoordDouble implements Serializable {
010    private static final long serialVersionUID = 300L;
011    public double x;
012    public double y;
013
014    public CoordDouble()
015    {
016        this(0, 0);
017    }
018    public CoordDouble(double x, double y)
019    {
020        this.x = x;
021        this.y = y;
022    }
023    public CoordDouble(CoordDouble other)
024    {
025        x = other.x;
026        y = other.y;
027    }
028
029    public CoordDouble(Coord other)
030    {
031        x = other.x;
032        y = other.y;
033    }
034    public static CoordDouble get(double x, double y)
035    {
036        return new CoordDouble(x, y);
037    }
038    public CoordDouble getLocation()
039    {
040        return new CoordDouble(x, y);
041    }
042    public void translate(double x, double y)
043    {
044        this.x += x;
045        this.y += y;
046    }
047    public void setLocation(double x, double y)
048    {
049        this.x = x;
050        this.y = y;
051    }
052    public void setLocation(CoordDouble co)
053    {
054        x = co.x;
055        y = co.y;
056    }
057    public void move(int x, int y)
058    {
059        this.x = x;
060        this.y = y;
061    }
062    public double distance(double x2, double y2)
063    {
064        return Math.sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));
065    }
066    public double distance(CoordDouble co)
067    {
068        return Math.sqrt((co.x - x) * (co.x - x) + (co.y - y) * (co.y - y));
069    }
070    public double distanceSq(double x2, double y2)
071    {
072        return (x2 - x) * (x2 - x) + (y2 - y) * (y2 - y);
073    }
074    public double distanceSq(CoordDouble co)
075    {
076        return (co.x - x) * (co.x - x) + (co.y - y) * (co.y - y);
077    }
078
079    public double getX() {
080        return x;
081    }
082
083    public void setX(int x) {
084        this.x = x;
085    }
086
087    public double getY() {
088        return y;
089    }
090
091    public void setY(int y) {
092        this.y = y;
093    }
094
095    @Override
096        public String toString()
097    {
098        return "Coord (x " + x + ", y " + y + ")";
099    }
100
101        @Override
102        /*
103         * smelC: This is Eclipse-generated code. The previous version was
104         * Gwt-incompatible (because of Double.doubleToRawLongBits).
105         */
106        public int hashCode() {
107                final int prime = 31;
108                int result = 1;
109                long temp;
110                temp = Double.doubleToLongBits(x);
111                result = prime * result + (int) (temp ^ (temp >>> 32));
112                temp = Double.doubleToLongBits(y);
113                result = prime * result + (int) (temp ^ (temp >>> 32));
114                return result;
115        }
116
117    @Override
118    public boolean equals(Object o) {
119        if (o instanceof CoordDouble) {
120            CoordDouble other = (CoordDouble) o;
121            return x == other.x && y == other.y;
122        } else {
123            return false;
124        }
125    }
126
127}