]> git.mxchange.org Git - simgear.git/blob - simgear/math/Rect.hxx
Some basic C++/Nasal binding helpers
[simgear.git] / simgear / math / Rect.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 namespace simgear
23 {
24
25   template<typename T>
26   class Rect
27   {
28     public:
29       Rect():
30         _x1(0),
31         _x2(0),
32         _y1(0),
33         _y2(0)
34       {}
35
36       Rect(T x, T y, T w, T h):
37         _x1(x),
38         _x2(x + w),
39         _y1(y),
40         _y2(y + h)
41       {}
42
43       void set(T x, T y, T w, T h)
44       {
45         _x1 = x;
46         _x2 = x + w;
47         _y1 = y;
48         _y2 = y + h;
49       }
50
51       T x() const { return _x1; }
52       T y() const { return _y1; }
53       T width() const { return _x2 - _x1; }
54       T height() const { return _y2 - _y1; }
55
56       void setX(T x) { T w = width(); _x1 = x; _x2 = x + w; }
57       void setY(T y) { T h = height(); _y1 = y; _y2 = y + h; }
58       void setWidth(T w) { _x2 = _x1 + w; }
59       void setHeight(T h) { _y2 = _y1 + h; }
60
61       T l() const { return _x1; }
62       T r() const { return _x2; }
63       T t() const { return _y1; }
64       T b() const { return _y2; }
65
66       void setLeft(T l) { _x1 = l; }
67       void setRight(T r) { _x2 = r; }
68       void setTop(T t) { _y1 = t; }
69       void setBottom(T b) { _y2 = b; }
70
71       bool contains(T x, T y) const
72       {
73         return _x1 <= x && x <= _x2
74             && _y1 <= y && y <= _y2;
75       }
76
77     private:
78       T _x1, _x2, _y1, _y2;
79   };
80
81 } // namespace simgear
82
83 #endif /* SG_RECT_HXX_ */