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

Last change on this file since 14273 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
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 /**
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 */
[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 /**
[6670]151 * Replies true if east and north are different from Double.NaN and not infinite
[6069]152 *
[6670]153 * @return true if east and north are different from Double.NaN and not infinite
[4640]154 */
155 public boolean isValid() {
[6669]156 return !Double.isNaN(x) && !Double.isNaN(y) && !Double.isInfinite(x) && !Double.isInfinite(y);
[4640]157 }
[1169]158
[1076]159 /**
[1209]160 * Returns an EastNorth representing the this EastNorth rotated around
[1076]161 * a given EastNorth by a given angle
162 * @param pivot the center of the rotation
163 * @param angle the angle of the rotation
[1169]164 * @return EastNorth rotated object
[1076]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();
[10378]171 // CHECKSTYLE.OFF: SingleSpaceSeparator
[1076]172 double nx = cosPhi * x + sinPhi * y + pivot.east();
173 double ny = -sinPhi * x + cosPhi * y + pivot.north();
[10378]174 // CHECKSTYLE.ON: SingleSpaceSeparator
[1076]175 return new EastNorth(nx, ny);
176 }
177
[8846]178 @Override
179 public String toString() {
180 return "EastNorth[e="+x+", n="+y+']';
[1169]181 }
182
[1076]183 /**
184 * Compares two EastNorth values
[9243]185 * @param other other east.north
186 * @param e epsilon
[1076]187 *
[9243]188 * @return true if "x" and "y" values are within epsilon {@code e} of each other
[1076]189 */
190 public boolean equalsEpsilon(EastNorth other, double e) {
[8345]191 return Math.abs(x - other.x) < e && Math.abs(y - other.y) < e;
[1076]192 }
[626]193}
Note: See TracBrowser for help on using the repository browser.