xref: /haiku/headers/os/interface/Rect.h (revision 51978af14a173e7fae0563b562be5603bc652aeb)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		Rect.h
23 //	Author:			Frans van Nispen (xlr8@tref.nl)
24 //	Description:	BRect represents a rectangular area.
25 //------------------------------------------------------------------------------
26 
27 #ifndef	_RECT_H
28 #define	_RECT_H
29 
30 // Standard Includes -----------------------------------------------------------
31 #include <math.h>
32 
33 // System Includes -------------------------------------------------------------
34 #include <SupportDefs.h>
35 #include <Point.h>
36 
37 // Project Includes ------------------------------------------------------------
38 
39 // Local Includes --------------------------------------------------------------
40 
41 // Local Defines ---------------------------------------------------------------
42 
43 // Globals ---------------------------------------------------------------------
44 
45 
46 // BRect class -----------------------------------------------------------------
47 class BRect {
48 public:
49 	float	left;
50 	float	top;
51 	float	right;
52 	float	bottom;
53 
54 	BRect();
55 	BRect(const BRect &r);
56 	BRect(float l, float t, float r, float b);
57 	BRect(BPoint lt, BPoint rb);
58 
59 	BRect	&operator=(const BRect &r);
60 	void	Set(float l, float t, float r, float b);
61 
62 	void	PrintToStream() const;
63 
64 	BPoint	LeftTop() const;
65 	BPoint	RightBottom() const;
66 	BPoint	LeftBottom() const;
67 	BPoint	RightTop() const;
68 
69 	void	SetLeftTop(const BPoint p);
70 	void	SetRightBottom(const BPoint p);
71 	void	SetLeftBottom(const BPoint p);
72 	void	SetRightTop(const BPoint p);
73 
74 	// transformation
75 	void	InsetBy(BPoint p);
76 	void	InsetBy(float dx, float dy);
77 	void	OffsetBy(BPoint p);
78 	void	OffsetBy(float dx, float dy);
79 	void	OffsetTo(BPoint p);
80 	void	OffsetTo(float x, float y);
81 
82 	// expression transformations
83 	BRect &	InsetBySelf(BPoint);
84 	BRect &	InsetBySelf(float dx, float dy);
85 	BRect	InsetByCopy(BPoint);
86 	BRect	InsetByCopy(float dx, float dy);
87 	BRect &	OffsetBySelf(BPoint);
88 	BRect &	OffsetBySelf(float dx, float dy);
89 	BRect	OffsetByCopy(BPoint);
90 	BRect	OffsetByCopy(float dx, float dy);
91 	BRect &	OffsetToSelf(BPoint);
92 	BRect &	OffsetToSelf(float dx, float dy);
93 	BRect	OffsetToCopy(BPoint);
94 	BRect	OffsetToCopy(float dx, float dy);
95 
96 	// comparison
97 	bool	operator==(BRect r) const;
98 	bool	operator!=(BRect r) const;
99 
100 	// intersection and union
101 	BRect	operator&(BRect r) const;
102 	BRect	operator|(BRect r) const;
103 
104 	bool	Intersects(BRect r) const;
105 	bool	IsValid() const;
106 	float	Width() const;
107 	int32	IntegerWidth() const;
108 	float	Height() const;
109 	int32	IntegerHeight() const;
110 	bool	Contains(BPoint p) const;
111 	bool	Contains(BRect r) const;
112 
113 };
114 //------------------------------------------------------------------------------
115 
116 // inline definitions ----------------------------------------------------------
117 
118 inline BPoint BRect::LeftTop() const
119 {
120 	return(*((const BPoint*)&left));
121 }
122 
123 inline BPoint BRect::RightBottom() const
124 {
125 	return(*((const BPoint*)&right));
126 }
127 
128 inline BPoint BRect::LeftBottom() const
129 {
130 	return(BPoint(left, bottom));
131 }
132 
133 inline BPoint BRect::RightTop() const
134 {
135 	return(BPoint(right, top));
136 }
137 
138 inline BRect::BRect()
139 {
140 	top = left = 0;
141 	bottom = right = -1;
142 }
143 
144 inline BRect::BRect(float l, float t, float r, float b)
145 {
146 	left = l;
147 	top = t;
148 	right = r;
149 	bottom = b;
150 }
151 
152 inline BRect::BRect(const BRect &r)
153 {
154 	left = r.left;
155 	top = r.top;
156 	right = r.right;
157 	bottom = r.bottom;
158 }
159 
160 inline BRect::BRect(BPoint leftTop, BPoint rightBottom)
161 {
162 	left = leftTop.x;
163 	top = leftTop.y;
164 	right = rightBottom.x;
165 	bottom = rightBottom.y;
166 }
167 
168 inline BRect &BRect::operator=(const BRect& from)
169 {
170 	left = from.left;
171 	top = from.top;
172 	right = from.right;
173 	bottom = from.bottom;
174 	return *this;
175 }
176 
177 inline void BRect::Set(float l, float t, float r, float b)
178 {
179 	left = l;
180 	top = t;
181 	right = r;
182 	bottom = b;
183 }
184 
185 inline bool BRect::IsValid() const
186 {
187 	if (left <= right && top <= bottom)
188 		return true;
189 	else
190 		return false;
191 }
192 
193 inline int32 BRect::IntegerWidth() const
194 {
195 	return((int32)ceil(right - left));
196 }
197 
198 inline float BRect::Width() const
199 {
200 	return(right - left);
201 }
202 
203 inline int32 BRect::IntegerHeight() const
204 {
205 	return((int32)ceil(bottom - top));
206 }
207 
208 inline float BRect::Height() const
209 {
210 	return(bottom - top);
211 }
212 
213 //------------------------------------------------------------------------------
214 
215 #endif	// _RECT_H
216 
217 /*
218  * $Log $
219  *
220  * $Id  $
221  *
222  */
223 
224