xref: /haiku/headers/os/interface/Rect.h (revision 344ded80d400028c8f561b4b876257b94c12db4a)
1 /*
2  * Copyright 2001-2012, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef	_RECT_H
6 #define	_RECT_H
7 
8 
9 #include <math.h>
10 
11 #include <Point.h>
12 #include <Size.h>
13 
14 
15 class BRect {
16 public:
17 			float				left;
18 			float				top;
19 			float				right;
20 			float				bottom;
21 
22 								BRect();
23 								BRect(const BRect& other);
24 								BRect(float left, float top, float right,
25 									float bottom);
26 								BRect(BPoint leftTop, BPoint rightBottom);
27 								BRect(BPoint leftTop, BSize size);
28 								BRect(float side);
29 								BRect(float width, float height);
30 
31 			BRect&				operator=(const BRect& other);
32 			void				Set(float left, float top, float right,
33 									float bottom);
34 
35 			void				PrintToStream() const;
36 
37 			BPoint				LeftTop() const;
38 			BPoint				RightBottom() const;
39 			BPoint				LeftBottom() const;
40 			BPoint				RightTop() const;
41 
42 			void				SetLeftTop(const BPoint point);
43 			void				SetRightBottom(const BPoint point);
44 			void				SetLeftBottom(const BPoint point);
45 			void				SetRightTop(const BPoint point);
46 
47 	// Transformation
48 			void				InsetBy(BPoint inset);
49 			void				InsetBy(float dx, float dy);
50 			void				OffsetBy(BPoint delta);
51 			void				OffsetBy(float dx, float dy);
52 			void				OffsetTo(BPoint offset);
53 			void				OffsetTo(float x, float y);
54 
55 	// Expression transformations
56 			BRect&				InsetBySelf(BPoint inset);
57 			BRect&				InsetBySelf(float dx, float dy);
58 			BRect				InsetByCopy(BPoint inset) const;
59 			BRect				InsetByCopy(float dx, float dy) const;
60 			BRect&				OffsetBySelf(BPoint offset);
61 			BRect&				OffsetBySelf(float dx, float dy);
62 			BRect				OffsetByCopy(BPoint offset) const;
63 			BRect				OffsetByCopy(float dx, float dy) const;
64 			BRect&				OffsetToSelf(BPoint offset);
65 			BRect&				OffsetToSelf(float x, float y);
66 			BRect				OffsetToCopy(BPoint offset) const;
67 			BRect				OffsetToCopy(float x, float y) const;
68 
69 	// Comparison
70 			bool				operator==(BRect other) const;
71 			bool				operator!=(BRect other) const;
72 
73 	// Intersection and union
74 			BRect				operator&(BRect other) const;
75 			BRect				operator|(BRect other) const;
76 
77 			bool				IsValid() const;
78 			float				Width() const;
79 			int32				IntegerWidth() const;
80 			float				Height() const;
81 			int32				IntegerHeight() const;
82 			BSize				Size() const;
83 
84 			bool				Intersects(BRect rect) const;
85 			bool				Contains(BPoint point) const;
86 			bool				Contains(BRect rect) const;
87 };
88 
89 
90 // #pragma mark - inline definitions
91 
92 inline BPoint
93 BRect::LeftTop() const
94 {
95 	return *(const BPoint*)&left;
96 }
97 
98 
99 inline BPoint
100 BRect::RightBottom() const
101 {
102 	return *(const BPoint*)&right;
103 }
104 
105 
106 inline BPoint
107 BRect::LeftBottom() const
108 {
109 	return BPoint(left, bottom);
110 }
111 
112 
113 inline BPoint
114 BRect::RightTop() const
115 {
116 	return BPoint(right, top);
117 }
118 
119 
120 inline
121 BRect::BRect()
122 	:
123 	left(0),
124 	top(0),
125 	right(-1),
126 	bottom(-1)
127 {
128 }
129 
130 
131 inline
132 BRect::BRect(float left, float top, float right, float bottom)
133 	:
134 	left(left),
135 	top(top),
136 	right(right),
137 	bottom(bottom)
138 {
139 }
140 
141 
142 inline
143 BRect::BRect(const BRect& other)
144 	:
145 	left(other.left),
146 	top(other.top),
147 	right(other.right),
148 	bottom(other.bottom)
149 {
150 }
151 
152 
153 inline
154 BRect::BRect(BPoint leftTop, BPoint rightBottom)
155 	:
156 	left(leftTop.x),
157 	top(leftTop.y),
158 	right(rightBottom.x),
159 	bottom(rightBottom.y)
160 {
161 }
162 
163 
164 inline
165 BRect::BRect(BPoint leftTop, BSize size)
166 	:
167 	left(leftTop.x),
168 	top(leftTop.y),
169 	right(leftTop.x + size.width),
170 	bottom(leftTop.y + size.height)
171 {
172 }
173 
174 
175 inline
176 BRect::BRect(float side)
177 	:
178 	left(0),
179 	top(0),
180 	right(side - 1),
181 	bottom(side - 1)
182 {
183 }
184 
185 
186 inline
187 BRect::BRect(float width, float height)
188 	:
189 	left(0),
190 	top(0),
191 	right(width),
192 	bottom(height)
193 {
194 }
195 
196 
197 inline BRect&
198 BRect::operator=(const BRect& other)
199 {
200 	left = other.left;
201 	top = other.top;
202 	right = other.right;
203 	bottom = other.bottom;
204 	return *this;
205 }
206 
207 
208 inline void
209 BRect::Set(float left, float top, float right, float bottom)
210 {
211 	this->left = left;
212 	this->top = top;
213 	this->right = right;
214 	this->bottom = bottom;
215 }
216 
217 
218 inline bool
219 BRect::IsValid() const
220 {
221 	return left <= right && top <= bottom;
222 }
223 
224 
225 inline int32
226 BRect::IntegerWidth() const
227 {
228 	return (int32)ceil(right - left);
229 }
230 
231 
232 inline float
233 BRect::Width() const
234 {
235 	return right - left;
236 }
237 
238 
239 inline int32
240 BRect::IntegerHeight() const
241 {
242 	return (int32)ceil(bottom - top);
243 }
244 
245 
246 inline float
247 BRect::Height() const
248 {
249 	return bottom - top;
250 }
251 
252 
253 inline BSize
254 BRect::Size() const
255 {
256 	return BSize(right - left, bottom - top);
257 }
258 
259 
260 #endif	// _RECT_H
261