Skip to content

Instantly share code, notes, and snippets.

@obastemur
Created March 5, 2019 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obastemur/dae895b21a48ee76766fa07078decc64 to your computer and use it in GitHub Desktop.
Save obastemur/dae895b21a48ee76766fa07078decc64 to your computer and use it in GitHub Desktop.
Calculate distance point from long/lat
#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