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

Last change on this file since 12659 was 12375, checked in by michael2402, 7 years ago

Document the data.coor package

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