1 #ifndef __DALI_RECT_H__
2 #define __DALI_RECT_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
26 #include <dali/public-api/math/math-utils.h>
31 * @addtogroup dali-core-object
36 * @brief Template class to create and operate on rectangles.
39 template< typename T = float >
58 * @param [in] x x coordinate (or left)
59 * @param [in] y y coordinate (or right)
60 * @param [in] width width (or bottom)
61 * @param [in] height height (or top)
63 Rect(T x, T y, T width, T height)
72 * @brief Copy constructor.
74 * @param [in] rhs The original object
76 Rect(const Rect<T>& rhs)
85 * @brief Assignment operator.
87 * @param [in] rhs The original object
88 * @return reference to this
90 Rect<T>& operator= (const Rect<T>& rhs)
104 * @brief Assignment from individual values.
106 * @param[in] newX x coordinate
107 * @param[in] newY y coordinate
108 * @param[in] newWidth width
109 * @param[in] newHeight height
111 void Set(T newX, T newY, T newWidth, T newHeight)
120 * @brief Determines whether or not this Rectangle is empty.
122 * @return true if width or height are zero
131 * @brief Get the left of the rectangle.
133 * @return The left edge of the rectangle
140 * @brief Get the right of the rectangle.
142 * @return The right edge of the rectangle
150 * @brief Get the top of the rectangle.
152 * @return The top of the rectangle
160 * @brief Get the bottom of the rectangle.
162 * @return The bottom of the rectangle
170 * @brief Get the area of the rectangle.
172 * @return The area of the rectangle
176 return width * height;
180 * @brief Determines whether or not this rectangle and the specified rectangle intersect.
182 * @param[in] other The other rectangle to test against this rectangle
183 * @return true if the rectangles intersect
185 bool Intersects(const Rect<T>& other) const
187 return (other.x + other.width) > x &&
188 other.x < (x + width) &&
189 (other.y + other.height) > y &&
190 other.y < (y + height);
194 * @brief Determines whether or not this Rectangle contains the specified rectangle.
196 * @param[in] other The other rectangle to test against this rectangle
197 * @return true if the specified rectangle is contained
199 bool Contains(const Rect<T>& other) const
201 return other.x >= x &&
202 (other.x + other.width) <= (x + width) &&
204 (other.y + other.height) <= (y + height);
211 T x; ///< X position of the rectangle
212 T left; ///< The left value
217 T y; ///< Y position of the rectangle
218 T right; ///< The right value
223 T width; ///< width of the rectangle
224 T bottom; ///< The bottom value
229 T height; ///< height of the rectangle
230 T top; ///< The top value
235 * @brief Equality operator.
237 * @param[in] lhs First operand
238 * @param[in] rhs Second operand
239 * @return true if boxes are exactly same
241 template< typename T >
242 inline bool operator==( const Rect<T>& lhs, const Rect<T>& rhs )
244 return ( lhs.x == rhs.x )&&
246 ( lhs.width == rhs.width )&&
247 ( lhs.height == rhs.height );
251 * @brief Inequality operator.
253 * @param[in] lhs The first rectangle
254 * @param[in] rhs The second rectangle
255 * @return true if rectangles are not identical
257 template< typename T >
258 inline bool operator!=( const Rect<T>& lhs, const Rect<T>& rhs )
260 return !(lhs == rhs);
264 * @brief Equality operator specialization for float.
266 * @param[in] lhs The first rectangle
267 * @param[in] rhs The second rectangle
268 * @return true if rectangles are exactly same
271 inline bool operator==( const Rect<float>& lhs, const Rect<float>& rhs )
273 return ( fabsf( lhs.x - rhs.x ) < GetRangedEpsilon(lhs.x, rhs.x) )&&
274 ( fabsf( lhs.y - rhs.y ) < GetRangedEpsilon(lhs.y, rhs.y) )&&
275 ( fabsf( lhs.width - rhs.width ) < GetRangedEpsilon(lhs.width, rhs.width) )&&
276 ( fabsf( lhs.height - rhs.height ) < GetRangedEpsilon(lhs.height, rhs.height) );
280 * @brief IsEmpty specialization for float.
282 * @return true if the rectangle has zero size.
285 inline bool Rect<float>::IsEmpty() const
287 return (fabsf(width) <= GetRangedEpsilon(width, width)
289 fabsf(height) <= GetRangedEpsilon(height, height));
293 * @brief Convert the value of the rectangle into a string and insert in to an output stream.
295 * @param [in] stream The output stream operator.
296 * @param [in] rectangle the rectangle to output
297 * @return The output stream operator.
299 template< typename T >
300 inline std::ostream& operator<< (std::ostream& stream, const Rect<T>& rectangle)
302 return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]";
310 #endif // __DALI_RECT_H__