31 lines
656 B
C++
31 lines
656 B
C++
#include "haversine.h"
|
|
#include "aliases.h"
|
|
#include "point_types.h"
|
|
#include <math.h>
|
|
|
|
#define PI 3.14159265358979323845
|
|
#define SQUARE(X) ((X) * (X))
|
|
|
|
f64 radians(f64 degrees);
|
|
|
|
f64 haversine_of_degrees(const PointPair &pair, f64 radius) {
|
|
f64 x0 = pair.p1.x;
|
|
f64 y0 = pair.p1.y;
|
|
f64 x1 = pair.p2.x;
|
|
f64 y1 = pair.p2.y;
|
|
|
|
f64 dy = radians(y1 - y0);
|
|
f64 dx = radians(x1 - x0);
|
|
y0 = radians(y0);
|
|
y1 = radians(y1);
|
|
|
|
f64 root_term =
|
|
SQUARE(sin(dy / 2.0)) + cos(y0) * cos(y1) * SQUARE(sin(dx / 2.0));
|
|
|
|
f64 result = 2.0 * radius * asin(sqrt(root_term));
|
|
|
|
return result;
|
|
}
|
|
|
|
f64 radians(f64 degrees) { return (degrees * PI) / 180.0; }
|