Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/SFML/Graphics/Rect.inl is written in an unsupported language. File is not indexed.

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 
0026 ////////////////////////////////////////////////////////////
0027 template <typename T>
0028 Rect<T>::Rect() :
0029 left  (0),
0030 top   (0),
0031 width (0),
0032 height(0)
0033 {
0034 
0035 }
0036 
0037 
0038 ////////////////////////////////////////////////////////////
0039 template <typename T>
0040 Rect<T>::Rect(T rectLeft, T rectTop, T rectWidth, T rectHeight) :
0041 left  (rectLeft),
0042 top   (rectTop),
0043 width (rectWidth),
0044 height(rectHeight)
0045 {
0046 
0047 }
0048 
0049 
0050 ////////////////////////////////////////////////////////////
0051 template <typename T>
0052 Rect<T>::Rect(const Vector2<T>& position, const Vector2<T>& size) :
0053 left  (position.x),
0054 top   (position.y),
0055 width (size.x),
0056 height(size.y)
0057 {
0058 
0059 }
0060 
0061 
0062 ////////////////////////////////////////////////////////////
0063 template <typename T>
0064 template <typename U>
0065 Rect<T>::Rect(const Rect<U>& rectangle) :
0066 left  (static_cast<T>(rectangle.left)),
0067 top   (static_cast<T>(rectangle.top)),
0068 width (static_cast<T>(rectangle.width)),
0069 height(static_cast<T>(rectangle.height))
0070 {
0071 }
0072 
0073 
0074 ////////////////////////////////////////////////////////////
0075 template <typename T>
0076 bool Rect<T>::contains(T x, T y) const
0077 {
0078     // Rectangles with negative dimensions are allowed, so we must handle them correctly
0079 
0080     // Compute the real min and max of the rectangle on both axes
0081     T minX = std::min(left, static_cast<T>(left + width));
0082     T maxX = std::max(left, static_cast<T>(left + width));
0083     T minY = std::min(top, static_cast<T>(top + height));
0084     T maxY = std::max(top, static_cast<T>(top + height));
0085 
0086     return (x >= minX) && (x < maxX) && (y >= minY) && (y < maxY);
0087 }
0088 
0089 
0090 ////////////////////////////////////////////////////////////
0091 template <typename T>
0092 bool Rect<T>::contains(const Vector2<T>& point) const
0093 {
0094     return contains(point.x, point.y);
0095 }
0096 
0097 
0098 ////////////////////////////////////////////////////////////
0099 template <typename T>
0100 bool Rect<T>::intersects(const Rect<T>& rectangle) const
0101 {
0102     Rect<T> intersection;
0103     return intersects(rectangle, intersection);
0104 }
0105 
0106 
0107 ////////////////////////////////////////////////////////////
0108 template <typename T>
0109 bool Rect<T>::intersects(const Rect<T>& rectangle, Rect<T>& intersection) const
0110 {
0111     // Rectangles with negative dimensions are allowed, so we must handle them correctly
0112 
0113     // Compute the min and max of the first rectangle on both axes
0114     T r1MinX = std::min(left, static_cast<T>(left + width));
0115     T r1MaxX = std::max(left, static_cast<T>(left + width));
0116     T r1MinY = std::min(top, static_cast<T>(top + height));
0117     T r1MaxY = std::max(top, static_cast<T>(top + height));
0118 
0119     // Compute the min and max of the second rectangle on both axes
0120     T r2MinX = std::min(rectangle.left, static_cast<T>(rectangle.left + rectangle.width));
0121     T r2MaxX = std::max(rectangle.left, static_cast<T>(rectangle.left + rectangle.width));
0122     T r2MinY = std::min(rectangle.top, static_cast<T>(rectangle.top + rectangle.height));
0123     T r2MaxY = std::max(rectangle.top, static_cast<T>(rectangle.top + rectangle.height));
0124 
0125     // Compute the intersection boundaries
0126     T interLeft   = std::max(r1MinX, r2MinX);
0127     T interTop    = std::max(r1MinY, r2MinY);
0128     T interRight  = std::min(r1MaxX, r2MaxX);
0129     T interBottom = std::min(r1MaxY, r2MaxY);
0130 
0131     // If the intersection is valid (positive non zero area), then there is an intersection
0132     if ((interLeft < interRight) && (interTop < interBottom))
0133     {
0134         intersection = Rect<T>(interLeft, interTop, interRight - interLeft, interBottom - interTop);
0135         return true;
0136     }
0137     else
0138     {
0139         intersection = Rect<T>(0, 0, 0, 0);
0140         return false;
0141     }
0142 }
0143 
0144 template <typename T>
0145 sf::Vector2<T> Rect<T>::getPosition() const
0146 {
0147     return sf::Vector2<T>(left, top);
0148 }
0149 
0150 template <typename T>
0151 sf::Vector2<T> Rect<T>::getSize() const
0152 {
0153     return sf::Vector2<T>(width, height);
0154 }
0155 
0156 
0157 ////////////////////////////////////////////////////////////
0158 template <typename T>
0159 inline bool operator ==(const Rect<T>& left, const Rect<T>& right)
0160 {
0161     return (left.left == right.left) && (left.width == right.width) &&
0162            (left.top == right.top) && (left.height == right.height);
0163 }
0164 
0165 
0166 ////////////////////////////////////////////////////////////
0167 template <typename T>
0168 inline bool operator !=(const Rect<T>& left, const Rect<T>& right)
0169 {
0170     return !(left == right);
0171 }