Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_geometry.h
1 // ================================================================================
2 // == This file is a part of Turbo Badger. (C) 2011-2014, Emil SegerÃ¥s ==
3 // == See tb_core.h for more information. ==
4 // ================================================================================
5 
6 #ifndef TB_GEOMETRY_H
7 #define TB_GEOMETRY_H
8 
9 #include "tb_core.h"
10 
11 namespace tb {
12 
15 class TBPoint
16 {
17 public:
18  int x, y;
19  TBPoint() : x(0), y(0) {}
20  TBPoint(int x_, int y_) : x(x_), y(y_) {}
21 };
22 
25 class TBRect
26 {
27 public:
28  int x, y, w, h;
29  TBRect() : x(0), y(0), w(0), h(0) {}
30  TBRect(int x_, int y_, int w_, int h_) : x(x_), y(y_), w(w_), h(h_) {}
31 
32  inline bool IsEmpty() const { return w <= 0 || h <= 0; }
33  inline bool IsInsideOut() const { return w < 0 || h < 0; }
34  inline bool Equals(const TBRect &rect) const { return rect.x == x && rect.y == y && rect.w == w && rect.h == h; }
35  bool Intersects(const TBRect &rect) const;
36  bool Contains(const TBPoint &p) const { return p.x >= x && p.y >= y && p.x < x + w && p.y < y + h; }
37 
38  inline void Reset() { x = y = w = h = 0; }
39  inline void Set(int x_, int y_, int w_, int h_) { this->x = x_; this->y = y_; this->w = w_; this->h = h_; }
40 
41  inline TBRect Shrink(int left, int top, int right, int bottom) const { return TBRect(x + left, y + top, w - left - right, h - top - bottom); }
42  inline TBRect Expand(int left, int top, int right, int bottom) const { return Shrink(-left, -top, -right, -bottom); }
43  inline TBRect Shrink(int tx, int ty) const { return TBRect(x + tx, y + ty, w - tx * 2, h - ty * 2); }
44  inline TBRect Expand(int tx, int ty) const { return Shrink(-tx, -ty); }
45  inline TBRect Offset(int dx, int dy) const { return TBRect(x + dx, y + dy, w, h); }
46 
49  TBRect MoveIn(const TBRect &bounding_rect) const;
50 
52  TBRect CenterIn(const TBRect &bounding_rect) const;
53 
55  TBRect RightCenterIn(const TBRect &bounding_rect) const;
56 
57  TBRect Union(const TBRect &rect) const;
58  TBRect Clip(const TBRect &clip_rect) const;
59 };
60 
63 class TBRegion
64 {
65 public:
66  TBRegion();
67  ~TBRegion();
68 
70  void RemoveRect(int index);
71 
74  void RemoveRectFast(int index);
75 
79  void RemoveAll(bool free_memory = true);
80 
82  bool Set(const TBRect &rect);
83 
88  bool AddRect(const TBRect &rect, bool coalesce);
89 
93  bool IncludeRect(const TBRect &include_rect);
94 
96  bool ExcludeRect(const TBRect &exclude_rect);
97 
99  bool AddExcludingRects(const TBRect &rect, const TBRect &exclude_rect, bool coalesce);
100 
101  bool IsEmpty() const { return m_num_rects == 0; }
102  int GetNumRects() const { return m_num_rects; }
103  const TBRect &GetRect(int index) const;
104 private:
105  TBRect *m_rects;
106  int m_num_rects;
107  int m_capacity;
108  bool GrowIfNeeded();
109 };
110 
111 } // namespace tb
112 
113 #endif // TB_GEOMETRY_H
TBRect RightCenterIn(const TBRect &bounding_rect) const
Return a rect vertically centered on the right side in bounding_rect.
Definition: tb_geometry.cpp:35
void RemoveRect(int index)
Remove the rect at the given index.
Definition: tb_geometry.cpp:86
Simple point class.
Definition: tb_geometry.h:15
TBRegion does calculations on regions represented by a list of rectangles.
Definition: tb_geometry.h:63
bool IncludeRect(const TBRect &include_rect)
Include the rect in the region.
Definition: tb_geometry.cpp:166
bool ExcludeRect(const TBRect &exclude_rect)
Exclude the rect from the region.
Definition: tb_geometry.cpp:190
void RemoveRectFast(int index)
Remove the rect at the given index.
Definition: tb_geometry.cpp:94
bool AddExcludingRects(const TBRect &rect, const TBRect &exclude_rect, bool coalesce)
Add the rectangles that&#39;s left of rect after excluding exclude_rect.
Definition: tb_geometry.cpp:213
Simple rectangle class.
Definition: tb_geometry.h:25
TBRect CenterIn(const TBRect &bounding_rect) const
Return a rect centered in bounding_rect.
Definition: tb_geometry.cpp:30
bool AddRect(const TBRect &rect, bool coalesce)
Add the rect without doing any overlap check.
Definition: tb_geometry.cpp:134
TBRect MoveIn(const TBRect &bounding_rect) const
Return a rect moved inside bounding_rect.
Definition: tb_geometry.cpp:23
bool Set(const TBRect &rect)
Set the region to the given rect.
Definition: tb_geometry.cpp:111
void RemoveAll(bool free_memory=true)
Remove all rectangles so the region becomes empty.
Definition: tb_geometry.cpp:100