diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-02-02 15:04:21 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-02-02 23:04:18 -0800 |
commit | 37c27f3a8f55ae74d7da9b5f45fa577de3af7f21 (patch) | |
tree | 88d479ad1d4a3559844c3fd8c82ab4a33c4bc55b /include | |
parent | 032c8fba3c8e3c122dd399b5c9341d92ad9d286f (diff) | |
download | qtlocation-mapboxgl-37c27f3a8f55ae74d7da9b5f45fa577de3af7f21.tar.gz |
[core] Improve LatLngBounds API
* Use "named constructors": empty, world, hull
* Make the two-argument constructor lenient (i.e., it is a hull operation)
* Add various accessors
* Enforce a single empty representation
Diffstat (limited to 'include')
-rw-r--r-- | include/mbgl/util/geo.hpp | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/include/mbgl/util/geo.hpp b/include/mbgl/util/geo.hpp index 643b82f890..0f6a049962 100644 --- a/include/mbgl/util/geo.hpp +++ b/include/mbgl/util/geo.hpp @@ -44,21 +44,47 @@ public: class LatLngBounds { public: - LatLng sw = {-90, -180}; - LatLng ne = {90, 180}; + // Return a bounds covering the entire (unwrapped) world. + static LatLngBounds world() { + return LatLngBounds({-90, -180}, {90, 180}); + } - LatLngBounds() {} + // Return the bounds consisting of the single point. + static LatLngBounds singleton(const LatLng& a) { + return LatLngBounds(a, a); + } - LatLngBounds(const LatLng& sw_, const LatLng& ne_) - : sw(sw_), ne(ne_) {} + // Return the convex hull of two points; the smallest bounds that contains both. + static LatLngBounds hull(const LatLng& a, const LatLng& b) { + LatLngBounds bounds(a, a); + bounds.extend(b); + return bounds; + } - static LatLngBounds getExtendable() { - LatLngBounds bounds; - return { bounds.ne, bounds.sw }; + // Return a bounds that may serve as the identity element for the extend operation. + static LatLngBounds empty() { + LatLngBounds bounds = world(); + std::swap(bounds.sw, bounds.ne); + return bounds; } // Constructs a LatLngBounds object with the tile's exact boundaries. - LatLngBounds(const TileID& id); + LatLngBounds(const TileID&); + + double south() const { return sw.latitude; } + double west() const { return sw.longitude; } + double north() const { return ne.latitude; } + double east() const { return ne.longitude; } + + LatLng southwest() const { return sw; } + LatLng northeast() const { return ne; } + LatLng southeast() const { return LatLng(south(), east()); } + LatLng northwest() const { return LatLng(north(), west()); } + + LatLng center() const { + return LatLng((sw.latitude + ne.latitude) / 2, + (sw.longitude + ne.longitude) / 2); + } void extend(const LatLng& point) { if (point.latitude < sw.latitude) sw.latitude = point.latitude; @@ -72,6 +98,11 @@ public: extend(bounds.ne); } + bool isEmpty() const { + return sw.latitude > ne.latitude || + sw.longitude > ne.longitude; + } + bool contains(const LatLng& point) const { return (point.latitude >= sw.latitude && point.latitude <= ne.latitude && @@ -85,6 +116,13 @@ public: area.ne.longitude > sw.longitude && area.sw.longitude < ne.longitude); } + +private: + LatLng sw; + LatLng ne; + + LatLngBounds(const LatLng& sw_, const LatLng& ne_) + : sw(sw_), ne(ne_) {} }; class MetersBounds { |