source: josm/trunk/src/org/openstreetmap/josm/data/coor/Coordinate.java@ 13388

Last change on this file since 13388 was 13173, checked in by Don-vip, 6 years ago

see #15310 - remove most of deprecated APIs

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