162583Sitojun/*
278064Sume * Copyright 2001-2012, Haiku, Inc. All rights reserved.
362583Sitojun * Distributed under the terms of the MIT License.
462583Sitojun */
562583Sitojun#ifndef	_RECT_H
662583Sitojun#define	_RECT_H
762583Sitojun
862583Sitojun
962583Sitojun#include <math.h>
1062583Sitojun
1162583Sitojun#include <Point.h>
1262583Sitojun#include <Size.h>
1362583Sitojun
1462583Sitojun
1562583Sitojunclass BRect {
1662583Sitojunpublic:
1762583Sitojun			float				left;
1862583Sitojun			float				top;
1962583Sitojun			float				right;
2062583Sitojun			float				bottom;
2162583Sitojun
2262583Sitojun								BRect();
2362583Sitojun								BRect(const BRect& other);
2462583Sitojun								BRect(float left, float top, float right,
2562583Sitojun									float bottom);
2662583Sitojun								BRect(BPoint leftTop, BPoint rightBottom);
2762583Sitojun								BRect(BPoint leftTop, BSize size);
2862583Sitojun								BRect(float side);
2962583Sitojun
3062583Sitojun			BRect&				operator=(const BRect& other);
3162583Sitojun			void				Set(float left, float top, float right,
3262583Sitojun									float bottom);
3378064Sume
3492917Sobrien			void				PrintToStream() const;
3592917Sobrien
3662583Sitojun			BPoint				LeftTop() const;
3762583Sitojun			BPoint				RightBottom() const;
3878064Sume			BPoint				LeftBottom() const;
3992917Sobrien			BPoint				RightTop() const;
4092917Sobrien
4192917Sobrien			void				SetLeftTop(const BPoint leftTop);
4292917Sobrien			void				SetRightBottom(const BPoint rightBottom);
4392917Sobrien			void				SetLeftBottom(const BPoint leftBottom);
4492941Sobrien			void				SetRightTop(const BPoint rightTop);
4592941Sobrien
4692941Sobrien	// Transformation
4792941Sobrien			void				InsetBy(BPoint inset);
4892941Sobrien			void				InsetBy(float dx, float dy);
4992941Sobrien			void				OffsetBy(BPoint delta);
5092941Sobrien			void				OffsetBy(float dx, float dy);
5192941Sobrien			void				OffsetTo(BPoint offset);
5292941Sobrien			void				OffsetTo(float x, float y);
5392941Sobrien
5492941Sobrien	// Expression transformations
5592941Sobrien			BRect&				InsetBySelf(BPoint inset);
5692941Sobrien			BRect&				InsetBySelf(float dx, float dy);
5792941Sobrien			BRect				InsetByCopy(BPoint inset) const;
5892917Sobrien			BRect				InsetByCopy(float dx, float dy) const;
5992917Sobrien			BRect&				OffsetBySelf(BPoint offset);
6092917Sobrien			BRect&				OffsetBySelf(float dx, float dy);
6192917Sobrien			BRect				OffsetByCopy(BPoint offset) const;
6292917Sobrien			BRect				OffsetByCopy(float dx, float dy) const;
6392917Sobrien			BRect&				OffsetToSelf(BPoint offset);
6492941Sobrien			BRect&				OffsetToSelf(float dx, float dy);
6592941Sobrien			BRect				OffsetToCopy(BPoint offset) const;
6692941Sobrien			BRect				OffsetToCopy(float dx, float dy) const;
6792941Sobrien
6892941Sobrien	// Comparison
6992941Sobrien			bool				operator==(BRect r) const;
7092941Sobrien			bool				operator!=(BRect r) const;
7192941Sobrien
7292941Sobrien	// Intersection and union
7392941Sobrien			BRect				operator&(BRect r) const;
7492917Sobrien			BRect				operator|(BRect r) const;
7592917Sobrien
7692941Sobrien			bool				IsValid() const;
7792941Sobrien			float				Width() const;
7892917Sobrien			int32				IntegerWidth() const;
7992917Sobrien			float				Height() const;
8062583Sitojun			int32				IntegerHeight() const;
8192917Sobrien			BSize				Size() const;
8292917Sobrien
8392917Sobrien			bool				Intersects(BRect r) const;
8492917Sobrien			bool				Contains(BPoint p) const;
8592917Sobrien			bool				Contains(BRect r) const;
8692917Sobrien};
87
88
89// #pragma mark - inline definitions
90
91inline BPoint
92BRect::LeftTop() const
93{
94	return *(const BPoint*)&left;
95}
96
97
98inline BPoint
99BRect::RightBottom() const
100{
101	return *(const BPoint*)&right;
102}
103
104
105inline BPoint
106BRect::LeftBottom() const
107{
108	return BPoint(left, bottom);
109}
110
111
112inline BPoint
113BRect::RightTop() const
114{
115	return BPoint(right, top);
116}
117
118
119inline
120BRect::BRect()
121	:
122	left(0),
123	top(0),
124	right(-1),
125	bottom(-1)
126{
127}
128
129
130inline
131BRect::BRect(float l, float t, float r, float b)
132	:
133	left(l),
134	top(t),
135	right(r),
136	bottom(b)
137{
138}
139
140
141inline
142BRect::BRect(const BRect& r)
143	:
144	left(r.left),
145	top(r.top),
146	right(r.right),
147	bottom(r.bottom)
148{
149}
150
151
152inline
153BRect::BRect(BPoint leftTop, BPoint rightBottom)
154	:
155	left(leftTop.x),
156	top(leftTop.y),
157	right(rightBottom.x),
158	bottom(rightBottom.y)
159{
160}
161
162
163inline
164BRect::BRect(BPoint leftTop, BSize size)
165	:
166	left(leftTop.x),
167	top(leftTop.y),
168	right(leftTop.x + size.width),
169	bottom(leftTop.y + size.height)
170{
171}
172
173
174inline
175BRect::BRect(float side)
176	:
177	left(0),
178	top(0),
179	right(side - 1),
180	bottom(side - 1)
181{
182}
183
184
185inline BRect&
186BRect::operator=(const BRect& from)
187{
188	left = from.left;
189	top = from.top;
190	right = from.right;
191	bottom = from.bottom;
192	return *this;
193}
194
195
196inline void
197BRect::Set(float l, float t, float r, float b)
198{
199	left = l;
200	top = t;
201	right = r;
202	bottom = b;
203}
204
205
206inline bool
207BRect::IsValid() const
208{
209	return left <= right && top <= bottom;
210}
211
212
213inline int32
214BRect::IntegerWidth() const
215{
216	return (int32)ceil(right - left);
217}
218
219
220inline float
221BRect::Width() const
222{
223	return right - left;
224}
225
226
227inline int32
228BRect::IntegerHeight() const
229{
230	return (int32)ceil(bottom - top);
231}
232
233
234inline float
235BRect::Height() const
236{
237	return bottom - top;
238}
239
240
241inline BSize
242BRect::Size() const
243{
244	return BSize(right - left, bottom - top);
245}
246
247
248#endif	// _RECT_H
249