/**
*Calculate the distance (in nautical miles) between two geographical points
*by using the law of cosines (pass values as decimal degrees).
*East Longitude is +, West Longitude is -
*North Latitude is +, South Latitude is -
*Example:<code>
*Dim myDistance As Double
*myDistance = mymath.locGreatCircleNm(59.9, -30.3, 37.8, 122.4)</code>
*/
public double locGreatCircleNm(double lat1, double long1, double lat2, double long2) {
double x1 = Math.toRadians(lat1);
double y1 = Math.toRadians(long1);
double x2 = Math.toRadians(lat2);
double y2 = Math.toRadians(long2);
/*************************************************************************
* Compute using law of cosines
*************************************************************************/
// great circle distance in radians
double angle1 = Math.acos(Math.sin(x1) * Math.sin(x2)
+ Math.cos(x1) * Math.cos(x2) * Math.cos(y1 - y2));
// convert back to degrees
angle1 = Math.toDegrees(angle1);
// each degree on a great circle of Earth is 60 nautical miles
double distance1 = 60 * angle1;
return distance1;
}
/**
*Calculate the distance (in nautical miles) between two geographical points
*by using the Haversine Formula (pass values as decimal degrees).
*East Longitude is +, West Longitude is -
*North Latitude is +, South Latitude is -
*Example:<code>
*Dim myDistance As Double
*myDistance = mymath.hsGreatCircleNm(40.35, 74.65, 48.87, -2.33)</code>
*/
public double hsGreatCircleNm(double lat1, double long1, double lat2, double long2) {
double x1 = Math.toRadians(lat1);
double y1 = Math.toRadians(long1);
double x2 = Math.toRadians(lat2);
double y2 = Math.toRadians(long2);
/*************************************************************************
* Compute using Haversine formula
*************************************************************************/
double a = Math.pow(Math.sin((x2-x1)/2), 2)
+ Math.cos(x1) * Math.cos(x2) * Math.pow(Math.sin((y2-y1)/2), 2);
// great circle distance in radians
double angle2 = 2 * Math.asin(Math.min(1, Math.sqrt(a)));
// convert back to degrees
angle2 = Math.toDegrees(angle2);
// each degree on a great circle of Earth is 60 nautical miles
double distance2 = 60 * angle2;
return distance2;
}