Skip to content

Instantly share code, notes, and snippets.

@dharanad
Last active February 12, 2018 18:49
Show Gist options
  • Save dharanad/56e06e97c3d7e85082d61c757f0986b0 to your computer and use it in GitHub Desktop.
Save dharanad/56e06e97c3d7e85082d61c757f0986b0 to your computer and use it in GitHub Desktop.
Great Circle Distance
#include <math.h>
#include <stdio.h>
/**
* @brief haversineDistance finds the
* [great-circle distance](https://en.wikipedia.org/wiki/Great-circle_distance)
* between two points on a sphere.
* @see https://en.wikipedia.org/wiki/Haversine_formula.
*
* Parameters are the latitude and longitude of the first and second point in
* radians, respectively.
*
* @return the great-circle distance in kilometers.
*/
double haversineDistance(double th1, double ph1, double th2, double ph2) {
double dx, dy, dz;
ph1 -= ph2;
dz = sin(th1) - sin(th2);
dx = cos(ph1) * cos(th1) - cos(th2);
dy = sin(ph1) * cos(th1);
return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R;
}
double to_radians(double degree) {
return (degree * M_PI) / 180.0;
}
int main(int argc, char **argc){
return 0;
}
@dharanad
Copy link
Author

Great Circle Distance formula, it is used to find distance between two point in a sphere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment