|
|
|||
File indexing completed on 2026-06-02 08:58:13
0001 //////////////////////////////////////////////////////////// 0002 // 0003 // SFML - Simple and Fast Multimedia Library 0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org) 0005 // 0006 // This software is provided 'as-is', without any express or implied warranty. 0007 // In no event will the authors be held liable for any damages arising from the use of this software. 0008 // 0009 // Permission is granted to anyone to use this software for any purpose, 0010 // including commercial applications, and to alter it and redistribute it freely, 0011 // subject to the following restrictions: 0012 // 0013 // 1. The origin of this software must not be misrepresented; 0014 // you must not claim that you wrote the original software. 0015 // If you use this software in a product, an acknowledgment 0016 // in the product documentation would be appreciated but is not required. 0017 // 0018 // 2. Altered source versions must be plainly marked as such, 0019 // and must not be misrepresented as being the original software. 0020 // 0021 // 3. This notice may not be removed or altered from any source distribution. 0022 // 0023 //////////////////////////////////////////////////////////// 0024 0025 #ifndef SFML_RECT_HPP 0026 #define SFML_RECT_HPP 0027 0028 //////////////////////////////////////////////////////////// 0029 // Headers 0030 //////////////////////////////////////////////////////////// 0031 #include <SFML/System/Vector2.hpp> 0032 #include <algorithm> 0033 0034 0035 namespace sf 0036 { 0037 //////////////////////////////////////////////////////////// 0038 /// \brief Utility class for manipulating 2D axis aligned rectangles 0039 /// 0040 //////////////////////////////////////////////////////////// 0041 template <typename T> 0042 class Rect 0043 { 0044 public: 0045 0046 //////////////////////////////////////////////////////////// 0047 /// \brief Default constructor 0048 /// 0049 /// Creates an empty rectangle (it is equivalent to calling 0050 /// Rect(0, 0, 0, 0)). 0051 /// 0052 //////////////////////////////////////////////////////////// 0053 Rect(); 0054 0055 //////////////////////////////////////////////////////////// 0056 /// \brief Construct the rectangle from its coordinates 0057 /// 0058 /// Be careful, the last two parameters are the width 0059 /// and height, not the right and bottom coordinates! 0060 /// 0061 /// \param rectLeft Left coordinate of the rectangle 0062 /// \param rectTop Top coordinate of the rectangle 0063 /// \param rectWidth Width of the rectangle 0064 /// \param rectHeight Height of the rectangle 0065 /// 0066 //////////////////////////////////////////////////////////// 0067 Rect(T rectLeft, T rectTop, T rectWidth, T rectHeight); 0068 0069 //////////////////////////////////////////////////////////// 0070 /// \brief Construct the rectangle from position and size 0071 /// 0072 /// Be careful, the last parameter is the size, 0073 /// not the bottom-right corner! 0074 /// 0075 /// \param position Position of the top-left corner of the rectangle 0076 /// \param size Size of the rectangle 0077 /// 0078 //////////////////////////////////////////////////////////// 0079 Rect(const Vector2<T>& position, const Vector2<T>& size); 0080 0081 //////////////////////////////////////////////////////////// 0082 /// \brief Construct the rectangle from another type of rectangle 0083 /// 0084 /// This constructor doesn't replace the copy constructor, 0085 /// it's called only when U != T. 0086 /// A call to this constructor will fail to compile if U 0087 /// is not convertible to T. 0088 /// 0089 /// \param rectangle Rectangle to convert 0090 /// 0091 //////////////////////////////////////////////////////////// 0092 template <typename U> 0093 explicit Rect(const Rect<U>& rectangle); 0094 0095 //////////////////////////////////////////////////////////// 0096 /// \brief Check if a point is inside the rectangle's area 0097 /// 0098 /// This check is non-inclusive. If the point lies on the 0099 /// edge of the rectangle, this function will return false. 0100 /// 0101 /// \param x X coordinate of the point to test 0102 /// \param y Y coordinate of the point to test 0103 /// 0104 /// \return True if the point is inside, false otherwise 0105 /// 0106 /// \see intersects 0107 /// 0108 //////////////////////////////////////////////////////////// 0109 bool contains(T x, T y) const; 0110 0111 //////////////////////////////////////////////////////////// 0112 /// \brief Check if a point is inside the rectangle's area 0113 /// 0114 /// This check is non-inclusive. If the point lies on the 0115 /// edge of the rectangle, this function will return false. 0116 /// 0117 /// \param point Point to test 0118 /// 0119 /// \return True if the point is inside, false otherwise 0120 /// 0121 /// \see intersects 0122 /// 0123 //////////////////////////////////////////////////////////// 0124 bool contains(const Vector2<T>& point) const; 0125 0126 //////////////////////////////////////////////////////////// 0127 /// \brief Check the intersection between two rectangles 0128 /// 0129 /// \param rectangle Rectangle to test 0130 /// 0131 /// \return True if rectangles overlap, false otherwise 0132 /// 0133 /// \see contains 0134 /// 0135 //////////////////////////////////////////////////////////// 0136 bool intersects(const Rect<T>& rectangle) const; 0137 0138 //////////////////////////////////////////////////////////// 0139 /// \brief Check the intersection between two rectangles 0140 /// 0141 /// This overload returns the overlapped rectangle in the 0142 /// \a intersection parameter. 0143 /// 0144 /// \param rectangle Rectangle to test 0145 /// \param intersection Rectangle to be filled with the intersection 0146 /// 0147 /// \return True if rectangles overlap, false otherwise 0148 /// 0149 /// \see contains 0150 /// 0151 //////////////////////////////////////////////////////////// 0152 bool intersects(const Rect<T>& rectangle, Rect<T>& intersection) const; 0153 0154 //////////////////////////////////////////////////////////// 0155 /// \brief Get the position of the rectangle's top-left corner 0156 /// 0157 /// \return Position of rectangle 0158 /// 0159 /// \see getSize 0160 /// 0161 //////////////////////////////////////////////////////////// 0162 sf::Vector2<T> getPosition() const; 0163 0164 //////////////////////////////////////////////////////////// 0165 /// \brief Get the size of the rectangle 0166 /// 0167 /// \return Size of rectangle 0168 /// 0169 /// \see getPosition 0170 /// 0171 //////////////////////////////////////////////////////////// 0172 sf::Vector2<T> getSize() const; 0173 0174 //////////////////////////////////////////////////////////// 0175 // Member data 0176 //////////////////////////////////////////////////////////// 0177 T left; //!< Left coordinate of the rectangle 0178 T top; //!< Top coordinate of the rectangle 0179 T width; //!< Width of the rectangle 0180 T height; //!< Height of the rectangle 0181 }; 0182 0183 //////////////////////////////////////////////////////////// 0184 /// \relates Rect 0185 /// \brief Overload of binary operator == 0186 /// 0187 /// This operator compares strict equality between two rectangles. 0188 /// 0189 /// \param left Left operand (a rectangle) 0190 /// \param right Right operand (a rectangle) 0191 /// 0192 /// \return True if \a left is equal to \a right 0193 /// 0194 //////////////////////////////////////////////////////////// 0195 template <typename T> 0196 bool operator ==(const Rect<T>& left, const Rect<T>& right); 0197 0198 //////////////////////////////////////////////////////////// 0199 /// \relates Rect 0200 /// \brief Overload of binary operator != 0201 /// 0202 /// This operator compares strict difference between two rectangles. 0203 /// 0204 /// \param left Left operand (a rectangle) 0205 /// \param right Right operand (a rectangle) 0206 /// 0207 /// \return True if \a left is not equal to \a right 0208 /// 0209 //////////////////////////////////////////////////////////// 0210 template <typename T> 0211 bool operator !=(const Rect<T>& left, const Rect<T>& right); 0212 0213 #include <SFML/Graphics/Rect.inl> 0214 0215 // Create typedefs for the most common types 0216 typedef Rect<int> IntRect; 0217 typedef Rect<float> FloatRect; 0218 0219 } // namespace sf 0220 0221 0222 #endif // SFML_RECT_HPP 0223 0224 0225 //////////////////////////////////////////////////////////// 0226 /// \class sf::Rect 0227 /// \ingroup graphics 0228 /// 0229 /// A rectangle is defined by its top-left corner and its size. 0230 /// It is a very simple class defined for convenience, so 0231 /// its member variables (left, top, width and height) are public 0232 /// and can be accessed directly, just like the vector classes 0233 /// (Vector2 and Vector3). 0234 /// 0235 /// To keep things simple, sf::Rect doesn't define 0236 /// functions to emulate the properties that are not directly 0237 /// members (such as right, bottom, center, etc.), it rather 0238 /// only provides intersection functions. 0239 /// 0240 /// sf::Rect uses the usual rules for its boundaries: 0241 /// \li The left and top edges are included in the rectangle's area 0242 /// \li The right (left + width) and bottom (top + height) edges are excluded from the rectangle's area 0243 /// 0244 /// This means that sf::IntRect(0, 0, 1, 1) and sf::IntRect(1, 1, 1, 1) 0245 /// don't intersect. 0246 /// 0247 /// sf::Rect is a template and may be used with any numeric type, but 0248 /// for simplicity the instantiations used by SFML are typedef'd: 0249 /// \li sf::Rect<int> is sf::IntRect 0250 /// \li sf::Rect<float> is sf::FloatRect 0251 /// 0252 /// So that you don't have to care about the template syntax. 0253 /// 0254 /// Usage example: 0255 /// \code 0256 /// // Define a rectangle, located at (0, 0) with a size of 20x5 0257 /// sf::IntRect r1(0, 0, 20, 5); 0258 /// 0259 /// // Define another rectangle, located at (4, 2) with a size of 18x10 0260 /// sf::Vector2i position(4, 2); 0261 /// sf::Vector2i size(18, 10); 0262 /// sf::IntRect r2(position, size); 0263 /// 0264 /// // Test intersections with the point (3, 1) 0265 /// bool b1 = r1.contains(3, 1); // true 0266 /// bool b2 = r2.contains(3, 1); // false 0267 /// 0268 /// // Test the intersection between r1 and r2 0269 /// sf::IntRect result; 0270 /// bool b3 = r1.intersects(r2, result); // true 0271 /// // result == (4, 2, 16, 3) 0272 /// \endcode 0273 /// 0274 ////////////////////////////////////////////////////////////
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|