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

Last change on this file since 6167 was 6167, checked in by Don-vip, 11 years ago

see #8987 - add missing clone() methods to fix broken plugins

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