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 | public EastNorth(double east, double north) {
|
---|
14 | super(east,north);
|
---|
15 | }
|
---|
16 |
|
---|
17 | public double east() {
|
---|
18 | return x;
|
---|
19 | }
|
---|
20 |
|
---|
21 | public double north() {
|
---|
22 | return y;
|
---|
23 | }
|
---|
24 |
|
---|
25 | public EastNorth add(double dx, double dy) {
|
---|
26 | return new EastNorth(x+dx, y+dy);
|
---|
27 | }
|
---|
28 |
|
---|
29 | public EastNorth add(EastNorth other) {
|
---|
30 | return new EastNorth(x+other.x, y+other.y);
|
---|
31 | }
|
---|
32 |
|
---|
33 | public EastNorth scale(double s) {
|
---|
34 | return new EastNorth(s * x, s * y);
|
---|
35 | }
|
---|
36 |
|
---|
37 | public EastNorth interpolate(EastNorth en2, double proportion) {
|
---|
38 | return new EastNorth(this.x + proportion * (en2.x - this.x),
|
---|
39 | this.y + proportion * (en2.y - this.y));
|
---|
40 | }
|
---|
41 |
|
---|
42 | public EastNorth getCenter(EastNorth en2) {
|
---|
43 | return new EastNorth((this.x + en2.x)/2.0, (this.y + en2.y)/2.0);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Returns the euclidean distance from this {@code EastNorth} to a specified {@code EastNorth}.
|
---|
48 | *
|
---|
49 | * @param en the specified coordinate to be measured against this {@code EastNorth}
|
---|
50 | * @return the euclidean distance from this {@code EastNorth} to a specified {@code EastNorth}
|
---|
51 | * @since 6166
|
---|
52 | */
|
---|
53 | public double distance(final EastNorth en) {
|
---|
54 | return super.distance(en);
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Returns the square of the euclidean distance from this {@code EastNorth} to a specified {@code EastNorth}.
|
---|
59 | *
|
---|
60 | * @param en the specified coordinate to be measured against this {@code EastNorth}
|
---|
61 | * @return the square of the euclidean distance from this {@code EastNorth} to a specified {@code EastNorth}
|
---|
62 | * @since 6166
|
---|
63 | */
|
---|
64 | public double distanceSq(final EastNorth en) {
|
---|
65 | return super.distanceSq(en);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Counts length (distance from [0,0]) of this.
|
---|
70 | *
|
---|
71 | * @return length of this
|
---|
72 | */
|
---|
73 | public double length(){
|
---|
74 | return Math.sqrt(x*x + y*y);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Returns the heading, in radians, that you have to use to get from
|
---|
79 | * this EastNorth to another. Heading is mapped into [0, 2pi)
|
---|
80 | *
|
---|
81 | * @param other the "destination" position
|
---|
82 | * @return heading
|
---|
83 | */
|
---|
84 | public double heading(EastNorth other) {
|
---|
85 | double hd = Math.atan2(other.east() - east(), other.north() - north());
|
---|
86 | if(hd < 0) {
|
---|
87 | hd = 2 * Math.PI + hd;
|
---|
88 | }
|
---|
89 | return hd;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Replies true if east and north are different from Double.NaN and not infinite
|
---|
94 | *
|
---|
95 | * @return true if east and north are different from Double.NaN and not infinite
|
---|
96 | */
|
---|
97 | public boolean isValid() {
|
---|
98 | return !Double.isNaN(x) && !Double.isNaN(y) && !Double.isInfinite(x) && !Double.isInfinite(y);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public EastNorth sub(EastNorth en) {
|
---|
102 | return new EastNorth(en.east() - east(), en.north() - north());
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Returns an EastNorth representing the this EastNorth rotated around
|
---|
107 | * a given EastNorth by a given angle
|
---|
108 | * @param pivot the center of the rotation
|
---|
109 | * @param angle the angle of the rotation
|
---|
110 | * @return EastNorth rotated object
|
---|
111 | */
|
---|
112 | public EastNorth rotate(EastNorth pivot, double angle) {
|
---|
113 | double cosPhi = Math.cos(angle);
|
---|
114 | double sinPhi = Math.sin(angle);
|
---|
115 | double x = east() - pivot.east();
|
---|
116 | double y = north() - pivot.north();
|
---|
117 | double nx = cosPhi * x + sinPhi * y + pivot.east();
|
---|
118 | double ny = -sinPhi * x + cosPhi * y + pivot.north();
|
---|
119 | return new EastNorth(nx, ny);
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override public String toString() {
|
---|
123 | return "EastNorth[e="+x+", n="+y+"]";
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Compares two EastNorth values
|
---|
128 | *
|
---|
129 | * @return true if "x" and "y" values are within 1E-6 of each other
|
---|
130 | */
|
---|
131 | public boolean equalsEpsilon(EastNorth other, double e) {
|
---|
132 | return (Math.abs(x - other.x) < e && Math.abs(y - other.y) < e);
|
---|
133 | }
|
---|
134 | }
|
---|