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

Last change on this file was 18468, checked in by taylor.smock, 2 years ago

Fix #21893: Align movement of objects when the last selected way has two nodes and the ctrl/meta modifier is pressed (patch by cmuelle8, modified)

  • Property svn:eol-style set to native
File size: 6.5 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[626]2package org.openstreetmap.josm.data.coor;
[477]3
[626]4/**
[655]5 * Northing, Easting of the projected coordinates.
[1169]6 *
[626]7 * This class is immutable.
[1169]8 *
[626]9 * @author Imi
10 */
[6173]11public class EastNorth extends Coordinate {
[626]12
[8308]13 private static final long serialVersionUID = 1L;
14
[12375]15 /**
16 * A zero constant
17 */
[12093]18 public static final EastNorth ZERO = new EastNorth(0, 0);
19
[10334]20 /**
21 * Constructs a new {@code EastNorth}.
22 * @param east easting
23 * @param north northing
24 */
[1169]25 public EastNorth(double east, double north) {
[8510]26 super(east, north);
[1169]27 }
[626]28
[10334]29 /**
30 * Returns easting.
31 * @return easting
32 */
[1169]33 public double east() {
34 return x;
35 }
[626]36
[10334]37 /**
38 * Returns northing.
39 * @return northing
40 */
[1169]41 public double north() {
42 return y;
43 }
44
[8549]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);
[1169]53 }
54
[8549]55 /**
[18342]56 * Adds the coordinates of another EastNorth instance to this one.
[8549]57 * @param other The other instance.
58 * @return The new EastNorth position.
59 */
[4134]60 public EastNorth add(EastNorth other) {
61 return new EastNorth(x+other.x, y+other.y);
62 }
63
[8549]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
[10334]73 /**
74 * Scales this {@link EastNorth} instance to a given factor and returns the result.
75 * @param s factor
76 * @return The result.
77 */
[4134]78 public EastNorth scale(double s) {
79 return new EastNorth(s * x, s * y);
80 }
81
[8549]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 */
[1169]88 public EastNorth interpolate(EastNorth en2, double proportion) {
[10915]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);
[1169]92 }
93
[8549]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 */
[1724]99 public EastNorth getCenter(EastNorth en2) {
[10915]100 // The JIT will inline this for us, it is as fast as the normal /2 approach
101 return interpolate(en2, .5);
[1724]102 }
103
[6162]104 /**
[6166]105 * Returns the euclidean distance from this {@code EastNorth} to a specified {@code EastNorth}.
[7509]106 *
[6166]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
[6162]110 */
[6166]111 public double distance(final EastNorth en) {
112 return super.distance(en);
[3837]113 }
[6173]114
[6166]115 /**
116 * Returns the square of the euclidean distance from this {@code EastNorth} to a specified {@code EastNorth}.
[7509]117 *
[6166]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 }
[3837]125
[1076]126 /**
[6162]127 * Counts length (distance from [0,0]) of this.
[7509]128 *
[6162]129 * @return length of this
130 */
[8510]131 public double length() {
[6162]132 return Math.sqrt(x*x + y*y);
133 }
[6173]134
[6162]135 /**
[1169]136 * Returns the heading, in radians, that you have to use to get from
[1076]137 * this EastNorth to another. Heading is mapped into [0, 2pi)
[1169]138 *
[1076]139 * @param other the "destination" position
[1169]140 * @return heading
[1076]141 */
142 public double heading(EastNorth other) {
143 double hd = Math.atan2(other.east() - east(), other.north() - north());
[8510]144 if (hd < 0) {
[6173]145 hd = 2 * Math.PI + hd;
146 }
[1169]147 return hd;
[1076]148 }
[6069]149
[4640]150 /**
[18468]151 * Behaves exactly like {@link EastNorth#heading(EastNorth)} if {@code refHeading} is {@code 0.0},
152 * otherwise gives a heading not relative to north (0.0). It replies the radians needed to add to
153 * {@code refHeading} to get from this EastNorth to another.
154 * @param other the "destination" position
155 * @param refHeading origin ("false" north if different from {@code 0.0})
156 * @return heading with respect to {@code refHeading} as origin
157 * @since 18468
158 */
159 public double heading(EastNorth other, double refHeading) {
160 double hd = heading(other);
161 hd -= refHeading;
162 if (hd < 0) {
163 hd = 2 * Math.PI + hd;
164 }
165 return hd;
166 }
167
168 /**
[6670]169 * Replies true if east and north are different from Double.NaN and not infinite
[6069]170 *
[6670]171 * @return true if east and north are different from Double.NaN and not infinite
[4640]172 */
173 public boolean isValid() {
[6669]174 return !Double.isNaN(x) && !Double.isNaN(y) && !Double.isInfinite(x) && !Double.isInfinite(y);
[4640]175 }
[1169]176
[1076]177 /**
[18342]178 * Returns an EastNorth representing this EastNorth rotated around
[1076]179 * a given EastNorth by a given angle
180 * @param pivot the center of the rotation
181 * @param angle the angle of the rotation
[1169]182 * @return EastNorth rotated object
[1076]183 */
184 public EastNorth rotate(EastNorth pivot, double angle) {
185 double cosPhi = Math.cos(angle);
186 double sinPhi = Math.sin(angle);
187 double x = east() - pivot.east();
188 double y = north() - pivot.north();
[10378]189 // CHECKSTYLE.OFF: SingleSpaceSeparator
[1076]190 double nx = cosPhi * x + sinPhi * y + pivot.east();
191 double ny = -sinPhi * x + cosPhi * y + pivot.north();
[10378]192 // CHECKSTYLE.ON: SingleSpaceSeparator
[1076]193 return new EastNorth(nx, ny);
194 }
195
[8846]196 @Override
197 public String toString() {
198 return "EastNorth[e="+x+", n="+y+']';
[1169]199 }
200
[1076]201 /**
202 * Compares two EastNorth values
[9243]203 * @param other other east.north
204 * @param e epsilon
[1076]205 *
[9243]206 * @return true if "x" and "y" values are within epsilon {@code e} of each other
[1076]207 */
208 public boolean equalsEpsilon(EastNorth other, double e) {
[8345]209 return Math.abs(x - other.x) < e && Math.abs(y - other.y) < e;
[1076]210 }
[626]211}
Note: See TracBrowser for help on using the repository browser.