1 // Class representing a rectangular region
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "SGLimits.hxx"
31 _min(SGLimits<T>::max(), SGLimits<T>::max()),
32 _max(-SGLimits<T>::max(), -SGLimits<T>::max())
37 explicit SGRect(const SGVec2<T>& pt):
51 SGRect(const SGVec2<T>& min, const SGVec2<T>& max):
58 SGRect(T x, T y, T w, T h):
66 explicit SGRect(const SGRect<S>& rect):
73 void setMin(const SGVec2<T>& min) { _min = min; }
74 const SGVec2<T>& getMin() const { return _min; }
76 void setMax(const SGVec2<T>& max) { _max = max; }
77 const SGVec2<T>& getMax() const { return _max; }
79 void set(T x, T y, T w, T h)
87 T x() const { return _min.x(); }
88 T y() const { return _min.y(); }
89 T width() const { return _max.x() - _min.x(); }
90 T height() const { return _max.y() - _min.y(); }
91 SGVec2<T> const& pos() const { return _min; }
92 SGVec2<T> size() const { return SGVec2<T>(width(), height()); }
94 void setX(T x) { T w = width(); _min.x() = x; _max.x() = x + w; }
95 void setY(T y) { T h = height(); _min.y() = y; _max.y() = y + h; }
96 void setWidth(T w) { _max.x() = _min.x() + w; }
97 void setHeight(T h) { _max.y() = _min.y() + h; }
98 void setPos(const SGVec2<T>& p) { setX(p.x()); setY(p.y()); }
99 void setSize(const SGVec2<T>& s) { setWidth(s.x()); setHeight(s.y()); }
101 T l() const { return _min.x(); }
102 T r() const { return _max.x(); }
103 T t() const { return _min.y(); }
104 T b() const { return _max.y(); }
106 T& l() { return _min.x(); }
107 T& r() { return _max.x(); }
108 T& t() { return _min.y(); }
109 T& b() { return _max.y(); }
111 void setLeft(T l) { _min.x() = l; }
112 void setRight(T r) { _max.x() = r; }
113 void setTop(T t) { _min.y() = t; }
114 void setBottom(T b) { _max.y() = b; }
117 * Move rect by vector
119 SGRect& operator+=(const SGVec2<T>& offset)
127 * Move rect by vector in inverse direction
129 SGRect& operator-=(const SGVec2<T>& offset)
136 bool operator==(const SGRect<T>& rhs) const
138 return _min == rhs._min
142 bool operator!=(const SGRect<T>& rhs) const
144 return !(*this == rhs);
147 bool contains(T x, T y) const
149 return _min.x() <= x && x <= _max.x()
150 && _min.y() <= y && y <= _max.y();
153 bool contains(T x, T y, T margin) const
155 return (_min.x() - margin) <= x && x <= (_max.x() + margin)
156 && (_min.y() - margin) <= y && y <= (_max.y() + margin);
165 inline SGRect<T> operator+(SGRect<T> rect, const SGVec2<T>& offset)
167 return rect += offset;
171 inline SGRect<T> operator+(const SGVec2<T>& offset, SGRect<T> rect)
173 return rect += offset;
177 inline SGRect<T> operator-(SGRect<T> rect, const SGVec2<T>& offset)
179 return rect -= offset;
183 inline SGRect<T> operator-(const SGVec2<T>& offset, SGRect<T> rect)
185 return rect -= offset;
188 template<typename char_type, typename traits_type, typename T>
190 std::basic_ostream<char_type, traits_type>&
191 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGRect<T>& rect)
192 { return s << "min = " << rect.getMin() << ", max = " << rect.getMax(); }
194 typedef SGRect<int> SGRecti;
195 typedef SGRect<float> SGRectf;
196 typedef SGRect<double> SGRectd;
198 #endif /* SG_RECT_HXX_ */