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