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