Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Added LatLngBounds::{valid,constrain}
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Apr 4, 2017
1 parent 5871fce commit 694a12f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/mbgl/util/geo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ class LatLngBounds {
// Constructs a LatLngBounds object with the tile's exact boundaries.
LatLngBounds(const CanonicalTileID&);

bool valid() const {
return (sw.latitude <= ne.latitude) && (sw.longitude <= ne.longitude);
}

double south() const { return sw.latitude; }
double west() const { return sw.longitude; }
double north() const { return ne.latitude; }
Expand All @@ -126,6 +130,18 @@ class LatLngBounds {
(sw.longitude + ne.longitude) / 2);
}

LatLng constrain(const LatLng& p) const {
if (contains(p)) {
return p;
}
return LatLng {
p.latitude < sw.latitude ? sw.latitude
: (p.latitude > ne.latitude ? ne.latitude: p.latitude),
p.longitude < sw.longitude ? sw.longitude
: (p.longitude > ne.longitude ? ne.longitude : p.longitude),
};
}

void extend(const LatLng& point) {
if (point.latitude < sw.latitude) sw.latitude = point.latitude;
if (point.latitude > ne.latitude) ne.latitude = point.latitude;
Expand Down

0 comments on commit 694a12f

Please sign in to comment.