#include "cache.h" #include #include const static int Earth_radius = 6378; Position Cache::home; static float degtorad(float x) { return x * M_PI / 180; } void Cache::show() const { std::cout << "Cache:\t" << code << ' ' << name << " (type: " << type << ", owned by " << owner << ")\n"; std::cout << '\t' << pos.lat << " " << pos.lon << "\t\tD/T: " << diff << '/' << terr << "\t\tFound on " << date() << '\n'; } std::string Cache::link() const { return "http://coord.eu/" + code; } float Cache::distance() const { return 2 * Earth_radius * asin(sqrt(pow(sin(degtorad((pos.lat - home.lat) / 2)), 2) + cos(degtorad(pos.lat)) * cos(degtorad(home.lat)) * pow(sin(degtorad((pos.lon - home.lon) / 2)), 2))); } std::string Cache::date() const { return std::to_string(date_found.tm_year+1900) + '-' + std::to_string(date_found.tm_mon) + "-" + std::to_string(date_found.tm_mday); } bool CacheCmpNS(const Cache& lhs, const Cache& rhs) { return lhs.pos.lat < rhs.pos.lat; } bool CacheCmpEW(const Cache& lhs, const Cache& rhs) { return lhs.pos.lon < rhs.pos.lon; } bool CacheCmpDist(const Cache& lhs, const Cache& rhs) { return lhs.distance() < rhs.distance(); }