Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Calculates distance between two sets of Decimal Degree coordinates using the Haversine formula
- //Accepts two sets of longitudes and latitudes in decimal degree format and calculates distance between
- //in meters.
- signed long get_distance(signed long lat1, signed long lat2, signed long lng1, signed long lng2){
- signed long radius = 6371000; //Approximate radius of the Earth in meters
- double latDif, lngDif, radLat1, radLat2, a, c, d;
- //It should be able to handle negatives, but in case it can't...
- // if(lat1 < 0)
- // lat1 *= -1;
- //
- // if(lat2 < 0)
- // lat2 *= -1;
- //
- // if(lng1 < 0)
- // lng1 *= -1;
- //
- // if(lng2 < 0)
- // lng2 *= -1;
- //Dividing breaks everything
- // lat1 /= 10000000;
- // lat2 /= 10000000;
- // lng1 /= 10000000;
- // lng2 /= 10000000;
- //Convert from degrees to radians
- radLat1 = lat1 * M_PI / 180;
- radLat2 = lat2 * M_PI / 180;
- latDif = (lat2 - lat1) * M_PI / 180;
- lngDif = (lng2 - lng1) * M_PI / 180;
- //Apply the Haversine formula
- a = sin(latDif/2) * sin(latDif/2) + cos(radLat1) * cos(radLat2) * sin(lngDif/2) * sin(lngDif/2);
- c = 2 * atan2(sqrt(a), sqrt(1-a));
- d = radius * c;
- return (long)d;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement