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

Last change on this file since 8906 was 7509, checked in by stoecker, 10 years ago

remove tabs

  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.coor;
3
4import java.io.Serializable;
5
6import 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 */
20abstract 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 protected final int computeHashCode(int init) {
116 final int prime = 31;
117 int result = init;
118 long temp;
119 temp = java.lang.Double.doubleToLongBits(x);
120 result = prime * result + (int) (temp ^ (temp >>> 32));
121 temp = java.lang.Double.doubleToLongBits(y);
122 result = prime * result + (int) (temp ^ (temp >>> 32));
123 return result;
124 }
125
126 @Override
127 public int hashCode() {
128 return computeHashCode(1);
129 }
130
131 @Override
132 public boolean equals(Object obj) {
133 if (this == obj)
134 return true;
135 if (obj == null || getClass() != obj.getClass())
136 return false;
137 Coordinate other = (Coordinate) obj;
138 if (java.lang.Double.doubleToLongBits(x) != java.lang.Double.doubleToLongBits(other.x))
139 return false;
140 if (java.lang.Double.doubleToLongBits(y) != java.lang.Double.doubleToLongBits(other.y))
141 return false;
142 return true;
143 }
144}
Note: See TracBrowser for help on using the repository browser.