-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo.cpp
42 lines (32 loc) · 1.14 KB
/
geo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#define _USE_MATH_DEFINES
#include <cmath>
#include "geo.hpp"
#include <sstream>
#include <stdexcept>
string geocoord2string(double angle) {
if ( (angle > 180) || (angle < -180) )
throw invalid_argument("Invalid argument : angle must be beetween -180° and 180°");
int deg = int(floor(angle));
double rest = angle - deg;
int minute = int(floor( rest * 60));
rest = rest * 60 - minute;
int second = int(floor( rest * 60));
ostringstream result;
result << deg << "° " << minute << "' " << second <<"''";
return result.str();
}
double deg2rad(double deg) {
return (deg * M_PI / 180);
}
GeoPoint::GeoPoint(double lat, double lng) : lat(lat), lng(lng)
{}
double GeoPoint::distance(const GeoPoint &other) {
double nDLat = deg2rad(other.lat - this->lat);
double nDLon = deg2rad(other.lng - this->lng);
double thisLatRad = deg2rad(this->lat);
double otherLatRad = deg2rad(other.lat);
double nA = pow ( sin(nDLat/2), 2 ) + cos(thisLatRad) * cos(otherLatRad) * pow ( sin(nDLon/2), 2 );
double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
double nD = EARTH_RADIUS * nC;
return nD; // Return our calculated distance
}