-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocation.h
75 lines (58 loc) · 1.97 KB
/
Location.h
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Copyright (C) 2015 Miguel Rodríguez Pérez <miguel@det.uvigo.es>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LOCATION_H
#define LOCATION_H
#include <cmath>
#include <cassert>
#include <ostream>
#include <algorithm>
class Location {
public:
using value_type = double;
Location(value_type lat, value_type lon) : lat_(lat), lon_(lon) {
assert(lat >= 0);
assert(lon >= 0);
}
auto getLatitude() const {
return lat_;
}
auto getLongitude() const {
return lon_;
}
auto getSquaredDistanceTo(const Location& origin) const {
return (lat_ - origin.lat_)*(lat_ - origin.lat_) + (lon_ - origin.lon_)*(lon_ - origin.lon_);
}
auto getDistanceTo(const Location& origin) const {
return std::sqrt(getSquaredDistanceTo(origin));
}
auto getDistanceToOrigin() const {
return getDistanceTo(Location(0, 0));
}
auto operator+(const Location& b) const {
return Location(lat_ + b.lat_, lon_ + b.lon_);
}
auto operator-(const Location& b) const {
return Location(std::max(0., lat_ - b.lat_), std::max(0., lon_ - b.lon_));
}
auto operator==(const Location& b) const {
return lat_ == b.lat_ && lon_ == b.lon_;
}
private:
value_type lat_, lon_;
};
std::ostream& operator<<(std::ostream& os, const Location& loc);
#endif /* LOCATION_H */