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.
26 #include <dali/public-api/math/math-utils.h>
32 * @brief Template class to create and operate on rectangles.
34 template< typename T = float >
53 * @param [in] x x coordinate (or left)
54 * @param [in] y y coordinate (or right)
55 * @param [in] width width (or bottom)
56 * @param [in] height height (or top)
58 Rect(T x, T y, T width, T height)
67 * @brief Copy constructor.
69 * @param [in] rhs The original object
71 Rect(const Rect<T>& rhs)
80 * @brief Assignment operator.
82 * @param [in] rhs The original object
83 * @return reference to this
85 Rect<T>& operator= (const Rect<T>& rhs)
99 * @brief Assignment from individual values.
101 * @param[in] newX x coordinate
102 * @param[in] newY y coordinate
103 * @param[in] newWidth width
104 * @param[in] newHeight height
106 void Set(T newX, T newY, T newWidth, T newHeight)
115 * @brief Determines whether or not this Rectangle is empty.
117 * @return true if width or height are zero
126 * @brief Get the left of the rectangle.
128 * @return The left edge of the rectangle
135 * @brief Get the right of the rectangle.
137 * @return The right edge of the rectangle
145 * @brief Get the top of the rectangle.
147 * @return The top of the rectangle
155 * @brief Get the bottom of the rectangle.
157 * @return The bottom of the rectangle
165 * @brief Get the area of the rectangle.
167 * @return The area of the rectangle
171 return width * height;
175 * @brief Determines whether or not this rectangle and the specified rectangle intersect.
177 * @param[in] other The other rectangle to test against this rectangle
178 * @return true if the rectangles intersect
180 bool Intersects(const Rect<T>& other) const
182 return (other.x + other.width) > x &&
183 other.x < (x + width) &&
184 (other.y + other.height) > y &&
185 other.y < (y + height);
189 * @brief Determines whether or not this Rectangle contains the specified rectangle.
191 * @param[in] other The other rectangle to test against this rectangle
192 * @return true if the specified rectangle is contained
194 bool Contains(const Rect<T>& other) const
196 return other.x >= x &&
197 (other.x + other.width) <= (x + width) &&
199 (other.y + other.height) <= (y + height);
206 T x; ///< X position of the rectangle
207 T left; ///< The left value
212 T y; ///< Y position of the rectangle
213 T right; ///< The right value
218 T width; ///< width of the rectangle
219 T bottom; ///< The bottom value
224 T height; ///< height of the rectangle
225 T top; ///< The top value
230 * @brief Equality operator.
232 * @param[in] lhs First operand
233 * @param[in] rhs Second operand
234 * @return true if boxes are exactly same
236 template< typename T >
237 inline bool operator==( const Rect<T>& lhs, const Rect<T>& rhs )
239 return ( lhs.x == rhs.x )&&
241 ( lhs.width == rhs.width )&&
242 ( lhs.height == rhs.height );
246 * @brief Inequality operator.
248 * @param[in] lhs The first rectangle
249 * @param[in] rhs The second rectangle
250 * @return true if rectangles are not identical
252 template< typename T >
253 inline bool operator!=( const Rect<T>& lhs, const Rect<T>& rhs )
255 return !(lhs == rhs);
259 * @brief Equality operator specialization for float.
261 * @param[in] lhs The first rectangle
262 * @param[in] rhs The second rectangle
263 * @return true if rectangles are exactly same
266 inline bool operator==( const Rect<float>& lhs, const Rect<float>& rhs )
268 return ( fabsf( lhs.x - rhs.x ) < GetRangedEpsilon(lhs.x, rhs.x) )&&
269 ( fabsf( lhs.y - rhs.y ) < GetRangedEpsilon(lhs.y, rhs.y) )&&
270 ( fabsf( lhs.width - rhs.width ) < GetRangedEpsilon(lhs.width, rhs.width) )&&
271 ( fabsf( lhs.height - rhs.height ) < GetRangedEpsilon(lhs.height, rhs.height) );
275 * @brief IsEmpty specialization for float.
277 * @return true if the rectangle has zero size.
280 inline bool Rect<float>::IsEmpty() const
282 return (fabsf(width) <= GetRangedEpsilon(width, width)
284 fabsf(height) <= GetRangedEpsilon(height, height));
288 * @brief Convert the value of the rectangle into a string and insert in to an output stream.
290 * @param [in] stream The output stream operator.
291 * @param [in] rectangle the rectangle to output
292 * @return The output stream operator.
294 inline std::ostream& operator<< (std::ostream& stream, const Rect<int>& rectangle)
296 return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]";
300 * @brief Convert the value of the rectangle into a string and insert in to an output stream.
302 * @param [in] stream The output stream operator.
303 * @param [in] rectangle the rectangle to output
304 * @return The output stream operator.
306 inline std::ostream& operator<< (std::ostream& stream, const Rect<unsigned int>& rectangle)
308 return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]";
312 * @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 inline std::ostream& operator<< (std::ostream& stream, const Rect<float>& rectangle)
320 return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]";
327 #endif // __DALI_RECT_H__