1253599a2SIngo Weinhold/*
26643ead5SAxel Dörfler * Copyright 2001-2012, Haiku, Inc. All rights reserved.
3253599a2SIngo Weinhold * Distributed under the terms of the MIT License.
4253599a2SIngo Weinhold */
552a38012Sejakowatz#ifndef	_RECT_H
652a38012Sejakowatz#define	_RECT_H
752a38012Sejakowatz
852a38012Sejakowatz
96643ead5SAxel Dörfler#include <math.h>
106643ead5SAxel Dörfler
1152a38012Sejakowatz#include <Point.h>
12a19a660aSIngo Weinhold#include <Size.h>
1352a38012Sejakowatz
1452a38012Sejakowatz
1552a38012Sejakowatzclass BRect {
1689208c77SStephan Aßmuspublic:
1789208c77SStephan Aßmus			float				left;
1889208c77SStephan Aßmus			float				top;
1989208c77SStephan Aßmus			float				right;
2089208c77SStephan Aßmus			float				bottom;
2189208c77SStephan Aßmus
2289208c77SStephan Aßmus								BRect();
2389208c77SStephan Aßmus								BRect(const BRect& other);
2489208c77SStephan Aßmus								BRect(float left, float top, float right,
2589208c77SStephan Aßmus									float bottom);
2689208c77SStephan Aßmus								BRect(BPoint leftTop, BPoint rightBottom);
2789208c77SStephan Aßmus								BRect(BPoint leftTop, BSize size);
282d4087c9SJonas Sundström								BRect(float side);
2989208c77SStephan Aßmus
3089208c77SStephan Aßmus			BRect&				operator=(const BRect& other);
3189208c77SStephan Aßmus			void				Set(float left, float top, float right,
3289208c77SStephan Aßmus									float bottom);
3389208c77SStephan Aßmus
3489208c77SStephan Aßmus			void				PrintToStream() const;
3589208c77SStephan Aßmus
3689208c77SStephan Aßmus			BPoint				LeftTop() const;
3789208c77SStephan Aßmus			BPoint				RightBottom() const;
3889208c77SStephan Aßmus			BPoint				LeftBottom() const;
3989208c77SStephan Aßmus			BPoint				RightTop() const;
4089208c77SStephan Aßmus
411f46fc6dSJohn Scipione			void				SetLeftTop(const BPoint point);
421f46fc6dSJohn Scipione			void				SetRightBottom(const BPoint point);
431f46fc6dSJohn Scipione			void				SetLeftBottom(const BPoint point);
441f46fc6dSJohn Scipione			void				SetRightTop(const BPoint point);
4589208c77SStephan Aßmus
4689208c77SStephan Aßmus	// Transformation
4789208c77SStephan Aßmus			void				InsetBy(BPoint inset);
4889208c77SStephan Aßmus			void				InsetBy(float dx, float dy);
4989208c77SStephan Aßmus			void				OffsetBy(BPoint delta);
5089208c77SStephan Aßmus			void				OffsetBy(float dx, float dy);
5189208c77SStephan Aßmus			void				OffsetTo(BPoint offset);
5289208c77SStephan Aßmus			void				OffsetTo(float x, float y);
5389208c77SStephan Aßmus
5489208c77SStephan Aßmus	// Expression transformations
5589208c77SStephan Aßmus			BRect&				InsetBySelf(BPoint inset);
5689208c77SStephan Aßmus			BRect&				InsetBySelf(float dx, float dy);
576643ead5SAxel Dörfler			BRect				InsetByCopy(BPoint inset) const;
586643ead5SAxel Dörfler			BRect				InsetByCopy(float dx, float dy) const;
5989208c77SStephan Aßmus			BRect&				OffsetBySelf(BPoint offset);
6089208c77SStephan Aßmus			BRect&				OffsetBySelf(float dx, float dy);
616643ead5SAxel Dörfler			BRect				OffsetByCopy(BPoint offset) const;
626643ead5SAxel Dörfler			BRect				OffsetByCopy(float dx, float dy) const;
6389208c77SStephan Aßmus			BRect&				OffsetToSelf(BPoint offset);
641f46fc6dSJohn Scipione			BRect&				OffsetToSelf(float x, float y);
656643ead5SAxel Dörfler			BRect				OffsetToCopy(BPoint offset) const;
661f46fc6dSJohn Scipione			BRect				OffsetToCopy(float x, float y) const;
6789208c77SStephan Aßmus
6889208c77SStephan Aßmus	// Comparison
691f46fc6dSJohn Scipione			bool				operator==(BRect other) const;
701f46fc6dSJohn Scipione			bool				operator!=(BRect other) const;
7189208c77SStephan Aßmus
7289208c77SStephan Aßmus	// Intersection and union
731f46fc6dSJohn Scipione			BRect				operator&(BRect other) const;
741f46fc6dSJohn Scipione			BRect				operator|(BRect other) const;
7589208c77SStephan Aßmus
7689208c77SStephan Aßmus			bool				IsValid() const;
7789208c77SStephan Aßmus			float				Width() const;
7889208c77SStephan Aßmus			int32				IntegerWidth() const;
7989208c77SStephan Aßmus			float				Height() const;
8089208c77SStephan Aßmus			int32				IntegerHeight() const;
8189208c77SStephan Aßmus			BSize				Size() const;
8289208c77SStephan Aßmus
831f46fc6dSJohn Scipione			bool				Intersects(BRect rect) const;
841f46fc6dSJohn Scipione			bool				Contains(BPoint point) const;
851f46fc6dSJohn Scipione			bool				Contains(BRect rect) const;
8652a38012Sejakowatz};
87a01d14b2SAxel Dörfler
8852a38012Sejakowatz
8989208c77SStephan Aßmus// #pragma mark - inline definitions
9052a38012Sejakowatz
91a01d14b2SAxel Dörflerinline BPoint
92a01d14b2SAxel DörflerBRect::LeftTop() const
9352a38012Sejakowatz{
942d4087c9SJonas Sundström	return *(const BPoint*)&left;
9552a38012Sejakowatz}
9652a38012Sejakowatz
97253599a2SIngo Weinhold
98a01d14b2SAxel Dörflerinline BPoint
99a01d14b2SAxel DörflerBRect::RightBottom() const
10052a38012Sejakowatz{
1012d4087c9SJonas Sundström	return *(const BPoint*)&right;
10252a38012Sejakowatz}
10352a38012Sejakowatz
104253599a2SIngo Weinhold
105a01d14b2SAxel Dörflerinline BPoint
106a01d14b2SAxel DörflerBRect::LeftBottom() const
10752a38012Sejakowatz{
108a01d14b2SAxel Dörfler	return BPoint(left, bottom);
10952a38012Sejakowatz}
11052a38012Sejakowatz
111253599a2SIngo Weinhold
112a01d14b2SAxel Dörflerinline BPoint
113a01d14b2SAxel DörflerBRect::RightTop() const
11452a38012Sejakowatz{
115a01d14b2SAxel Dörfler	return BPoint(right, top);
11652a38012Sejakowatz}
11752a38012Sejakowatz
118253599a2SIngo Weinhold
119a01d14b2SAxel Dörflerinline
120a01d14b2SAxel DörflerBRect::BRect()
1212d4087c9SJonas Sundström	:
1222d4087c9SJonas Sundström	left(0),
1236643ead5SAxel Dörfler	top(0),
1242d4087c9SJonas Sundström	right(-1),
1252d4087c9SJonas Sundström	bottom(-1)
12652a38012Sejakowatz{
12752a38012Sejakowatz}
12852a38012Sejakowatz
129253599a2SIngo Weinhold
130a01d14b2SAxel Dörflerinline
1311f46fc6dSJohn ScipioneBRect::BRect(float left, float top, float right, float bottom)
1322d4087c9SJonas Sundström	:
1331f46fc6dSJohn Scipione	left(left),
1341f46fc6dSJohn Scipione	top(top),
1351f46fc6dSJohn Scipione	right(right),
1361f46fc6dSJohn Scipione	bottom(bottom)
13752a38012Sejakowatz{
13852a38012Sejakowatz}
13952a38012Sejakowatz
140253599a2SIngo Weinhold
141a01d14b2SAxel Dörflerinline
1421f46fc6dSJohn ScipioneBRect::BRect(const BRect& other)
1432d4087c9SJonas Sundström	:
1441f46fc6dSJohn Scipione	left(other.left),
1451f46fc6dSJohn Scipione	top(other.top),
1461f46fc6dSJohn Scipione	right(other.right),
1471f46fc6dSJohn Scipione	bottom(other.bottom)
14852a38012Sejakowatz{
14952a38012Sejakowatz}
15052a38012Sejakowatz
151253599a2SIngo Weinhold
152a01d14b2SAxel Dörflerinline
153a01d14b2SAxel DörflerBRect::BRect(BPoint leftTop, BPoint rightBottom)
1542d4087c9SJonas Sundström	:
1552d4087c9SJonas Sundström	left(leftTop.x),
1562d4087c9SJonas Sundström	top(leftTop.y),
1572d4087c9SJonas Sundström	right(rightBottom.x),
1582d4087c9SJonas Sundström	bottom(rightBottom.y)
15952a38012Sejakowatz{
16052a38012Sejakowatz}
16152a38012Sejakowatz
162253599a2SIngo Weinhold
163a19a660aSIngo Weinholdinline
164a19a660aSIngo WeinholdBRect::BRect(BPoint leftTop, BSize size)
1652d4087c9SJonas Sundström	:
1662d4087c9SJonas Sundström	left(leftTop.x),
1672d4087c9SJonas Sundström	top(leftTop.y),
1682d4087c9SJonas Sundström	right(leftTop.x + size.width),
1692d4087c9SJonas Sundström	bottom(leftTop.y + size.height)
1702d4087c9SJonas Sundström{
1712d4087c9SJonas Sundström}
1722d4087c9SJonas Sundström
1732d4087c9SJonas Sundström
1742d4087c9SJonas Sundströminline
1752d4087c9SJonas SundströmBRect::BRect(float side)
1762d4087c9SJonas Sundström	:
1772d4087c9SJonas Sundström	left(0),
1782d4087c9SJonas Sundström	top(0),
1792d4087c9SJonas Sundström	right(side - 1),
1802d4087c9SJonas Sundström	bottom(side - 1)
181a19a660aSIngo Weinhold{
182a19a660aSIngo Weinhold}
183a19a660aSIngo Weinhold
184a19a660aSIngo Weinhold
18589208c77SStephan Aßmusinline BRect&
1861f46fc6dSJohn ScipioneBRect::operator=(const BRect& other)
18752a38012Sejakowatz{
1881f46fc6dSJohn Scipione	left = other.left;
1891f46fc6dSJohn Scipione	top = other.top;
1901f46fc6dSJohn Scipione	right = other.right;
1911f46fc6dSJohn Scipione	bottom = other.bottom;
19252a38012Sejakowatz	return *this;
19352a38012Sejakowatz}
19452a38012Sejakowatz
195253599a2SIngo Weinhold
196a01d14b2SAxel Dörflerinline void
1971f46fc6dSJohn ScipioneBRect::Set(float left, float top, float right, float bottom)
19852a38012Sejakowatz{
1991f46fc6dSJohn Scipione	this->left = left;
2001f46fc6dSJohn Scipione	this->top = top;
2011f46fc6dSJohn Scipione	this->right = right;
2021f46fc6dSJohn Scipione	this->bottom = bottom;
20352a38012Sejakowatz}
20452a38012Sejakowatz
205253599a2SIngo Weinhold
206a01d14b2SAxel Dörflerinline bool
207a01d14b2SAxel DörflerBRect::IsValid() const
20852a38012Sejakowatz{
209a01d14b2SAxel Dörfler	return left <= right && top <= bottom;
21052a38012Sejakowatz}
21152a38012Sejakowatz
212253599a2SIngo Weinhold
213a01d14b2SAxel Dörflerinline int32
214a01d14b2SAxel DörflerBRect::IntegerWidth() const
21552a38012Sejakowatz{
216a01d14b2SAxel Dörfler	return (int32)ceil(right - left);
21752a38012Sejakowatz}
21852a38012Sejakowatz
219253599a2SIngo Weinhold
220a01d14b2SAxel Dörflerinline float
221a01d14b2SAxel DörflerBRect::Width() const
22252a38012Sejakowatz{
223a01d14b2SAxel Dörfler	return right - left;
22452a38012Sejakowatz}
22552a38012Sejakowatz
226253599a2SIngo Weinhold
227a01d14b2SAxel Dörflerinline int32
228a01d14b2SAxel DörflerBRect::IntegerHeight() const
22952a38012Sejakowatz{
230a01d14b2SAxel Dörfler	return (int32)ceil(bottom - top);
23152a38012Sejakowatz}
23252a38012Sejakowatz
233253599a2SIngo Weinhold
234a01d14b2SAxel Dörflerinline float
235a01d14b2SAxel DörflerBRect::Height() const
23652a38012Sejakowatz{
237a01d14b2SAxel Dörfler	return bottom - top;
23852a38012Sejakowatz}
23952a38012Sejakowatz
2402d4087c9SJonas Sundström
241a19a660aSIngo Weinholdinline BSize
242a19a660aSIngo WeinholdBRect::Size() const
243a19a660aSIngo Weinhold{
244a19a660aSIngo Weinhold	return BSize(right - left, bottom - top);
245a19a660aSIngo Weinhold}
246a19a660aSIngo Weinhold
247a19a660aSIngo Weinhold
24852a38012Sejakowatz#endif	// _RECT_H
249