sf::Rect< T > Class Template Reference
[Graphics module]

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

#include <Rect.hpp>

List of all members.

Public Member Functions

 Rect ()
 Default constructor.
 Rect (T left, T top, T width, T height)
 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.
template<typename T >
 Rect (T left, T top, T width, T height)
template<typename T >
 Rect (const Vector2< T > &position, const Vector2< T > &size)
template<typename U >
 Rect (const Rect< U > &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.

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:

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 instanciations used by SFML are typedefed:

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)

Definition at line 42 of file Rect.hpp.


Constructor & Destructor Documentation

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

Default constructor.

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

template<typename T>
sf::Rect< T >::Rect ( left,
top,
width,
height 
)

Construct the rectangle from its coordinates.

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

Parameters:
left Left coordinate of the rectangle
top Top coordinate of the rectangle
width Width of the rectangle
height Height of the rectangle
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:
position Position of the top-left corner of the rectangle
size Size of the rectangle
template<typename T>
template<typename U >
sf::Rect< T >::Rect ( const Rect< U > &  rectangle  )  [inline, 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:
rectangle Rectangle to convert

Member Function Documentation

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

Check if a point is inside the rectangle's area.

Parameters:
point Point to test
Returns:
True if the point is inside, false otherwise
See also:
Intersects
template<typename T>
bool sf::Rect< T >::Contains ( x,
y 
) const

Check if a point is inside the rectangle's area.

Parameters:
x X coordinate of the point to test
y Y coordinate of the point to test
Returns:
True if the point is inside, false otherwise
See also:
Intersects
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:
rectangle Rectangle to test
intersection Rectangle to be filled with the intersection
Returns:
True if rectangles overlap, false otherwise
See also:
Contains
template<typename T>
bool sf::Rect< T >::Intersects ( const Rect< T > &  rectangle  )  const

Check the intersection between two rectangles.

Parameters:
rectangle Rectangle to test
Returns:
True if rectangles overlap, false otherwise
See also:
Contains

Member Data Documentation

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

Height of the rectangle.

Definition at line 154 of file Rect.hpp.

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

Left coordinate of the rectangle.

Definition at line 151 of file Rect.hpp.

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

Top coordinate of the rectangle.

Definition at line 152 of file Rect.hpp.

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

Width of the rectangle.

Definition at line 153 of file Rect.hpp.


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