5 * Copyright (c) 2020 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_math
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 Conversion constructor from Vector4.
78 * @param[in] vec4 Vector4 to convert from
80 Rect( const Vector4& vec4 )
89 * @brief Copy constructor.
92 * @param[in] rhs The original object
94 Rect(const Rect<T>& rhs)
103 * @brief Assignment operator.
106 * @param[in] rhs The original object
107 * @return Reference to this
109 Rect<T>& operator= (const Rect<T>& rhs)
123 * @brief Assignment operator.
126 * @param[in] vec4 The Vector4 to assign
127 * @return Reference to this
129 Rect<T>& operator= (const Vector4& vec4)
140 * @brief Assignment from individual values.
143 * @param[in] newX X coordinate
144 * @param[in] newY Y coordinate
145 * @param[in] newWidth Width
146 * @param[in] newHeight Height
148 void Set(T newX, T newY, T newWidth, T newHeight)
157 * @brief Determines whether or not this Rectangle is empty.
160 * @return True if width or height are zero
169 * @brief Gets the left of the rectangle.
172 * @return The left edge of the rectangle
179 * @brief Gets the right of the rectangle.
182 * @return The right edge of the rectangle
190 * @brief Gets the top of the rectangle.
193 * @return The top of the rectangle
201 * @brief Gets the bottom of the rectangle.
204 * @return The bottom of the rectangle
212 * @brief Gets the area of the rectangle.
215 * @return The area of the rectangle
219 return width * height;
223 * @brief Determines whether or not this rectangle and the specified rectangle intersect.
226 * @param[in] other The other rectangle to test against this rectangle
227 * @return True if the rectangles intersect
229 bool Intersects(const Rect<T>& other) const
231 return (other.x + other.width) > x &&
232 other.x < (x + width) &&
233 (other.y + other.height) > y &&
234 other.y < (y + height);
238 * @brief Determines whether or not this Rectangle contains the specified rectangle.
241 * @param[in] other The other rectangle to test against this rectangle
242 * @return True if the specified rectangle is contained
244 bool Contains(const Rect<T>& other) const
246 return other.x >= x &&
247 (other.x + other.width) <= (x + width) &&
249 (other.y + other.height) <= (y + height);
256 T x; ///< X position of the rectangle
257 T left; ///< The left value
262 T y; ///< Y position of the rectangle
263 T right; ///< The right value
268 T width; ///< width of the rectangle
269 T bottom; ///< The bottom value
274 T height; ///< height of the rectangle
275 T top; ///< The top value
280 * @brief Equality operator.
283 * @param[in] lhs First operand
284 * @param[in] rhs Second operand
285 * @return True if boxes are exactly same
287 template< typename T >
288 inline bool operator==( const Rect<T>& lhs, const Rect<T>& rhs )
290 return ( lhs.x == rhs.x )&&
292 ( lhs.width == rhs.width )&&
293 ( lhs.height == rhs.height );
297 * @brief Inequality operator.
300 * @param[in] lhs The first rectangle
301 * @param[in] rhs The second rectangle
302 * @return True if rectangles are not identical
304 template< typename T >
305 inline bool operator!=( const Rect<T>& lhs, const Rect<T>& rhs )
307 return !(lhs == rhs);
311 * @brief Equality operator specialization for float.
314 * @param[in] lhs The first rectangle
315 * @param[in] rhs The second rectangle
316 * @return True if rectangles are exactly same
319 inline bool operator==( const Rect<float>& lhs, const Rect<float>& rhs )
321 return ( fabsf( lhs.x - rhs.x ) < GetRangedEpsilon(lhs.x, rhs.x) )&&
322 ( fabsf( lhs.y - rhs.y ) < GetRangedEpsilon(lhs.y, rhs.y) )&&
323 ( fabsf( lhs.width - rhs.width ) < GetRangedEpsilon(lhs.width, rhs.width) )&&
324 ( fabsf( lhs.height - rhs.height ) < GetRangedEpsilon(lhs.height, rhs.height) );
328 * @brief IsEmpty specialization for float.
331 * @return True if the rectangle has zero size
334 inline bool Rect<float>::IsEmpty() const
336 return (fabsf(width) <= GetRangedEpsilon(width, width)
338 fabsf(height) <= GetRangedEpsilon(height, height));
342 * @brief Converts the value of the rectangle into a string and insert in to an output stream.
345 * @param[in] stream The output stream operator
346 * @param[in] rectangle the rectangle to output
347 * @return The output stream operator
349 template< typename T >
350 inline std::ostream& operator<< (std::ostream& stream, const Rect<T>& rectangle)
352 return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]";
360 #endif // DALI_RECT_H