Game Development Reference
In-Depth Information
than the spherical law of cosines while remaining much simpler than the Vincenty
formula.
The haversine formula for distance is:
d = (R)(c)
where R = earth's radius and c is the angular distance in radians given by:
c = 2 arcsin ( a )
Here, a is the square of half the chord length between the two points, calculated as:
a = sin 2 lat /2) + cos(lat 1 )cos(lat 2 )sin 2 long /2)
Finally:
Δ long = long 2 - long 1
Δ lat = lat 2 - lat 1
Remember to first convert the angles to radians before using them in the trig function.
Next we will begin showing you an implementation of several different formulas in
Objective-C; however, these should translate to C with little modification. These all use
the following data structure to hold latitude and longitude information:
typedef enum {
float lat;
float lon;
} Coordinate2D;
Given this, the haversine implementation would look like:
float distanceGreatCircle(Coordinate2D startPoint, Coordinate2D endPoint){
//Convert location from degrees to radians
float lat1 = (M_PI/180.) * startPoint.lat;
float lon1 = (M_PI/180.) * endPoint.longi;
float lat2 = (M_PI/180.) * endPoint.lat;
float lon2 = (M_PI/180.) * endPoint.longi;
//Calculate deltas
float dLat = lat2 - lat1;
float dLon = lon2 - lon1;
//Calculate half chord legnth
float a = sin(dLat/2) * sin(dLat/2) + cos(lat1) * cos(lat2) * sin(dLon/2)
* sin(dLon/2);
Search Nedrilad ::




Custom Search