xref: /haiku/headers/os/interface/Rect.h (revision b55a57da7173b9af0432bd3e148d03f06161d036)
1 /*
2  * Copyright 2001-2009, 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 <Point.h>
10 #include <Size.h>
11 
12 #include <math.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 
29 			BRect&				operator=(const BRect& other);
30 			void				Set(float left, float top, float right,
31 									float bottom);
32 
33 			void				PrintToStream() const;
34 
35 			BPoint				LeftTop() const;
36 			BPoint				RightBottom() const;
37 			BPoint				LeftBottom() const;
38 			BPoint				RightTop() const;
39 
40 			void				SetLeftTop(const BPoint leftTop);
41 			void				SetRightBottom(const BPoint rightBottom);
42 			void				SetLeftBottom(const BPoint leftBottom);
43 			void				SetRightTop(const BPoint rightTop);
44 
45 	// Transformation
46 			void				InsetBy(BPoint inset);
47 			void				InsetBy(float dx, float dy);
48 			void				OffsetBy(BPoint delta);
49 			void				OffsetBy(float dx, float dy);
50 			void				OffsetTo(BPoint offset);
51 			void				OffsetTo(float x, float y);
52 
53 	// Expression transformations
54 			BRect&				InsetBySelf(BPoint inset);
55 			BRect&				InsetBySelf(float dx, float dy);
56 			BRect				InsetByCopy(BPoint inset);
57 			BRect				InsetByCopy(float dx, float dy);
58 			BRect&				OffsetBySelf(BPoint offset);
59 			BRect&				OffsetBySelf(float dx, float dy);
60 			BRect				OffsetByCopy(BPoint offset);
61 			BRect				OffsetByCopy(float dx, float dy);
62 			BRect&				OffsetToSelf(BPoint offset);
63 			BRect&				OffsetToSelf(float dx, float dy);
64 			BRect				OffsetToCopy(BPoint offset);
65 			BRect				OffsetToCopy(float dx, float dy);
66 
67 	// Comparison
68 			bool				operator==(BRect r) const;
69 			bool				operator!=(BRect r) const;
70 
71 	// Intersection and union
72 			BRect				operator&(BRect r) const;
73 			BRect				operator|(BRect r) const;
74 
75 			bool				IsValid() const;
76 			float				Width() const;
77 			int32				IntegerWidth() const;
78 			float				Height() const;
79 			int32				IntegerHeight() const;
80 			BSize				Size() const;
81 
82 			bool				Intersects(BRect r) const;
83 			bool				Contains(BPoint p) const;
84 			bool				Contains(BRect r) const;
85 };
86 
87 
88 // #pragma mark - inline definitions
89 
90 inline BPoint
91 BRect::LeftTop() const
92 {
93 	return *(const BPoint *)&left;
94 }
95 
96 
97 inline BPoint
98 BRect::RightBottom() const
99 {
100 	return *(const BPoint *)&right;
101 }
102 
103 
104 inline BPoint
105 BRect::LeftBottom() const
106 {
107 	return BPoint(left, bottom);
108 }
109 
110 
111 inline BPoint
112 BRect::RightTop() const
113 {
114 	return BPoint(right, top);
115 }
116 
117 
118 inline
119 BRect::BRect()
120 {
121 	top = left = 0;
122 	bottom = right = -1;
123 }
124 
125 
126 inline
127 BRect::BRect(float l, float t, float r, float b)
128 {
129 	left = l;
130 	top = t;
131 	right = r;
132 	bottom = b;
133 }
134 
135 
136 inline
137 BRect::BRect(const BRect& r)
138 {
139 	left = r.left;
140 	top = r.top;
141 	right = r.right;
142 	bottom = r.bottom;
143 }
144 
145 
146 inline
147 BRect::BRect(BPoint leftTop, BPoint rightBottom)
148 {
149 	left = leftTop.x;
150 	top = leftTop.y;
151 	right = rightBottom.x;
152 	bottom = rightBottom.y;
153 }
154 
155 
156 inline
157 BRect::BRect(BPoint leftTop, BSize size)
158 	: left(leftTop.x),
159 	  top(leftTop.y),
160 	  right(leftTop.x + size.width),
161 	  bottom(leftTop.y + size.height)
162 {
163 }
164 
165 
166 inline BRect&
167 BRect::operator=(const BRect& from)
168 {
169 	left = from.left;
170 	top = from.top;
171 	right = from.right;
172 	bottom = from.bottom;
173 	return *this;
174 }
175 
176 
177 inline void
178 BRect::Set(float l, float t, float r, float b)
179 {
180 	left = l;
181 	top = t;
182 	right = r;
183 	bottom = b;
184 }
185 
186 
187 inline bool
188 BRect::IsValid() const
189 {
190 	return left <= right && top <= bottom;
191 }
192 
193 
194 inline int32
195 BRect::IntegerWidth() const
196 {
197 	return (int32)ceil(right - left);
198 }
199 
200 
201 inline float
202 BRect::Width() const
203 {
204 	return right - left;
205 }
206 
207 
208 inline int32
209 BRect::IntegerHeight() const
210 {
211 	return (int32)ceil(bottom - top);
212 }
213 
214 
215 inline float
216 BRect::Height() const
217 {
218 	return bottom - top;
219 }
220 
221 inline BSize
222 BRect::Size() const
223 {
224 	return BSize(right - left, bottom - top);
225 }
226 
227 
228 #endif	// _RECT_H
229