Documentation of SFML 2.6.2

Loading...
Searching...
No Matches

Utility class for manipulating 2D axis aligned rectangles. More...

#include <SFML/Graphics/Rect.hpp>

Public Member Functions

 Rect ()
 Default constructor.
 
 Rect (T rectLeft, T rectTop, T rectWidth, T rectHeight)
 Construct the rectangle from its coordinates.
 
 Rect (const Vector2< T > &position, const Vector2< T > &size)
 Construct the rectangle from position and size.
 
template<typename U >
 Rect (const Rect< U > &rectangle)
 Construct the rectangle from another type of rectangle.
 
bool contains (T x, T y) const
 Check if a point is inside the rectangle's area.
 
bool contains (const Vector2< T > &point) const
 Check if a point is inside the rectangle's area.
 
bool intersects (const Rect< T > &rectangle) const
 Check the intersection between two rectangles.
 
bool intersects (const Rect< T > &rectangle, Rect< T > &intersection) const
 Check the intersection between two rectangles.
 
sf::Vector2< T > getPosition () const
 Get the position of the rectangle's top-left corner.
 
sf::Vector2< T > getSize () const
 Get the size of the rectangle.
 

Public Attributes

left
 Left coordinate of the rectangle.
 
top
 Top coordinate of the rectangle.
 
width
 Width of the rectangle.
 
height
 Height of the rectangle.
 

Related Symbols

(Note that these are not member symbols.)

template<typename T >
bool operator== (const Rect< T > &left, const Rect< T > &right)
 Overload of binary operator ==.
 
template<typename T >
bool operator!= (const Rect< T > &left, const Rect< T > &right)
 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 (left, top, width and height) 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, center, 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 (left + width) and bottom (top + height) 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 the instantiations used by SFML are typedef'd:

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
sf::IntRect result;
bool b3 = r1.intersects(r2, result); // true
// result == (4, 2, 16, 3)
bool intersects(const Rect< T > &rectangle) const
Check the intersection between two rectangles.
Utility template class for manipulating 2-dimensional vectors.
Definition Vector2.hpp:38

Definition at line 42 of file Rect.hpp.

Constructor & Destructor Documentation

◆ Rect() [1/4]

template<typename T >
sf::Rect< T >::Rect ( )

Default constructor.

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

◆ Rect() [2/4]

template<typename T >
sf::Rect< T >::Rect ( T rectLeft,
T rectTop,
T rectWidth,
T rectHeight )

Construct the rectangle from its coordinates.

Be careful, the last two parameters are the width and height, not the right and bottom coordinates!

Parameters
rectLeftLeft coordinate of the rectangle
rectTopTop coordinate of the rectangle
rectWidthWidth of the rectangle
rectHeightHeight of the rectangle

◆ Rect() [3/4]

template<typename T >
sf::Rect< T >::Rect ( const Vector2< T > & position,
const Vector2< T > & size )

Construct the rectangle from position and size.

Be careful, the last parameter is the size, not the bottom-right corner!

Parameters
positionPosition of the top-left corner of the rectangle
sizeSize of the rectangle

◆ Rect() [4/4]

template<typename T >
template<typename U >
sf::Rect< T >::Rect ( const Rect< U > & rectangle)
explicit

Construct the rectangle from another type of rectangle.

This constructor doesn't replace the copy constructor, it's called only when U != T. A call to this constructor will fail to compile if U is not convertible to T.

Parameters
rectangleRectangle to convert

Member Function Documentation

◆ contains() [1/2]

template<typename T >
bool sf::Rect< T >::contains ( const Vector2< T > & point) const

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
pointPoint to test
Returns
True if the point is inside, false otherwise
See also
intersects

◆ contains() [2/2]

template<typename T >
bool sf::Rect< T >::contains ( T x,
T y ) const

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
xX coordinate of the point to test
yY coordinate of the point to test
Returns
True if the point is inside, false otherwise
See also
intersects

◆ getPosition()

template<typename T >
sf::Vector2< T > sf::Rect< T >::getPosition ( ) const

Get the position of the rectangle's top-left corner.

Returns
Position of rectangle
See also
getSize

◆ getSize()

template<typename T >
sf::Vector2< T > sf::Rect< T >::getSize ( ) const

Get the size of the rectangle.

Returns
Size of rectangle
See also
getPosition

◆ intersects() [1/2]

template<typename T >
bool sf::Rect< T >::intersects ( const Rect< T > & rectangle) const

Check the intersection between two rectangles.

Parameters
rectangleRectangle to test
Returns
True if rectangles overlap, false otherwise
See also
contains

◆ intersects() [2/2]

template<typename T >
bool sf::Rect< T >::intersects ( const Rect< T > & rectangle,
Rect< T > & intersection ) const

Check the intersection between two rectangles.

This overload returns the overlapped rectangle in the intersection parameter.

Parameters
rectangleRectangle to test
intersectionRectangle to be filled with the intersection
Returns
True if rectangles overlap, false otherwise
See also
contains

Friends And Related Symbol Documentation

◆ operator!=()

template<typename T >
bool operator!= ( const Rect< T > & left,
const Rect< T > & right )
related

Overload of binary operator !=.

This operator compares strict difference between two rectangles.

Parameters
leftLeft operand (a rectangle)
rightRight operand (a rectangle)
Returns
True if left is not equal to right

◆ operator==()

template<typename T >
bool operator== ( const Rect< T > & left,
const Rect< T > & right )
related

Overload of binary operator ==.

This operator compares strict equality between two rectangles.

Parameters
leftLeft operand (a rectangle)
rightRight operand (a rectangle)
Returns
True if left is equal to right

Member Data Documentation

◆ height

template<typename T >
T sf::Rect< T >::height

Height of the rectangle.

Definition at line 180 of file Rect.hpp.

◆ left

template<typename T >
T sf::Rect< T >::left

Left coordinate of the rectangle.

Definition at line 177 of file Rect.hpp.

◆ top

template<typename T >
T sf::Rect< T >::top

Top coordinate of the rectangle.

Definition at line 178 of file Rect.hpp.

◆ width

template<typename T >
T sf::Rect< T >::width

Width of the rectangle.

Definition at line 179 of file Rect.hpp.


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