#include #include "geography.h" #include "more_math.h" // changed constants to preprocessor variables (b/c initializers of global // constants can't refer to other variables, whether they're constant or not) #define NORTH_POLE_LATITUDE (PI / 2) #define EARTH_RADIUS_MILES 3959.0 /** * Computes the distance of the great circle route between two points. * The latitudes and longitudes should be in degrees and the distance * is computes assuming that the Earth is spherical. * * @param lat1 a double between -90 and +90 * @param lon1 a double between -180 and 180 * @param lat2 a double between -90 and +90 * @param lon2 a double between -180 and 180 * @return the distance between (lat1, lon1) and (lat2, lon2) */ double distance(const location *from, const location *to) { // convert latitudes to radians and get colatitude double colat1 = colatitude(to_radians(from->lat)); double colat2 = colatitude(to_radians(to->lat)); // compute difference in longitudes in radians double delta_lon = to_radians(from->lon - to->lon); // use spherical law of cosines to compute angle double x = acos(cos(colat1) * cos(colat2) + sin(colat1) * sin(colat2) * cos(delta_lon)); // convert radians to arc length by multiplying by radius return x * EARTH_RADIUS_MILES; } /** * Returns the colatitude of the given latitude in degrees. The given latitude * should be in degrees. * * @param lat a double between -90 and +90 * @return the colatitude of lat, in degrees */ double colatitude(double lat) { return NORTH_POLE_LATITUDE - lat; }