1 #ifndef __DALI_RECT_H__
2 #define __DALI_RECT_H__
5 * Copyright (c) 2014 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.
25 #include <dali/public-api/math/math-utils.h>
31 * @brief Template class to create and operate on rectangles.
33 template< typename T = float >
52 * @param [in] x x coordinate
53 * @param [in] y y coordinate
54 * @param [in] width width
55 * @param [in] height height
57 Rect(T x, T y, T width, T height)
66 * @brief Copy constructor.
68 * @param [in] rhs The original object
70 Rect(const Rect<T>& rhs)
79 * @brief Assignment operator.
81 * @param [in] rhs The original object
82 * @return reference to this
84 Rect<T>& operator= (const Rect<T>& rhs)
98 * @brief Assignment from individual values.
100 * @param[in] newX x coordinate
101 * @param[in] newY y coordinate
102 * @param[in] newWidth width
103 * @param[in] newHeight height
105 void Set(T newX, T newY, T newWidth, T newHeight)
114 * @brief Determines whether or not this Rectangle is empty.
116 * @return true if width or height are zero
125 * @brief Get the left of the rectangle.
127 * @return The left edge of the rectangle
134 * @brief Get the right of the rectangle.
136 * @return The right edge of the rectangle
144 * @brief Get the top of the rectangle.
146 * @return The top of the rectangle
154 * @brief Get the bottom of the rectangle.
156 * @return The bottom of the rectangle
164 * @brief Get the area of the rectangle.
166 * @return The area of the rectangle
170 return width * height;
174 * @brief Determines whether or not this rectangle and the specified rectangle intersect.
176 * @param[in] other The other rectangle to test against this rectangle
177 * @return true if the rectangles intersect
179 bool Intersects(const Rect<T>& other) const
181 return (other.x + other.width) > x &&
182 other.x < (x + width) &&
183 (other.y + other.height) > y &&
184 other.y < (y + height);
188 * @brief Determines whether or not this Rectangle contains the specified rectangle.
190 * @param[in] other The other rectangle to test against this rectangle
191 * @return true if the specified rectangle is contained
193 bool Contains(const Rect<T>& other) const
195 return other.x >= x &&
196 (other.x + other.width) <= (x + width) &&
198 (other.y + other.height) <= (y + height);
203 T x; ///< X position of the rectangle
204 T y; ///< Y position of the rectangle
205 T width; ///< width of the rectangle
206 T height; ///< height of the rectangle
210 * @brief Equality operator.
212 * @param[in] lhs First operand
213 * @param[in] rhs Second operand
214 * @return true if boxes are exactly same
216 template< typename T >
217 inline bool operator==( const Rect<T>& lhs, const Rect<T>& rhs )
219 return ( lhs.x == rhs.x )&&
221 ( lhs.width == rhs.width )&&
222 ( lhs.height == rhs.height );
226 * @brief Inequality operator.
228 * @param[in] lhs The first rectangle
229 * @param[in] rhs The second rectangle
230 * @return true if rectangles are not identical
232 template< typename T >
233 inline bool operator!=( const Rect<T>& lhs, const Rect<T>& rhs )
235 return !(lhs == rhs);
239 * @brief Equality operator specialization for float.
241 * @param[in] lhs The first rectangle
242 * @param[in] rhs The second rectangle
243 * @return true if rectangles are exactly same
246 inline bool operator==( const Rect<float>& lhs, const Rect<float>& rhs )
248 return ( fabsf( lhs.x - rhs.x ) < GetRangedEpsilon(lhs.x, rhs.x) )&&
249 ( fabsf( lhs.y - rhs.y ) < GetRangedEpsilon(lhs.y, rhs.y) )&&
250 ( fabsf( lhs.width - rhs.width ) < GetRangedEpsilon(lhs.width, rhs.width) )&&
251 ( fabsf( lhs.height - rhs.height ) < GetRangedEpsilon(lhs.height, rhs.height) );
255 * @brief IsEmpty specialization for float.
257 * @return true if the rectangle has zero size.
260 inline bool Rect<float>::IsEmpty() const
262 return (fabsf(width) <= GetRangedEpsilon(width, width)
264 fabsf(height) <= GetRangedEpsilon(height, height));
269 #endif // __DALI_RECT_H__