-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatlonbounds.cpp
73 lines (56 loc) · 1.47 KB
/
latlonbounds.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
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
#include "infra/latlonbounds.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
namespace inf {
bool LatLonBounds::contains(const LatLonBounds& area) const
{
bool containsAreaLatitude = area.north() <= north() && area.south() >= south();
if (!containsAreaLatitude) {
return false;
}
bool containsUnwrapped = area.east() <= east() && area.west() >= west();
if (containsUnwrapped) {
return true;
}
return false;
}
bool LatLonBounds::intersects(const LatLonBounds area) const
{
bool latitudeIntersects = area.north() > south() && area.south() < north();
if (!latitudeIntersects) {
return false;
}
bool longitudeIntersects = area.east() > west() && area.west() < east();
if (longitudeIntersects) {
return true;
}
return false;
}
Coordinate LatLonBounds::constrain(const Coordinate& p) const
{
if (!bounded) {
return p;
}
double lat = p.latitude;
double lng = p.longitude;
if (!contains_latitude(lat)) {
lat = std::clamp(lat, south(), north());
}
if (!contains_longitude(lng)) {
lng = std::clamp(lng, west(), east());
}
return Coordinate{lat, lng};
}
bool LatLonBounds::contains_latitude(double latitude) const
{
return latitude >= south() && latitude <= north();
}
bool LatLonBounds::contains_longitude(double longitude) const
{
if (longitude >= west() && longitude <= east()) {
return true;
}
return false;
}
}