1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.coor;
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * Northing, Easting of the projected coordinates.
|
---|
6 | *
|
---|
7 | * This class is immutable.
|
---|
8 | *
|
---|
9 | * @author Imi
|
---|
10 | */
|
---|
11 | public class EastNorth extends Coordinate {
|
---|
12 |
|
---|
13 | private static final long serialVersionUID = 1L;
|
---|
14 |
|
---|
15 | public EastNorth(double east, double north) {
|
---|
16 | super(east, north);
|
---|
17 | }
|
---|
18 |
|
---|
19 | public double east() {
|
---|
20 | return x;
|
---|
21 | }
|
---|
22 |
|
---|
23 | public double north() {
|
---|
24 | return y;
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Adds an offset to this {@link EastNorth} instance and returns the result.
|
---|
29 | * @param dEast The offset to add in east direction.
|
---|
30 | * @param dNorth The offset to add in north direction.
|
---|
31 | * @return The result.
|
---|
32 | */
|
---|
33 | public EastNorth add(double dEast, double dNorth) {
|
---|
34 | return new EastNorth(east()+dEast, north()+dNorth);
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Adds the coordinates of an other EastNorth instance to this one.
|
---|
39 | * @param other The other instance.
|
---|
40 | * @return The new EastNorth position.
|
---|
41 | */
|
---|
42 | public EastNorth add(EastNorth other) {
|
---|
43 | return new EastNorth(x+other.x, y+other.y);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Subtracts the coordinates of this EastNorth instance from the other one.
|
---|
48 | * <p>
|
---|
49 | * This produces result = en2 - this.
|
---|
50 | * @param en2 The instance to subtract this one from.
|
---|
51 | * @return The new EastNorth position.
|
---|
52 | */
|
---|
53 | @Deprecated
|
---|
54 | public EastNorth sub(EastNorth en2) {
|
---|
55 | return en2.subtract(this);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Subtracts an east/north value from this point.
|
---|
60 | * @param other The other value to subtract from this.
|
---|
61 | * @return A point with the new coordinates.
|
---|
62 | */
|
---|
63 | public EastNorth subtract(EastNorth other) {
|
---|
64 | return new EastNorth(x-other.x, y-other.y);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public EastNorth scale(double s) {
|
---|
68 | return new EastNorth(s * x, s * y);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Does a linear interpolation between two EastNorth instances.
|
---|
73 | * @param en2 The other EstNort instance.
|
---|
74 | * @param proportion The proportion the other instance influences the result.
|
---|
75 | * @return The new {@link EastNorth} position.
|
---|
76 | */
|
---|
77 | public EastNorth interpolate(EastNorth en2, double proportion) {
|
---|
78 | return new EastNorth(this.x + proportion * (en2.x - this.x),
|
---|
79 | this.y + proportion * (en2.y - this.y));
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Gets the center between two {@link EastNorth} instances.
|
---|
84 | * @param en2 The other instance.
|
---|
85 | * @return The center between this and the other instance.
|
---|
86 | */
|
---|
87 | public EastNorth getCenter(EastNorth en2) {
|
---|
88 | return new EastNorth((this.x + en2.x)/2.0, (this.y + en2.y)/2.0);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Returns the euclidean distance from this {@code EastNorth} to a specified {@code EastNorth}.
|
---|
93 | *
|
---|
94 | * @param en the specified coordinate to be measured against this {@code EastNorth}
|
---|
95 | * @return the euclidean distance from this {@code EastNorth} to a specified {@code EastNorth}
|
---|
96 | * @since 6166
|
---|
97 | */
|
---|
98 | public double distance(final EastNorth en) {
|
---|
99 | return super.distance(en);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Returns the square of the euclidean distance from this {@code EastNorth} to a specified {@code EastNorth}.
|
---|
104 | *
|
---|
105 | * @param en the specified coordinate to be measured against this {@code EastNorth}
|
---|
106 | * @return the square of the euclidean distance from this {@code EastNorth} to a specified {@code EastNorth}
|
---|
107 | * @since 6166
|
---|
108 | */
|
---|
109 | public double distanceSq(final EastNorth en) {
|
---|
110 | return super.distanceSq(en);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Counts length (distance from [0,0]) of this.
|
---|
115 | *
|
---|
116 | * @return length of this
|
---|
117 | */
|
---|
118 | public double length() {
|
---|
119 | return Math.sqrt(x*x + y*y);
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Returns the heading, in radians, that you have to use to get from
|
---|
124 | * this EastNorth to another. Heading is mapped into [0, 2pi)
|
---|
125 | *
|
---|
126 | * @param other the "destination" position
|
---|
127 | * @return heading
|
---|
128 | */
|
---|
129 | public double heading(EastNorth other) {
|
---|
130 | double hd = Math.atan2(other.east() - east(), other.north() - north());
|
---|
131 | if (hd < 0) {
|
---|
132 | hd = 2 * Math.PI + hd;
|
---|
133 | }
|
---|
134 | return hd;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Replies true if east and north are different from Double.NaN and not infinite
|
---|
139 | *
|
---|
140 | * @return true if east and north are different from Double.NaN and not infinite
|
---|
141 | */
|
---|
142 | public boolean isValid() {
|
---|
143 | return !Double.isNaN(x) && !Double.isNaN(y) && !Double.isInfinite(x) && !Double.isInfinite(y);
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Returns an EastNorth representing the this EastNorth rotated around
|
---|
148 | * a given EastNorth by a given angle
|
---|
149 | * @param pivot the center of the rotation
|
---|
150 | * @param angle the angle of the rotation
|
---|
151 | * @return EastNorth rotated object
|
---|
152 | */
|
---|
153 | public EastNorth rotate(EastNorth pivot, double angle) {
|
---|
154 | double cosPhi = Math.cos(angle);
|
---|
155 | double sinPhi = Math.sin(angle);
|
---|
156 | double x = east() - pivot.east();
|
---|
157 | double y = north() - pivot.north();
|
---|
158 | double nx = cosPhi * x + sinPhi * y + pivot.east();
|
---|
159 | double ny = -sinPhi * x + cosPhi * y + pivot.north();
|
---|
160 | return new EastNorth(nx, ny);
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Override public String toString() {
|
---|
164 | return "EastNorth[e="+x+", n="+y+"]";
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Compares two EastNorth values
|
---|
169 | *
|
---|
170 | * @return true if "x" and "y" values are within 1E-6 of each other
|
---|
171 | */
|
---|
172 | public boolean equalsEpsilon(EastNorth other, double e) {
|
---|
173 | return Math.abs(x - other.x) < e && Math.abs(y - other.y) < e;
|
---|
174 | }
|
---|
175 | }
|
---|