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
54 * @param [in] y y coordinate
55 * @param [in] width width
56 * @param [in] height height
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);
204 T x; ///< X position of the rectangle
205 T y; ///< Y position of the rectangle
206 T width; ///< width of the rectangle
207 T height; ///< height of the rectangle
211 * @brief Equality operator.
213 * @param[in] lhs First operand
214 * @param[in] rhs Second operand
215 * @return true if boxes are exactly same
217 template< typename T >
218 inline bool operator==( const Rect<T>& lhs, const Rect<T>& rhs )
220 return ( lhs.x == rhs.x )&&
222 ( lhs.width == rhs.width )&&
223 ( lhs.height == rhs.height );
227 * @brief Inequality operator.
229 * @param[in] lhs The first rectangle
230 * @param[in] rhs The second rectangle
231 * @return true if rectangles are not identical
233 template< typename T >
234 inline bool operator!=( const Rect<T>& lhs, const Rect<T>& rhs )
236 return !(lhs == rhs);
240 * @brief Equality operator specialization for float.
242 * @param[in] lhs The first rectangle
243 * @param[in] rhs The second rectangle
244 * @return true if rectangles are exactly same
247 inline bool operator==( const Rect<float>& lhs, const Rect<float>& rhs )
249 return ( fabsf( lhs.x - rhs.x ) < GetRangedEpsilon(lhs.x, rhs.x) )&&
250 ( fabsf( lhs.y - rhs.y ) < GetRangedEpsilon(lhs.y, rhs.y) )&&
251 ( fabsf( lhs.width - rhs.width ) < GetRangedEpsilon(lhs.width, rhs.width) )&&
252 ( fabsf( lhs.height - rhs.height ) < GetRangedEpsilon(lhs.height, rhs.height) );
256 * @brief IsEmpty specialization for float.
258 * @return true if the rectangle has zero size.
261 inline bool Rect<float>::IsEmpty() const
263 return (fabsf(width) <= GetRangedEpsilon(width, width)
265 fabsf(height) <= GetRangedEpsilon(height, height));
269 * @brief Convert the value of the rectangle into a string and insert in to an output stream.
271 * @param [in] stream The output stream operator.
272 * @param [in] rectangle the rectangle to output
273 * @return The output stream operator.
275 inline std::ostream& operator<< (std::ostream& stream, const Rect<int>& rectangle)
277 return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]";
281 * @brief Convert the value of the rectangle into a string and insert in to an output stream.
283 * @param [in] stream The output stream operator.
284 * @param [in] rectangle the rectangle to output
285 * @return The output stream operator.
287 inline std::ostream& operator<< (std::ostream& stream, const Rect<unsigned int>& rectangle)
289 return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]";
293 * @brief Convert the value of the rectangle into a string and insert in to an output stream.
295 * @param [in] stream The output stream operator.
296 * @param [in] rectangle the rectangle to output
297 * @return The output stream operator.
299 inline std::ostream& operator<< (std::ostream& stream, const Rect<float>& rectangle)
301 return stream << "[" << rectangle.x << ", " << rectangle.y << ", " << rectangle.width << ", " << rectangle.height << "]";
308 #endif // __DALI_RECT_H__