]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGRect.hxx
HTTP: Rename urlretrieve/urlload to save/load.
[simgear.git] / simgear / math / SGRect.hxx
1 // Class representing a rectangular region
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
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.
9 //
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.
14 //
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
18
19 #ifndef SG_RECT_HXX_
20 #define SG_RECT_HXX_
21
22 #include "SGLimits.hxx"
23 #include "SGVec2.hxx"
24
25 template<typename T>
26 class SGRect
27 {
28   public:
29
30     SGRect():
31       _min(SGLimits<T>::max(), SGLimits<T>::max()),
32       _max(-SGLimits<T>::max(), -SGLimits<T>::max())
33     {
34
35     }
36
37     SGRect(const SGVec2<T>& pt):
38       _min(pt),
39       _max(pt)
40     {
41
42     }
43
44     SGRect(T x, T y):
45       _min(x, y),
46       _max(x, y)
47     {
48
49     }
50
51     SGRect(const SGVec2<T>& min, const SGVec2<T>& max):
52       _min(min),
53       _max(max)
54     {
55
56     }
57
58     SGRect(T x, T y, T w, T h):
59       _min(x, y),
60       _max(x + w, y + h)
61     {
62
63     }
64
65     template<typename S>
66     explicit SGRect(const SGRect<S>& rect):
67       _min(rect.getMin()),
68       _max(rect.getMax())
69     {
70
71     }
72
73     void setMin(const SGVec2<T>& min) { _min = min; }
74     const SGVec2<T>& getMin() const { return _min; }
75
76     void setMax(const SGVec2<T>& max) { _max = max; }
77     const SGVec2<T>& getMax() const { return _max; }
78
79     void set(T x, T y, T w, T h)
80     {
81       _min.x() = x;
82       _min.y() = y;
83       _max.x() = x + w;
84       _max.y() = y + h;
85     }
86
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
92     void setX(T x) { T w = width(); _min.x() = x; _max.x() = x + w; }
93     void setY(T y) { T h = height(); _min.y() = y; _max.y() = y + h; }
94     void setWidth(T w) { _max.x() = _min.x() + w; }
95     void setHeight(T h) { _max.y() = _min.y() + h; }
96
97     T l() const { return _min.x(); }
98     T r() const { return _max.x(); }
99     T t() const { return _min.y(); }
100     T b() const { return _max.y(); }
101
102     T& l() { return _min.x(); }
103     T& r() { return _max.x(); }
104     T& t() { return _min.y(); }
105     T& b() { return _max.y(); }
106
107     void setLeft(T l) { _min.x() = l; }
108     void setRight(T r) { _max.x() = r; }
109     void setTop(T t) { _min.y() = t; }
110     void setBottom(T b) { _max.y() = b; }
111
112     /**
113      * Move rect by vector
114      */
115     SGRect& operator+=(const SGVec2<T>& offset)
116     {
117       _min += offset;
118       _max += offset;
119       return *this;
120     }
121
122     /**
123      * Move rect by vector in inverse direction
124      */
125     SGRect& operator-=(const SGVec2<T>& offset)
126     {
127       _min -= offset;
128       _max -= offset;
129       return *this;
130     }
131
132     bool contains(T x, T y) const
133     {
134       return _min.x() <= x && x <= _max.x()
135           && _min.y() <= y && y <= _max.y();
136     }
137
138     bool contains(T x, T y, T margin) const
139     {
140       return (_min.x() - margin) <= x && x <= (_max.x() + margin)
141           && (_min.y() - margin) <= y && y <= (_max.y() + margin);
142     }
143
144   private:
145     SGVec2<T> _min,
146               _max;
147 };
148
149 template<typename T>
150 inline SGRect<T> operator+(SGRect<T> rect, const SGVec2<T>& offset)
151 {
152   return rect += offset;
153 }
154
155 template<typename T>
156 inline SGRect<T> operator+(const SGVec2<T>& offset, SGRect<T> rect)
157 {
158   return rect += offset;
159 }
160
161 template<typename T>
162 inline SGRect<T> operator-(SGRect<T> rect, const SGVec2<T>& offset)
163 {
164   return rect -= offset;
165 }
166
167 template<typename T>
168 inline SGRect<T> operator-(const SGVec2<T>& offset, SGRect<T> rect)
169 {
170   return rect -= offset;
171 }
172
173 template<typename char_type, typename traits_type, typename T>
174 inline
175 std::basic_ostream<char_type, traits_type>&
176 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGRect<T>& rect)
177 { return s << "min = " << rect.getMin() << ", max = " << rect.getMax(); }
178
179 #endif /* SG_RECT_HXX_ */