Mastering Algorithms With C - Kyle Loudon [222]
As an example, to compute the distance between Paris, France (49.010N, 2.548E) and Perth, Australia (31.940S, 115.967E), we begin by converting their latitudes and longitudes to spherical equivalents: (3440.065, 2.548, 40.990) for Paris and (3440.065, 115.967, 121.940) for Perth. Next, we convert the angles in each point to radians. Last, we compute the length of the arc between the points, which is 7706 nautical miles (see Figure 17.8b).
Figure 17.8. Computing the distance between (a) Paris and (b) Perth
This example presents a function, geodist (see Examples Example 17.5 and Example 17.6), that approximates the distance between two points on Earth using the method just described. The function accepts the latitude and longitude for each point as lat1 and lon1, and lat2 and lon2. It returns the distance between the points in d. After performing some initial validation of the latitudes and longitudes, geodist converts the latitude and longitude representations into spherical coordinates, stores each representation in p1 and p2 with all angles converted to radians, and calls arclen to compute the distance.
The runtime complexity of geodist is O (1) because all of the steps in computing a great-circle distance run in a constant amount of time.
Example 17.5. Header for a Function for Approximating Distances on Earth
/*****************************************************************************
* *
* ------------------------------- geodist.h ------------------------------ *
* *
*****************************************************************************/
#ifndef GEODIST_H
#define GEODIST_H
/*****************************************************************************
* *
* Define the radius of the earth in nautical miles. *
* *
*****************************************************************************/
#define EARTH_RADIUS 3440.065
/*****************************************************************************
* *
* --------------------------- Public Interface --------------------------- *
* *
*****************************************************************************/
int geodist(double lat1, double lon1, double lat2, double lon2, double *d);
#endif
Example 17.6. Implementation of a Function for Approximating Distances on Earth
/*****************************************************************************
* *
* ------------------------------- geodist.c ------------------------------ *
* *
*****************************************************************************/
#include "geodist.h"
#include "geometry.h"
/*****************************************************************************
* *
* -------------------------------- geodist
------------------------------- *
* *
*****************************************************************************/
int geodist(double lat1, double lon1, double lat2, double lon2, double *d) {
SPoint p1,
p2;
/*****************************************************************************
* *
* Validate the coordinates. *
* *
*****************************************************************************/
if (lat1 < -90.0 || lat1 > 90.0 || lat2 < -90.0 || lat2 > 90.0)
return -1;
if (lon1 < -180.0 || lon1 > 180.0 || lon2