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