SFML 3.0.1
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Related Symbols | List of all members
sf::Rect< T > Class Template Reference
Graphics module

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

template<typename T>
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> is sf::IntRect
- sf::Rect<float> is sf::FloatRect

So that you don't have to care about the template syntax.

Usage example:

// Define a rectangle, located at (0, 0) with a size of 20x5
sf::IntRect r1({0, 0}, {20, 5});
// Define another rectangle, located at (4, 2) with a size of 18x10
sf::Vector2i position(4, 2);
sf::Vector2i size(18, 10);
sf::IntRect r2(position, size);
// Test intersections with the point (3, 1)
bool b1 = r1.contains({3, 1}); // true
bool b2 = r2.contains({3, 1}); // false
// Test the intersection between r1 and r2
std::optional<sf::IntRect> result = r1.findIntersection(r2);
// result.has_value() == true
// result.value() == sf::IntRect({4, 2}, {16, 3})
sf::Rect::size
Vector2< T > size
Size of the rectangle.
Definition Rect.hpp:112
sf::Rect::position
Vector2< T > position
Position of the top-left corner of the rectangle.
Definition Rect.hpp:111
sf::IntRect
Rect< int > IntRect
Definition Rect.hpp:146
sf::Vector2i
Vector2< int > Vector2i
Definition Vector2.hpp:208

Definition at line 42 of file Rect.hpp.

## Constructor & Destructor Documentation

## ◆ Rect() [1/2]

template<typename T>

sf::Rect< T >::Rect | ( |  | ) |
| constexpr default

Default constructor.

Creates an empty rectangle (it is equivalent to calling Rect({0, 0}, {0, 0})).

## ◆ Rect() [2/2]

template<typename T>

sf::Rect< T >::Rect | ( | Vector2< T > | position,
|  | Vector2< T > | size )
| constexpr

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()

template<typename T>

bool sf::Rect< T >::contains | ( | Vector2< T > | point | ) | const
| nodiscard constexpr

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()

template<typename T>

std::optional< Rect< T > > sf::Rect< T >::findIntersection | ( | const Rect< T > & | rectangle | ) | const
| nodiscard constexpr

Check the intersection between two rectangles.

Parameters

  rectangle | Rectangle to test

Returns
  Intersection rectangle if intersecting, std::nullopt otherwise

See also
  contains

## ◆ getCenter()

template<typename T>

Vector2< T > sf::Rect< T >::getCenter | ( |  | ) | const
| nodiscard constexpr

Get the position of the center of the rectangle.

Returns
  Center of rectangle

## ◆ operator Rect< U >()

template<typename T>
template<typename U>

sf::Rect< T >::operator Rect< U > | ( |  | ) | const
| explicit constexpr

Converts the rectangle to another type of rectangle.

## Friends And Related Symbol Documentation

## ◆ operator!=()

template<typename T>

bool operator!= | ( | const Rect< T > & | lhs,
|  | const Rect< T > & | rhs )
| related

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==()

template<typename T>

bool operator== | ( | const Rect< T > & | lhs,
|  | const Rect< T > & | rhs )
| related

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

template<typename T>

Vector2<T> sf::Rect< T >::position {}

Position of the top-left corner of the rectangle.

Definition at line 111 of file Rect.hpp.

## ◆ size

template<typename T>

Vector2<T> sf::Rect< T >::size {}

Size of the rectangle.

Definition at line 112 of file Rect.hpp.

The documentation for this class was generated from the following file:

- Rect.hpp