forked from danielpaul/Java-Data-Structures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistance.java
More file actions
30 lines (18 loc) · 902 Bytes
/
distance.java
File metadata and controls
30 lines (18 loc) · 902 Bytes
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
/* Find distance between two GPS locations. */
public class distance {
public static void main(String[] args) {
double[] a = {53.3807, -6.594596000000024}; // Maynooth University.
double[] b = {40.6892494, -74.0445004}; // Statue of Liberty.
double distance = getDistance(a, b);
System.out.println("Distance between point a (" + a[0] + "," + a[1] + ") and b (" + b[0] + "," + b[1] + ") is " + distance + "km.");
}
public static double getDistance(double[] a, double[] b) {
final int r = 6371; // Radius of the earth.
double h = haversin(Math.toRadians(b[0] - a[0])) + (Math.cos(a[0]) * Math.cos(b[0]) * haversin(Math.toRadians(b[1] - a[1])));
double d = 2 * r * Math.asin(Math.sqrt(h));
return (haversin(d/r) * 10000) / 0.62137; // Convert to Km before return.
}
public static double haversin(double rad) {
return (1 - Math.cos(rad)) / 2;
}
}