diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2019-02-23 13:22:45 +0200 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2019-02-26 21:17:31 +0200 |
commit | e5ed9d9ddfaf0fe48d2bb987127b69c42ce49ae0 (patch) | |
tree | b61a4ee7ddc30554eda2afb2faa599de92914399 /src/mbgl/util/geo.cpp | |
parent | 7d407175e03c129e4361af871ed4d43f138ab337 (diff) | |
download | qtlocation-mapboxgl-e5ed9d9ddfaf0fe48d2bb987127b69c42ce49ae0.tar.gz |
[core] Use unwrapped longitude for bounds check
Diffstat (limited to 'src/mbgl/util/geo.cpp')
-rw-r--r-- | src/mbgl/util/geo.cpp | 67 |
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(), |