Warning
This page refers to an old version of SFML. Click here to switch to the latest version.
Warning
This page refers to an old version of SFML. Click here to switch to the latest version.
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!= |
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:
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:
So that you don't have to care about the template syntax.
Usage example:
|
constexprdefault |
Default constructor.
Creates an empty rectangle (it is equivalent to calling Rect({0, 0}, {0, 0})).
Construct the rectangle from position and size.
Be careful, the last parameter is the size, not the bottom-right corner!
position | Position of the top-left corner of the rectangle |
size | Size of the rectangle |
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.
point | Point to test |
|
nodiscardconstexpr |
Check the intersection between two rectangles.
rectangle | Rectangle to test |
Get the position of the center of the rectangle.
|
explicitconstexpr |
Converts the rectangle to another type of rectangle.
Overload of binary operator!=
This operator compares strict difference between two rectangles.
lhs | Left operand (a rectangle) |
rhs | Right operand (a rectangle) |
Overload of binary operator==
This operator compares strict equality between two rectangles.
lhs | Left operand (a rectangle) |
rhs | Right operand (a rectangle) |