1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.coor;
|
---|
3 |
|
---|
4 | import java.io.Serializable;
|
---|
5 | import java.util.Objects;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Base class of points of both coordinate systems.
|
---|
11 | *
|
---|
12 | * The variables are default package protected to allow routines in the
|
---|
13 | * data package to access them directly.
|
---|
14 | *
|
---|
15 | * As the class itself is package protected too, it is not visible
|
---|
16 | * outside of the data package. Routines there should only use LatLon or
|
---|
17 | * EastNorth.
|
---|
18 | *
|
---|
19 | * @since 6162
|
---|
20 | */
|
---|
21 | abstract class Coordinate implements Serializable {
|
---|
22 |
|
---|
23 | protected final double x;
|
---|
24 | protected final double y;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Construct the point with latitude / longitude values.
|
---|
28 | *
|
---|
29 | * @param x X coordinate of the point.
|
---|
30 | * @param y Y coordinate of the point.
|
---|
31 | */
|
---|
32 | Coordinate(double x, double y) {
|
---|
33 | this.x = x; this.y = y;
|
---|
34 | }
|
---|
35 |
|
---|
36 | public double getX() {
|
---|
37 | return x;
|
---|
38 | }
|
---|
39 |
|
---|
40 | public double getY() {
|
---|
41 | return y;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Returns the euclidean distance from this {@code Coordinate} to a specified {@code Coordinate}.
|
---|
46 | *
|
---|
47 | * @param coor the specified coordinate to be measured against this {@code Coordinate}
|
---|
48 | * @return the euclidean distance from this {@code Coordinate} to a specified {@code Coordinate}
|
---|
49 | * @since 6166
|
---|
50 | */
|
---|
51 | protected final double distance(final Coordinate coor) {
|
---|
52 | return distance(coor.x, coor.y);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Returns the euclidean distance from this {@code Coordinate} to a specified coordinate.
|
---|
57 | *
|
---|
58 | * @param px the X coordinate of the specified point to be measured against this {@code Coordinate}
|
---|
59 | * @param py the Y coordinate of the specified point to be measured against this {@code Coordinate}
|
---|
60 | * @return the euclidean distance from this {@code Coordinate} to a specified coordinate
|
---|
61 | * @since 6166
|
---|
62 | */
|
---|
63 | public final double distance(final double px, final double py) {
|
---|
64 | final double dx = this.x-px;
|
---|
65 | final double dy = this.y-py;
|
---|
66 | return Math.sqrt(dx*dx + dy*dy);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Returns the square of the euclidean distance from this {@code Coordinate} to a specified {@code Coordinate}.
|
---|
71 | *
|
---|
72 | * @param coor the specified coordinate to be measured against this {@code Coordinate}
|
---|
73 | * @return the square of the euclidean distance from this {@code Coordinate} to a specified {@code Coordinate}
|
---|
74 | * @since 6166
|
---|
75 | */
|
---|
76 | protected final double distanceSq(final Coordinate coor) {
|
---|
77 | return distanceSq(coor.x, coor.y);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Returns the square of euclidean distance from this {@code Coordinate} to a specified coordinate.
|
---|
82 | *
|
---|
83 | * @param px the X coordinate of the specified point to be measured against this {@code Coordinate}
|
---|
84 | * @param py the Y coordinate of the specified point to be measured against this {@code Coordinate}
|
---|
85 | * @return the square of the euclidean distance from this {@code Coordinate} to a specified coordinate
|
---|
86 | * @since 6166
|
---|
87 | */
|
---|
88 | public final double distanceSq(final double px, final double py) {
|
---|
89 | final double dx = this.x-px;
|
---|
90 | final double dy = this.y-py;
|
---|
91 | return dx*dx + dy*dy;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Creates bbox around this coordinate. Coordinate defines
|
---|
96 | * center of bbox, its edge will be 2*r.
|
---|
97 | *
|
---|
98 | * @param r size
|
---|
99 | * @return BBox around this coordinate
|
---|
100 | * @since 6203
|
---|
101 | */
|
---|
102 | public BBox toBBox(final double r) {
|
---|
103 | return new BBox(x - r, y - r, x + r, y + r);
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public int hashCode() {
|
---|
108 | return Objects.hash(x, y);
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public boolean equals(Object obj) {
|
---|
113 | if (this == obj) return true;
|
---|
114 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
115 | Coordinate that = (Coordinate) obj;
|
---|
116 | return Double.compare(that.x, x) == 0 &&
|
---|
117 | Double.compare(that.y, y) == 0;
|
---|
118 | }
|
---|
119 | }
|
---|