bWidgets
Core widget toolkit designed for Blender
bwRectangle.h
Go to the documentation of this file.
1#pragma once
2
3namespace bWidgets {
4
5template<typename T> class bwRectangle {
6 public:
7 inline bwRectangle(const T xmin = 0, const T xmax = 0, const T ymin = 0, const T ymax = 0)
9 {
10 }
11 template<typename U>
12 inline bwRectangle(const bwRectangle<U>& rect)
13 : xmin(rect.xmin), xmax(rect.xmax), ymin(rect.ymin), ymax(rect.ymax)
14 {
15 }
16 template<typename U>
18 : xmin(rect.xmin), xmax(rect.xmax), ymin(rect.ymin), ymax(rect.ymax)
19 {
20 }
21
22 inline void set(const T _xmin, const T width, const T _ymin, const T height)
23 {
24 xmin = _xmin;
25 xmax = xmin + width;
26 ymin = _ymin;
27 ymax = ymin + height;
28 }
29
33 inline void resize(const T pixel)
34 {
35 xmin -= pixel;
36 xmax += pixel;
37 ymin -= pixel;
38 ymax += pixel;
39 }
40
41 template<typename U> inline bool isCoordinateInside(const U x, const U y) const
42 {
43 return (x >= xmin) && (x <= xmax) && (y >= ymin) && (y <= ymax);
44 }
45
46 inline bool isEmpty() const
47 {
48 return (xmin == xmax) || (ymin == ymax);
49 }
50
51 inline T width() const
52 {
53 return xmax - xmin;
54 }
55 inline T height() const
56 {
57 return ymax - ymin;
58 }
59
60 inline T centerX() const
61 {
62 return xmin + (width() / T(2));
63 }
64 inline T centerY() const
65 {
66 return ymin + (height() / T(2));
67 }
68
69 inline void scale(float scale)
70 {
71 const T cent_x = centerX();
72 const T cent_y = centerY();
73 const T size_x_half = width() * (scale * 0.5f);
74 const T size_y_half = height() * (scale * 0.5f);
75 xmin = cent_x - size_x_half;
76 ymin = cent_y - size_y_half;
77 xmax = cent_x + size_x_half;
78 ymax = cent_y + size_y_half;
79 }
80
81 inline void clamp(const bwRectangle<T> boundary)
82 {
83 if (xmin < boundary.xmin) {
84 xmin = boundary.xmin;
85 }
86 if (xmin > boundary.xmax) {
87 xmin = boundary.xmax;
88 }
89 if (xmax > boundary.xmax) {
90 xmax = boundary.xmax;
91 }
92 if (xmax < boundary.xmin) {
93 xmax = boundary.xmin;
94 }
95 if (ymin < boundary.ymin) {
96 ymin = boundary.ymin;
97 }
98 if (ymin > boundary.ymax) {
99 ymin = boundary.ymax;
100 }
101 if (ymax > boundary.ymax) {
102 ymax = boundary.ymax;
103 }
104 if (ymax < boundary.ymin) {
105 ymax = boundary.ymin;
106 }
107 }
108
111};
112
113// useful aliases
115
116} // namespace bWidgets
Definition: bwRectangle.h:5
bool isEmpty() const
Definition: bwRectangle.h:46
bool isCoordinateInside(const U x, const U y) const
Definition: bwRectangle.h:41
T ymax
Definition: bwRectangle.h:110
void clamp(const bwRectangle< T > boundary)
Definition: bwRectangle.h:81
bwRectangle(bwRectangle< U > &&rect)
Definition: bwRectangle.h:17
T height() const
Definition: bwRectangle.h:55
T centerY() const
Definition: bwRectangle.h:64
bwRectangle(const bwRectangle< U > &rect)
Definition: bwRectangle.h:12
T ymin
Definition: bwRectangle.h:110
T centerX() const
Definition: bwRectangle.h:60
bwRectangle(const T xmin=0, const T xmax=0, const T ymin=0, const T ymax=0)
Definition: bwRectangle.h:7
T width() const
Definition: bwRectangle.h:51
T xmax
Definition: bwRectangle.h:109
void set(const T _xmin, const T width, const T _ymin, const T height)
Definition: bwRectangle.h:22
void scale(float scale)
Definition: bwRectangle.h:69
T xmin
Definition: bwRectangle.h:109
void resize(const T pixel)
Definition: bwRectangle.h:33
Definition: bwContext.h:3