My program calculates the distance between two points in kilometres, given two latitude and longitude values.
#include<iostream>
#include <cmath>
#define pi 3.14159265358979323846
using namespace std;
double calculateDistance(double lat1, double long1, double lat2, double long2) {
double dist;
dist = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(long1 - long2);
dist = acos(dist);
dist = (6371 * pi * dist) / 180;
return dist;
}
int main() {
cout << calculateDistance(51.752021, -1.257726, 51.507351, -0.127758);
return 0;
}
This is the formula I am trying to implement:
And the calculator I am using to test my output, http://www.movable-type.co.uk/scripts/latlong.html which states the answer should be 82.60km. (I am getting 33.6227km)