Created
March 5, 2019 03:05
-
-
Save obastemur/dae895b21a48ee76766fa07078decc64 to your computer and use it in GitHub Desktop.
Calculate distance point from long/lat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#define RAD(a) ((a) * (a) * M_PI) | |
double calculateDistance(double lat1, double lon1, double lat2, double lon2) { | |
double R = 6371; | |
double dLat = RAD(lat2-lat1); | |
double dLon = RAD(lon2-lon1); | |
double aX = sin(dLat/2) * sin(dLat/2) + | |
cos(RAD(lat1)) * cos(RAD(lat2)) * | |
sin(dLon/2) * sin(dLon/2); | |
double c = 2 * atan2(sqrt(aX), sqrt(1 - aX)); | |
return R * c; | |
} | |
int main() { | |
double startLat = 44.957; | |
double startLong = -93.284; | |
double endLat = 45.000; | |
double endLong = -93.284; | |
printf("TEST %f\r\n", calculateDistance(startLat, startLong, endLat, endLong)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment