source: josm/trunk/src/org/openstreetmap/josm/data/coor/EastNorth.java@ 12669

Last change on this file since 12669 was 12669, checked in by Don-vip, 7 years ago

see #15182 - remove dependence on JMapViewer for package data.coor (only useful for imagery)

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