summaryrefslogtreecommitdiff
path: root/src/mbgl/util/geo.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mbgl/util/geo.cpp')
-rw-r--r--src/mbgl/util/geo.cpp67
1 files changed, 41 insertions, 26 deletions
diff --git a/src/mbgl/util/geo.cpp b/src/mbgl/util/geo.cpp
index a04e2c5355..19510e2039 100644
--- a/src/mbgl/util/geo.cpp
+++ b/src/mbgl/util/geo.cpp
@@ -39,35 +39,12 @@ bool LatLngBounds::contains(const CanonicalTileID& tileID) const {
}
bool LatLngBounds::contains(const LatLng& point, LatLng::WrapMode wrap /*= LatLng::Unwrapped*/) const {
- bool containsLatitude = point.latitude() >= sw.latitude() &&
- point.latitude() <= ne.latitude();
- if (!containsLatitude) {
- return false;
- }
-
- bool containsUnwrappedLongitude = point.longitude() >= sw.longitude() &&
- point.longitude() <= ne.longitude();
- if (containsUnwrappedLongitude) {
- return true;
- } else if (wrap == LatLng::Wrapped) {
- LatLngBounds wrapped(sw.wrapped(), ne.wrapped());
- auto ptLon = point.wrapped().longitude();
- if (crossesAntimeridian()) {
- return (ptLon >= wrapped.sw.longitude() &&
- ptLon <= util::LONGITUDE_MAX) ||
- (ptLon <= wrapped.ne.longitude() &&
- ptLon >= -util::LONGITUDE_MAX);
- } else {
- return (ptLon >= wrapped.sw.longitude() &&
- ptLon <= wrapped.ne.longitude());
- }
- }
- return false;
+ return containsLatitude(point.latitude()) && containsLongitude(point.longitude(), wrap);
}
bool LatLngBounds::contains(const LatLngBounds& area, LatLng::WrapMode wrap /*= LatLng::Unwrapped*/) const {
- bool containsLatitude = area.north() <= north() && area.south() >= south();
- if (!containsLatitude) {
+ bool containsAreaLatitude = area.north() <= north() && area.south() >= south();
+ if (!containsAreaLatitude) {
return false;
}
@@ -114,6 +91,44 @@ bool LatLngBounds::intersects(const LatLngBounds area, LatLng::WrapMode wrap /*=
return false;
}
+LatLng LatLngBounds::constrain(const LatLng& p) const {
+ double lat = p.latitude();
+ double lng = p.longitude();
+
+ if (!containsLatitude(lat)) {
+ lat = util::clamp(lat, south(), north());
+ }
+
+ if (!containsLongitude(lng, LatLng::Unwrapped)) {
+ lng = util::clamp(lng, west(), east());
+ }
+
+ return LatLng { lat, lng };
+}
+
+bool LatLngBounds::containsLatitude(double latitude) const {
+ return latitude >= south() && latitude <= north();
+}
+
+bool LatLngBounds::containsLongitude(double longitude, LatLng::WrapMode wrap) const {
+ if (longitude >= west() && longitude <= east()) {
+ return true;
+ }
+
+ if (wrap == LatLng::Wrapped) {
+ LatLngBounds wrapped(sw.wrapped(), ne.wrapped());
+ longitude = LatLng(0.0, longitude).wrapped().longitude();
+ if (crossesAntimeridian()) {
+ return (longitude >= -util::LONGITUDE_MAX && longitude <= wrapped.east()) ||
+ (longitude >= wrapped.west() && longitude <= util::LONGITUDE_MAX);
+ }
+
+ return longitude >= wrapped.west() && longitude <= wrapped.east();
+ }
+
+ return false;
+}
+
ScreenCoordinate EdgeInsets::getCenter(uint16_t width, uint16_t height) const {
return {
(width - left() - right()) / 2.0 + left(),