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 from individual values.
126 * @param[in] newX X coordinate
127 * @param[in] newY Y coordinate
128 * @param[in] newWidth Width
129 * @param[in] newHeight Height
131 void Set(T newX, T newY, T newWidth, T newHeight)
140 * @brief Determines whether or not this Rectangle is empty.
143 * @return True if width or height are zero
152 * @brief Gets the left of the rectangle.
155 * @return The left edge of the rectangle
162 * @brief Gets the right of the rectangle.
165 * @return The right edge of the rectangle
173 * @brief Gets the top of the rectangle.
176 * @return The top of the rectangle
184 * @brief Gets the bottom of the rectangle.
187 * @return The bottom of the rectangle
195 * @brief Gets the area of the rectangle.
198 * @return The area of the rectangle
202 return width * height;
206 * @brief Determines whether or not this rectangle and the specified rectangle intersect.
209 * @param[in] other The other rectangle to test against this rectangle
210 * @return True if the rectangles intersect
212 bool Intersects(const Rect<T>& other) const
214 return (other.x + other.width) > x &&
215 other.x < (x + width) &&
216 (other.y + other.height) > y &&
217 other.y < (y + height);
221 * @brief Determines whether or not this Rectangle contains the specified rectangle.
224 * @param[in] other The other rectangle to test against this rectangle
225 * @return True if the specified rectangle is contained
227 bool Contains(const Rect<T>& other) const
229 return other.x >= x &&
230 (other.x + other.width) <= (x + width) &&
232 (other.y + other.height) <= (y + height);
239 T x; ///< X position of the rectangle
240 T left; ///< The left value
245 T y; ///< Y position of the rectangle
246 T right; ///< The right value
251 T width; ///< width of the rectangle
252 T bottom; ///< The bottom value
257 T height; ///< height of the rectangle
258 T top; ///< The top value
263 * @brief Equality operator.
266 * @param[in] lhs First operand
267 * @param[in] rhs Second operand
268 * @return True if boxes are exactly same
270 template< typename T >
271 inline bool operator==( const Rect<T>& lhs, const Rect<T>& rhs )
273 return ( lhs.x == rhs.x )&&
275 ( lhs.width == rhs.width )&&
276 ( lhs.height == rhs.height );
280 * @brief Inequality operator.
283 * @param[in] lhs The first rectangle
284 * @param[in] rhs The second rectangle
285 * @return True if rectangles are not identical
287 template< typename T >
288 inline bool operator!=( const Rect<T>& lhs, const Rect<T>& rhs )
290 return !(lhs == rhs);
294 * @brief Equality operator specialization for float.
297 * @param[in] lhs The first rectangle
298 * @param[in] rhs The second rectangle
299 * @return True if rectangles are exactly same
302 inline bool operator==( const Rect<float>& lhs, const Rect<float>& rhs )
304 return ( fabsf( lhs.x - rhs.x ) < GetRangedEpsilon(lhs.x, rhs.x) )&&
305 ( fabsf( lhs.y - rhs.y ) < GetRangedEpsilon(lhs.y, rhs.y) )&&
306 ( fabsf( lhs.width - rhs.width ) < GetRangedEpsilon(lhs.width, rhs.width) )&&
307 ( fabsf( lhs.height - rhs.height ) < GetRangedEpsilon(lhs.height, rhs.height) );
311 * @brief IsEmpty specialization for float.
314 * @return True if the rectangle has zero size
317 inline bool Rect<float>::IsEmpty() const
319 return (fabsf(width) <= GetRangedEpsilon(width, width)
321 fabsf(height) <= GetRangedEpsilon(height, height));
325 * @brief Converts the value of the rectangle into a string and insert in to an output stream.
328 * @param[in] stream The output stream operator
329 * @param[in] rectangle the rectangle to output
330 * @return The output stream operator
332 template< typename T >
333 inline std::ostream& operator<< (std::ostream& stream, const Rect<T>& rectangle)
335 return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]";
343 #endif // DALI_RECT_H