Utility class for manipulating 2D axis aligned rectangles. More...
#include <SFML/Graphics/Rect.hpp>
Public Member Functions | |
constexpr | Rect ()=default |
Default constructor. | |
constexpr | Rect (Vector2< T > position, Vector2< T > size) |
Construct the rectangle from position and size. | |
template<typename U > | |
constexpr | operator Rect< U > () const |
Converts the rectangle to another type of rectangle. | |
constexpr bool | contains (Vector2< T > point) const |
Check if a point is inside the rectangle's area. | |
constexpr std::optional< Rect< T > > | findIntersection (const Rect< T > &rectangle) const |
Check the intersection between two rectangles. | |
constexpr Vector2< T > | getCenter () const |
Get the position of the center of the rectangle. | |
Public Attributes | |
Vector2< T > | position {} |
Position of the top-left corner of the rectangle. | |
Vector2< T > | size {} |
Size of the rectangle. | |
Related Symbols | |
(Note that these are not member symbols.) | |
template<typename T > | |
constexpr bool | operator== (const Rect< T > &lhs, const Rect< T > &rhs) |
Overload of binary operator== | |
template<typename T > | |
constexpr bool | operator!= (const Rect< T > &lhs, const Rect< T > &rhs) |
Overload of binary operator!= | |
Detailed Description
class sf::Rect< T >
Utility class for manipulating 2D axis aligned rectangles.
A rectangle is defined by its top-left corner and its size.
It is a very simple class defined for convenience, so its member variables (position and size) are public and can be accessed directly, just like the vector classes (Vector2
and Vector3
).
To keep things simple, sf::Rect
doesn't define functions to emulate the properties that are not directly members (such as right, bottom, etc.), it rather only provides intersection functions.
sf::Rect
uses the usual rules for its boundaries:
- The left and top edges are included in the rectangle's area
- The right and bottom edges are excluded from the rectangle's area
This means that sf::IntRect({0, 0}, {1, 1})
and sf::IntRect({1, 1}, {1, 1})
don't intersect.
sf::Rect
is a template and may be used with any numeric type, but for simplicity type aliases for the instantiations used by SFML are given:
sf::Rect<int>
issf::IntRect
sf::Rect<float>
issf::FloatRect
So that you don't have to care about the template syntax.
Usage example:
Constructor & Destructor Documentation
◆ Rect() [1/2]
|
constexprdefault |
Default constructor.
Creates an empty rectangle (it is equivalent to calling Rect({0, 0}, {0, 0})
).
◆ Rect() [2/2]
Construct the rectangle from position and size.
Be careful, the last parameter is the size, not the bottom-right corner!
- Parameters
-
position Position of the top-left corner of the rectangle size Size of the rectangle
Member Function Documentation
◆ contains()
Check if a point is inside the rectangle's area.
This check is non-inclusive. If the point lies on the edge of the rectangle, this function will return false
.
- Parameters
-
point Point to test
- Returns
true
if the point is inside,false
otherwise
- See also
findIntersection
◆ findIntersection()
|
nodiscardconstexpr |
Check the intersection between two rectangles.
- Parameters
-
rectangle Rectangle to test
- Returns
- Intersection rectangle if intersecting,
std::nullopt
otherwise
- See also
contains
◆ getCenter()
Get the position of the center of the rectangle.
- Returns
- Center of rectangle
◆ operator Rect< U >()
|
explicitconstexpr |
Converts the rectangle to another type of rectangle.
Friends And Related Symbol Documentation
◆ operator!=()
Overload of binary operator!=
This operator compares strict difference between two rectangles.
- Parameters
-
lhs Left operand (a rectangle) rhs Right operand (a rectangle)
- Returns
true
if lhs is not equal to rhs
◆ operator==()
Overload of binary operator==
This operator compares strict equality between two rectangles.
- Parameters
-
lhs Left operand (a rectangle) rhs Right operand (a rectangle)
- Returns
true
if lhs is equal to rhs
Member Data Documentation
◆ position
◆ size
The documentation for this class was generated from the following file: