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

Last change on this file since 8526 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

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