Skip to content

Instantly share code, notes, and snippets.

@adventurist
Created April 4, 2020 18:26
Show Gist options
  • Save adventurist/1f08539bb4421859d402e749148676d9 to your computer and use it in GitHub Desktop.
Save adventurist/1f08539bb4421859d402e749148676d9 to your computer and use it in GitHub Desktop.
GeoCoords
#include <cstdio>
#include <type_traits>
#include <utility>
#define First(x) std::get<0>(x)
#define Second(x) std::get<1>(x)
template <typename T>
class GeoCoordinate {
typedef std::pair<T, T> CoordPair;
public:
typedef T DegreeValue;
typedef DegreeValue Longitude;
typedef DegreeValue Latitude;
template <typename CType>
struct Coordinate {
DegreeValue degree;
T limit;
Coordinate(T value) : degree(value) {
if constexpr (std::is_same_v<CType, Longitude>) {
limit = 180;
} else if constexpr (std::is_same_v<CType, Latitude>) {
limit = 90;
}
}
Coordinate operator+(const Coordinate& other) {
auto long_this = *this;
auto l1 = long_this.degree;
auto l2 = other.degree;
CoordPair p {l1, l2};
if ((First(p) + Second(p)) > limit) {
auto diff = limit - First(p);
auto remain = Second(p) - diff;
return (Coordinate(0 - remain));
}
return Coordinate(First(p) + Second(p));
}
Coordinate operator-(const Coordinate& other) {
auto long_this = *this;
auto l1 = long_this.degree;
auto l2 = other.degree;
CoordPair p{l1, l2};
if ((First(p) - Second(p)) < -limit) {
auto diff = -limit - First(p);
auto remain = diff - Second(p);
return Coordinate(0 - remain);
}
return Coordinate(First(p) - Second(p));
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment