If I have two GPS locations, say 51.507222, -0.1275 and 48.856667, 2.350833, what formula could I use to calculate the distance between the two? I've heard a lot about a haversine formula, but can't find any information about it, or how to apply it to C.
I've written the following code, however, it's very innacurate. Anybody know why? I can't figure it out. The problem comes from the function itself, but I don't know what it is.
float calcDistance(float A, float B, float C, float D)
{
float dLat;
float dLon;
dLat = (C - A);
dLon = (D - B);
dLat /= 57.29577951;
dLon /= 57.29577951;
float v_a;
float v_c;
float distance;
v_a = sin(dLat/2) * sin(dLat/2) + cos(A) * cos(C) * sin(dLon/2) * sin(dLon/2);
v_c = 2 * atan2(sqrt(v_a),sqrt(1-v_a));
distance = r * v_c;
return distance;
}
This page has a block of Javascript which I'm sure you could adapt easily enough in C:
var R = 6371e3; // metres
var φ1 = lat1.toRadians();
var φ2 = lat2.toRadians();
var Δφ = (lat2-lat1).toRadians();
var Δλ = (lon2-lon1).toRadians();
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;