Skip to content

Instantly share code, notes, and snippets.

@psycharo-zz
Created August 20, 2012 13:02
Show Gist options
  • Save psycharo-zz/3403849 to your computer and use it in GitHub Desktop.
Save psycharo-zz/3403849 to your computer and use it in GitHub Desktop.
//! Earth radius
static const double EARTH_RADIUS = 6371400;
//! convert degrees to radians
double toRad(double degrees)
{
return M_PI / 180 * degrees;
}
//! convert radians to degrees
double toDeg(double rads)
{
return 180 / M_PI * rads;
}
//! get distance between two points in lat/lon format (in degrees)
double distance(double lat1, double lon1,
double lat2, double lon2)
{
double dLat = toRad(lat2 - lat1);
double dLon = toRad(lon2 - lon1);
lat1 = toRad(lat1);
lat2 = toRad(lat2);
dLat = sin(0.5 * dLat);
dLon = sin(0.5 * dLon);
double a = dLat * dLat + dLon * dLon * cos(lat1) * cos(lat2);
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
return EARTH_RADIUS * c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment