Shapes
GIS made easy, a lightweight header-only planar geometry library for Modern C++
bounds.hpp
1 #pragma once
2 
3 #include <ciso646>
4 #include <algorithm>
5 #include <tuple>
6 
7 namespace simo
8 {
9 namespace shapes
10 {
11 
18 struct bounds_t
19 {
21  double minx;
22 
24  double miny;
25 
27  double maxx;
28 
30  double maxy;
31 
38  : minx(std::numeric_limits<double>::max()),
39  miny(std::numeric_limits<double>::max()),
40  maxx(std::numeric_limits<double>::min()),
41  maxy(std::numeric_limits<double>::min())
42  {
43  }
44 
55  bounds_t(double minx, double miny, double maxx, double maxy)
56  : minx(minx), miny(miny), maxx(maxx), maxy(maxy)
57  {
58  }
59 
69  bounds_t& extend(double x, double y)
70  {
71  minx = std::min(x, minx);
72  maxx = std::max(x, maxx);
73  miny = std::min(y, miny);
74  maxy = std::max(y, maxy);
75  return *this;
76  }
77 
88  {
89  minx = std::min(b.minx, minx);
90  maxx = std::max(b.maxx, maxx);
91  miny = std::min(b.miny, miny);
92  maxy = std::max(b.maxy, maxy);
93  return *this;
94  }
95 
101  std::tuple<double, double> center() const
102  {
103  return std::make_tuple((minx + maxx) / 2.0, (miny + maxy) / 2.0);
104  }
105 
111  std::tuple<double, double> bottom_left() const
112  {
113  return std::make_tuple(minx, maxy);
114  }
115 
121  std::tuple<double, double> top_right() const
122  {
123  return std::make_tuple(maxx, miny);
124  }
125 
131  std::tuple<double, double> top_left() const
132  {
133  return std::make_tuple(minx, miny);
134  }
135 
141  std::tuple<double, double> bottom_right() const
142  {
143  return std::make_tuple(maxx, maxy);
144  }
145 
153  bool contains(double x, double y) const
154  {
155  return (x >= minx) && (x <= maxx) && (y >= miny) && (y <= maxy);
156  }
157 
164  bool contains(const bounds_t& other)
165  {
166  return contains(other.minx, other.miny) && contains(other.maxx, other.maxy);
167  }
168 
175  bool intersects(const bounds_t& other)
176  {
177  return (other.maxx >= minx) && (other.minx <= maxx) && (other.maxy >= miny) && (other.miny <= maxy);
178  }
179 
186  bool overlaps(const bounds_t& other)
187  {
188  return (other.maxx > minx) && (other.minx < maxx) && (other.maxy > miny) && (other.miny < maxy);
189  }
190 };
191 
192 } // namespace shapes
193 } // namespace simo
double minx
the minimum x-coordinate
Definition: bounds.hpp:21
double miny
the minimum y-coordinate
Definition: bounds.hpp:24
bool overlaps(const bounds_t &other)
Definition: bounds.hpp:186
std::tuple< double, double > bottom_left() const
Definition: bounds.hpp:111
bool contains(const bounds_t &other)
Definition: bounds.hpp:164
Represents an axis-aligned bounding box.
Definition: bounds.hpp:18
std::tuple< double, double > top_left() const
Definition: bounds.hpp:131
bounds_t & extend(double x, double y)
Extends the bounds to contain the given point.
Definition: bounds.hpp:69
std::tuple< double, double > top_right() const
Definition: bounds.hpp:121
bool contains(double x, double y) const
Definition: bounds.hpp:153
bounds_t(double minx, double miny, double maxx, double maxy)
Creates a bounds object from the given coordinates.
Definition: bounds.hpp:55
double maxx
the maximum x-coordinate
Definition: bounds.hpp:27
bool intersects(const bounds_t &other)
Definition: bounds.hpp:175
std::tuple< double, double > center() const
Definition: bounds.hpp:101
bounds_t()
Creates a bounds object.
Definition: bounds.hpp:37
std::tuple< double, double > bottom_right() const
Definition: bounds.hpp:141
bounds_t & extend(const bounds_t &b)
Extends the bounds to contain the given bounds.
Definition: bounds.hpp:87
double maxy
the maximum y-coordinate
Definition: bounds.hpp:30