Online Book Reader

Home Category

Mastering Algorithms With C - Kyle Loudon [222]

By Root 1533 0
(r, θ, φ). In terms of the earth, r is the distance along an imaginary line from the earth's center to a point on its surface; that is, r is the earth's radius, which is 3440.065 nautical miles. The coordinate θ is the angle the point forms with the prime meridian. Thus, θ corresponds to longitude. However, since positive longitudes are to the west and positive values of θ are the opposite direction, to obtain θ from degrees longitude, we reverse the sign of the longitude. The coordinate φ is the angle a point forms with an imaginary line extending vertically from the center of the earth to the north pole. Thus, φ corresponds to latitude. However, since latitudes are relative to the equator and not the north pole, to obtain φ from degrees latitude, we reverse the sign of the latitude and add 90 degrees.

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

Return Main Page Previous Page Next Page

®Online Book Reader