0

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:

enter image description here

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)

  • 1
    You should convert degrees to radians first. – Gábor Bakos Nov 25 '14 at 12:28
  • "where d is expressed in degrees" – XCS Nov 25 '14 at 12:30
  • 1
    possible duplicate of [How do I calculate distance between two latitude-longitude points?](http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points) – XCS Nov 25 '14 at 12:32
  • Your question is insufficiently defined. There are many ways to define the distance between two points on the Earth, depending on ---what approximation you use for the Earth's shape and-- what co-ordinate system you are using. – Lightness Races in Orbit Nov 25 '14 at 12:37
  • @LightnessRacesinOrbit From the examples I think the Earth's shape was approximated by a sphere. The longitudes, latitudes I think define a unique (generally accepted) coordinate system (with degrees). The article also mentioned the assumption of plain surface. – Gábor Bakos Nov 25 '14 at 13:06
  • @GáborBakos: Yes I "struck through" that part of my comment when I spotted the sphere approximation text :P The co-ordinate system still needs to be defined. – Lightness Races in Orbit Nov 25 '14 at 13:16

3 Answers3

2

You could try this, for me its working and more precisely. I'm using the Haversine formula for this.

#include <iostream>
#include <math.h>
#include <algorithm>

#define PI 3.14159265358979323846
#define RADIO_TERRESTRE 6372797.56085
#define GRADOS_RADIANES PI / 180
using namespace std;

float CalcGPSDistance(float latitud1, float longitud1, float latitud2, float longitud2){
    double haversine;
    double temp;
    double distancia_puntos;

    latitud1  = latitud1  * GRADOS_RADIANES;
    longitud1 = longitud1 * GRADOS_RADIANES;
    latitud2  = latitud2  * GRADOS_RADIANES;
    longitud2 = longitud2 * GRADOS_RADIANES;

    haversine = (pow(sin((1.0 / 2) * (latitud2 - latitud1)), 2)) + ((cos(latitud1)) * (cos(latitud2)) * (pow(sin((1.0 / 2) * (longitud2 - longitud1)), 2)));
    temp = 2 * asin(min(1.0, sqrt(haversine)));
    distancia_puntos = RADIO_TERRESTRE * temp;

   return distancia_puntos;
}

int main(){

    cout << CalcGPSDistance(37.824574,-2.533276,37.821897,-2.537297)<< endl;

    return 0;
}
1

Something like this should work (not tested though):

double toRad(double degree) {
    return degree/180 * pi;
}

double calculateDistance(double lat1, double long1, double lat2, double long2) {
    double dist;
    dist = sin(toRad(lat1)) * sin(toRad(lat2)) + cos(toRad(lat1)) * cos(toRad(lat2)) * cos(toRad(long1 - long2));
    dist = acos(dist);
//        dist = (6371 * pi * dist) / 180;
    //got dist in radian, no need to change back to degree and convert to rad again.
    dist = 6371 * dist;
    return dist;
}
Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52
0

Work & Tested

Returns in meters. latitude & longitude are in degrees.

    #define PI 3.14159265358979323846
    #define RADIO_TERRESTRE 6372797.56085
    #define GRADOS_RADIANES PI / 180
    #define RADIANES_GRADOS 180 / PI
        
    double calcGPSDistance(double latitude_new, double longitude_new, double latitude_old, double longitude_old)
{
    double  lat_new = latitude_old * GRADOS_RADIANES;
    double  lat_old = latitude_new * GRADOS_RADIANES;
    double  lat_diff = (latitude_new-latitude_old) * GRADOS_RADIANES;
    double  lng_diff = (longitude_new-longitude_old) * GRADOS_RADIANES;

    double  a = sin(lat_diff/2) * sin(lat_diff/2) +
                cos(lat_new) * cos(lat_old) *
                sin(lng_diff/2) * sin(lng_diff/2);
    double  c = 2 * atan2(sqrt(a), sqrt(1-a));

    double  distance = RADIO_TERRESTRE * c;
    
    // std::cout <<__FILE__ << "." << __FUNCTION__ << " line:" << __LINE__ << "  "
    
    return distance;
M.Hefny
  • 2,677
  • 1
  • 26
  • 30