Created
May 11, 2015 04:20
-
-
Save ixqbar/c72b9c0d1a3edf3730f9 to your computer and use it in GitHub Desktop.
两点距离
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <math.h> | |
#ifndef M_PI | |
#define M_PI 3.14159265358979323846264338327950288 | |
#endif | |
#define D_R (M_PI / 180.0) | |
const double EARTH_RADIUS_IN_METERS = 6372797.560856; | |
static inline double deg_rad(double ang) { | |
return ang * D_R; | |
} | |
double geoDistanceEarth(double lat1d, double lon1d, double lat2d, double lon2d) { | |
double lat1r, lon1r, lat2r, lon2r, u, v; | |
lat1r = deg_rad(lat1d); | |
lon1r = deg_rad(lon1d); | |
lat2r = deg_rad(lat2d); | |
lon2r = deg_rad(lon2d); | |
u = sin((lat2r - lat1r) / 2); | |
v = sin((lon2r - lon1r) / 2); | |
return 2.0 * EARTH_RADIUS_IN_METERS * asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment