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