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.
40 template< typename T = float >
61 * @param [in] x x coordinate (or left)
62 * @param [in] y y coordinate (or right)
63 * @param [in] width width (or bottom)
64 * @param [in] height height (or top)
66 Rect(T x, T y, T width, T height)
75 * @brief Copy constructor.
78 * @param [in] rhs The original object
80 Rect(const Rect<T>& rhs)
89 * @brief Assignment operator.
92 * @param [in] rhs The original object
93 * @return reference to this
95 Rect<T>& operator= (const Rect<T>& rhs)
109 * @brief Assignment from individual values.
112 * @param[in] newX x coordinate
113 * @param[in] newY y coordinate
114 * @param[in] newWidth width
115 * @param[in] newHeight height
117 void Set(T newX, T newY, T newWidth, T newHeight)
126 * @brief Determines whether or not this Rectangle is empty.
129 * @return true if width or height are zero
138 * @brief Get the left of the rectangle.
141 * @return The left edge of the rectangle
148 * @brief Get the right of the rectangle.
151 * @return The right edge of the rectangle
159 * @brief Get the top of the rectangle.
162 * @return The top of the rectangle
170 * @brief Get the bottom of the rectangle.
173 * @return The bottom of the rectangle
181 * @brief Get the area of the rectangle.
184 * @return The area of the rectangle
188 return width * height;
192 * @brief Determines whether or not this rectangle and the specified rectangle intersect.
195 * @param[in] other The other rectangle to test against this rectangle
196 * @return true if the rectangles intersect
198 bool Intersects(const Rect<T>& other) const
200 return (other.x + other.width) > x &&
201 other.x < (x + width) &&
202 (other.y + other.height) > y &&
203 other.y < (y + height);
207 * @brief Determines whether or not this Rectangle contains the specified rectangle.
210 * @param[in] other The other rectangle to test against this rectangle
211 * @return true if the specified rectangle is contained
213 bool Contains(const Rect<T>& other) const
215 return other.x >= x &&
216 (other.x + other.width) <= (x + width) &&
218 (other.y + other.height) <= (y + height);
225 T x; ///< X position of the rectangle
226 T left; ///< The left value
231 T y; ///< Y position of the rectangle
232 T right; ///< The right value
237 T width; ///< width of the rectangle
238 T bottom; ///< The bottom value
243 T height; ///< height of the rectangle
244 T top; ///< The top value
249 * @brief Equality operator.
252 * @param[in] lhs First operand
253 * @param[in] rhs Second operand
254 * @return true if boxes are exactly same
256 template< typename T >
257 inline bool operator==( const Rect<T>& lhs, const Rect<T>& rhs )
259 return ( lhs.x == rhs.x )&&
261 ( lhs.width == rhs.width )&&
262 ( lhs.height == rhs.height );
266 * @brief Inequality operator.
269 * @param[in] lhs The first rectangle
270 * @param[in] rhs The second rectangle
271 * @return true if rectangles are not identical
273 template< typename T >
274 inline bool operator!=( const Rect<T>& lhs, const Rect<T>& rhs )
276 return !(lhs == rhs);
280 * @brief Equality operator specialization for float.
283 * @param[in] lhs The first rectangle
284 * @param[in] rhs The second rectangle
285 * @return true if rectangles are exactly same
288 inline bool operator==( const Rect<float>& lhs, const Rect<float>& rhs )
290 return ( fabsf( lhs.x - rhs.x ) < GetRangedEpsilon(lhs.x, rhs.x) )&&
291 ( fabsf( lhs.y - rhs.y ) < GetRangedEpsilon(lhs.y, rhs.y) )&&
292 ( fabsf( lhs.width - rhs.width ) < GetRangedEpsilon(lhs.width, rhs.width) )&&
293 ( fabsf( lhs.height - rhs.height ) < GetRangedEpsilon(lhs.height, rhs.height) );
297 * @brief IsEmpty specialization for float.
300 * @return true if the rectangle has zero size.
303 inline bool Rect<float>::IsEmpty() const
305 return (fabsf(width) <= GetRangedEpsilon(width, width)
307 fabsf(height) <= GetRangedEpsilon(height, height));
311 * @brief Convert the value of the rectangle into a string and insert in to an output stream.
314 * @param [in] stream The output stream operator.
315 * @param [in] rectangle the rectangle to output
316 * @return The output stream operator.
318 template< typename T >
319 inline std::ostream& operator<< (std::ostream& stream, const Rect<T>& rectangle)
321 return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]";
329 #endif // __DALI_RECT_H__