-
Notifications
You must be signed in to change notification settings - Fork 0
/
Edge.java
37 lines (29 loc) · 1.2 KB
/
Edge.java
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
/* Project 3
* Abdul Moid Munawar, amunawar, [email protected]
* Moazzam Salman, msalman, [email protected]
*/
public class Edge {
String id;
double weight;
private static final int RADIUS_EARTH = 3959; // Approx Earth radius in MILEs
// constructor
public Edge(String id, Node o, Node d) {
this.weight = distance(o.lattitude, o.longitude, d.lattitude, d.longitude);
this.id = id;
}
// find distance between two edges (in meters)
// the following code for the Haversin Functon is taken from:
// https://github.com/jasonwinn/haversine/blob/master/Haversine.java
private static double haversin(double val) {
return Math.pow(Math.sin(val / 2), 2);
}
private static double distance(double sLattitude, double sLongitude, double eLattitude, double eLongitude) {
double disLat = Math.toRadians((eLattitude - sLattitude));
double disLong = Math.toRadians((eLongitude - sLongitude));
sLattitude = Math.toRadians(sLattitude);
eLattitude = Math.toRadians(eLattitude);
double a = haversin(disLat) + Math.cos(sLattitude) * Math.cos(eLattitude) * haversin(disLong);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return RADIUS_EARTH * c; // returns distance in miles
}
}