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

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

Checkstyle 6.19: enable SingleSpaceSeparator and fix violations

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