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

Last change on this file since 9375 was 9375, checked in by simon04, 8 years ago

Java 7: use Objects.equals and Objects.hash (fixup r9371)

  • 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
4import java.io.Serializable;
5import java.util.Objects;
6
7import 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 */
21abstract 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 * Converts to single point BBox.
96 *
97 * @return single point BBox defined by this coordinate.
98 * @since 6203
99 */
100 public BBox toBBox() {
101 return new BBox(x, y);
102 }
103
104 /**
105 * Creates bbox around this coordinate. Coordinate defines
106 * center of bbox, its edge will be 2*r.
107 *
108 * @param r size
109 * @return BBox around this coordinate
110 * @since 6203
111 */
112 public BBox toBBox(final double r) {
113 return new BBox(x - r, y - r, x + r, y + r);
114 }
115
116 @Override
117 public int hashCode() {
118 return Objects.hash(x, y);
119 }
120
121 @Override
122 public boolean equals(Object obj) {
123 if (this == obj) return true;
124 if (obj == null || getClass() != obj.getClass()) return false;
125 Coordinate that = (Coordinate) obj;
126 return Double.compare(that.x, x) == 0 &&
127 Double.compare(that.y, y) == 0;
128 }
129}
Note: See TracBrowser for help on using the repository browser.