5 * Copyright (c) 2019 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 Gets the left of the rectangle.
141 * @return The left edge of the rectangle
148 * @brief Gets the right of the rectangle.
151 * @return The right edge of the rectangle
159 * @brief Gets the top of the rectangle.
162 * @return The top of the rectangle
170 * @brief Gets the bottom of the rectangle.
173 * @return The bottom of the rectangle
181 * @brief Gets 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 Converts 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