- cpp3ds
- Rect
Utility class for manipulating 2D axis aligned rectangles. More...
#include <Rect.hpp>
Public Member Functions | |
Rect () | |
Default constructor. More... | |
Rect (T rectLeft, T rectTop, T rectWidth, T rectHeight) | |
Construct the rectangle from its coordinates. More... | |
Rect (const Vector2< T > &position, const Vector2< T > &size) | |
Construct the rectangle from position and size. More... | |
template<typename U > | |
Rect (const Rect< U > &rectangle) | |
Construct the rectangle from another type of rectangle. More... | |
bool | contains (T x, T y) const |
Check if a point is inside the rectangle's area. More... | |
bool | contains (const Vector2< T > &point) const |
Check if a point is inside the rectangle's area. More... | |
bool | intersects (const Rect< T > &rectangle) const |
Check the intersection between two rectangles. More... | |
bool | intersects (const Rect< T > &rectangle, Rect< T > &intersection) const |
Check the intersection between two rectangles. More... | |
Public Attributes | |
T | left |
Left coordinate of the rectangle. More... | |
T | top |
Top coordinate of the rectangle. More... | |
T | width |
Width of the rectangle. More... | |
T | height |
Height of the rectangle. More... | |
Related Functions | |
(Note that these are not member functions.) | |
template<typename T > | |
bool | operator== (const Rect< T > &left, const Rect< T > &right) |
Overload of binary operator ==. More... | |
template<typename T > | |
bool | operator!= (const Rect< T > &left, const Rect< T > &right) |
Overload of binary operator !=. More... | |
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, cpp3ds::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.
cpp3ds::Rect uses the usual rules for its boundaries:
This means that cpp3ds::IntRect(0, 0, 1, 1) and cpp3ds::IntRect(1, 1, 1, 1) don't intersect.
cpp3ds::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:
cpp3ds::Rect< T >::Rect | ( | ) |
Default constructor.
Creates an empty rectangle (it is equivalent to calling Rect(0, 0, 0, 0)).
cpp3ds::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!
rectLeft | Left coordinate of the rectangle |
rectTop | Top coordinate of the rectangle |
rectWidth | Width of the rectangle |
rectHeight | Height of the rectangle |
cpp3ds::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!
position | Position of the top-left corner of the rectangle |
size | Size of the 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.
rectangle | Rectangle to convert |
bool cpp3ds::Rect< T >::contains | ( | T | x, |
T | y | ||
) | const |
Check if a point is inside the rectangle's area.
x | X coordinate of the point to test |
y | Y coordinate of the point to test |
bool cpp3ds::Rect< T >::contains | ( | const Vector2< T > & | point | ) | const |
Check if a point is inside the rectangle's area.
point | Point to test |
bool cpp3ds::Rect< T >::intersects | ( | const Rect< T > & | rectangle | ) | const |
Check the intersection between two rectangles.
rectangle | Rectangle to test |
bool cpp3ds::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.
rectangle | Rectangle to test |
intersection | Rectangle to be filled with the intersection |
Overload of binary operator !=.
This operator compares strict difference between two rectangles.
left | Left operand (a rectangle) |
right | Right operand (a rectangle) |
Overload of binary operator ==.
This operator compares strict equality between two rectangles.
left | Left operand (a rectangle) |
right | Right operand (a rectangle) |
T cpp3ds::Rect< T >::height |
T cpp3ds::Rect< T >::left |
T cpp3ds::Rect< T >::top |
T cpp3ds::Rect< T >::width |