]> git.mxchange.org Git - simgear.git/blobdiff - simgear/math/SGRect.hxx
Trying to make old gcc on Jenkins happy.
[simgear.git] / simgear / math / SGRect.hxx
index 7e70a12488a0dec89045f39f5eaec5f8b7560c7d..86dd482c9c8875c54bdd848abff6fe2df6656e32 100644 (file)
@@ -34,7 +34,7 @@ class SGRect
 
     }
 
-    SGRect(const SGVec2<T>& pt):
+    explicit SGRect(const SGVec2<T>& pt):
       _min(pt),
       _max(pt)
     {
@@ -88,11 +88,15 @@ class SGRect
     T y() const { return _min.y(); }
     T width() const { return _max.x() - _min.x(); }
     T height() const { return _max.y() - _min.y(); }
+    SGVec2<T> const& pos() const { return _min; }
+    SGVec2<T> size() const { return SGVec2<T>(width(), height()); }
 
     void setX(T x) { T w = width(); _min.x() = x; _max.x() = x + w; }
     void setY(T y) { T h = height(); _min.y() = y; _max.y() = y + h; }
     void setWidth(T w) { _max.x() = _min.x() + w; }
     void setHeight(T h) { _max.y() = _min.y() + h; }
+    void setPos(const SGVec2<T>& p) { setX(p.x()); setY(p.y()); }
+    void setSize(const SGVec2<T>& s) { setWidth(s.x()); setHeight(s.y()); }
 
     T l() const { return _min.x(); }
     T r() const { return _max.x(); }
@@ -109,6 +113,49 @@ class SGRect
     void setTop(T t) { _min.y() = t; }
     void setBottom(T b) { _max.y() = b; }
 
+    /**
+     * Expand rectangle to include the given position
+     */
+    void expandBy(T x, T y)
+    {
+      if( x < _min.x() ) _min.x() = x;
+      if( x > _max.x() ) _max.x() = x;
+
+      if( y < _min.y() ) _min.y() = y;
+      if( y > _max.y() ) _max.y() = y;
+    }
+
+    /**
+     * Move rect by vector
+     */
+    SGRect& operator+=(const SGVec2<T>& offset)
+    {
+      _min += offset;
+      _max += offset;
+      return *this;
+    }
+
+    /**
+     * Move rect by vector in inverse direction
+     */
+    SGRect& operator-=(const SGVec2<T>& offset)
+    {
+      _min -= offset;
+      _max -= offset;
+      return *this;
+    }
+
+    bool operator==(const SGRect<T>& rhs) const
+    {
+      return _min == rhs._min
+          && _max == rhs._max;
+    }
+
+    bool operator!=(const SGRect<T>& rhs) const
+    {
+      return !(*this == rhs);
+    }
+
     bool contains(T x, T y) const
     {
       return _min.x() <= x && x <= _max.x()
@@ -126,10 +173,38 @@ class SGRect
               _max;
 };
 
+template<typename T>
+inline SGRect<T> operator+(SGRect<T> rect, const SGVec2<T>& offset)
+{
+  return rect += offset;
+}
+
+template<typename T>
+inline SGRect<T> operator+(const SGVec2<T>& offset, SGRect<T> rect)
+{
+  return rect += offset;
+}
+
+template<typename T>
+inline SGRect<T> operator-(SGRect<T> rect, const SGVec2<T>& offset)
+{
+  return rect -= offset;
+}
+
+template<typename T>
+inline SGRect<T> operator-(const SGVec2<T>& offset, SGRect<T> rect)
+{
+  return rect -= offset;
+}
+
 template<typename char_type, typename traits_type, typename T>
 inline
 std::basic_ostream<char_type, traits_type>&
 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGRect<T>& rect)
 { return s << "min = " << rect.getMin() << ", max = " << rect.getMax(); }
 
+typedef SGRect<int> SGRecti;
+typedef SGRect<float> SGRectf;
+typedef SGRect<double> SGRectd;
+
 #endif /* SG_RECT_HXX_ */