-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoint.java
More file actions
182 lines (150 loc) · 3.43 KB
/
Point.java
File metadata and controls
182 lines (150 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package Individual_Project;
import java.util.Objects;
/**
* A class representing a point in the map using its longitude x and its latitude y.
*/
public class Point {
private double x;
private double y;
/**
* @param x longitude
* @param y latitude
*/
public Point(double x, double y) {
this.x = x;
this.y = y;
}
/**
* @return the x
*/
public double getX() {
return x;
}
/**
* @param x longitude to set
*/
public void setX(double x) {
this.x = x;
}
/**
* @return the y
*/
public double getY() {
return y;
}
/**
* @param y latitude to set
*/
public void setY(double y) {
this.y = y;
}
/**
* @param p1
* @param p2
* @return euclidean distance of the two input points
*/
public static double euclideanDistance(Point p1, Point p2) {
return Math.sqrt((Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2)));
}
/**
* @param lat1 the latitude of the first point
* @param lon1 the longitude of the first point
* @param lat2 the latitude of the second point
* @param lon2 the longitude of the second point
* @return the haversine formula as described here https://rosettacode.org/wiki/Haversine_formula
*/
public static final double R = 6372.8; // In kilometers
/**
*
* Haversine
*
* @param lat1 the lat1
* @param lon1 the lon1
* @param lat2 the lat2
* @param lon2 the lon2
* @return double
*/
public static double haversine(double lat1, double lon1, double lat2, double lon2) {
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
double a = Math.pow(Math.sin(dLat / 2),2) + Math.pow(Math.sin(dLon / 2),2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.asin(Math.sqrt(a));
return round(R * c, 3);
}
/**
* @return the Node of the closest point
*/
Node closestNodeByPoint() {
Node minNode = null;
double minDistance = 1000000000;
for (Node currentNode : Node.nodes) {
if (haversine(y, x, currentNode.getY(), currentNode.getX()) < minDistance) {
minNode = currentNode;
minDistance = haversine(y, x, currentNode.getY(), currentNode.getX());
}
}
if (minNode != null) {
return minNode;
}
else {
System.out.println("Error in finding closest node");
return null;
}
}
@Override
/**
*
* Hash code
*
* @return int
*/
public int hashCode() {
return Objects.hash(x, y);
}
@Override
/**
*
* Equals
*
* @param obj the obj
* @return boolean
*/
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Point other = (Point) obj;
return Double.doubleToLongBits(x) == Double.doubleToLongBits(other.x)
&& Double.doubleToLongBits(y) == Double.doubleToLongBits(other.y);
}
/**
*
* Round
*
* @param value the value
* @param places the places
* @return double
*/
public static double round(double value, int places) {
double scale = Math.pow(10, places);
return Math.round(value * scale) / scale;
}
@Override
/**
*
* To string
*
* @return String
*/
public String toString() {
return "Point with x = " + x + ", y = " + y;
}
}