![]() |
Gamedev Framework (gf)
0.7.0
A C++14 framework for 2D games
|
Utility class for manipulating 2D axis aligned rectangles. More...
#include <gf/Rect.h>
Public Member Functions | |
constexpr | Rect () noexcept |
Default constructor. More... | |
constexpr | Rect (T rectLeft, T rectTop, T rectWidth, T rectHeight) noexcept |
Construct the rectangle from its coordinates. More... | |
Rect (const Vector< T, 2 > &position, const Vector< T, 2 > &size) noexcept | |
Construct the rectangle from position and size. More... | |
Rect (const Rect &)=default | |
Default copy constructor. More... | |
Rect & | operator= (const Rect &)=default |
Default copy assignment. More... | |
constexpr Vector< T, 2 > | getPosition () const noexcept |
Get the position of the rectangle. More... | |
constexpr void | setPosition (Vector< T, 2 > position) noexcept |
Set the position of the rectangle. More... | |
constexpr Vector< T, 2 > | getSize () const noexcept |
Get the size of the rectangle. More... | |
constexpr void | setSize (Vector< T, 2 > size) noexcept |
Set the size of the rectangle. More... | |
constexpr bool | isEmpty () const noexcept |
Check if the rectangle is empty. More... | |
constexpr Vector< T, 2 > | getCenter () const noexcept |
Get the center of the rectangle. More... | |
constexpr Vector< T, 2 > | getTopLeft () const noexcept |
Get the top left corner. More... | |
constexpr Vector< T, 2 > | getTopRight () const noexcept |
Get the top right corner. More... | |
constexpr Vector< T, 2 > | getBottomLeft () const noexcept |
Get the bottom left corner. More... | |
constexpr Vector< T, 2 > | getBottomRight () const noexcept |
Get the bottom right corner. More... | |
constexpr bool | contains (const Vector< T, 2 > &point) const noexcept |
Check if a point is inside the rectangle's area. More... | |
constexpr bool | contains (const Rect< T > &other) const noexcept |
Check if a rectangle is inside the rectangle's area. More... | |
constexpr bool | intersects (const Rect< T > &other) const noexcept |
Check the intersection between two rectangles. More... | |
bool | intersects (const Rect< T > &other, Rect< T > &result) const noexcept |
Check the intersection between two rectangles. More... | |
constexpr Rect< T > | extend (T value) const noexcept |
Extend the rectangle. More... | |
constexpr Rect< T > | shrink (T value) const noexcept |
Shrink the rectangle. 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 > &lhs, const Rect< T > &rhs) |
Equality operator. More... | |
template<typename T > | |
bool | operator!= (const Rect< T > &lhs, const Rect< T > &rhs) |
Inequality 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.
gf::Rect uses the usual rules for its boundaries:
This means that gf::RectI(0, 0, 1, 1) and gf::RectI(1, 1, 1, 1) don't intersect.
gf::Rect is a template and may be used with any numeric type, but for simplicity, some common typedef are defined:
int
as T
unsigned
as T
std::size_t
as T
float
as T
So that you don't have to care about the template syntax.
Usage example:
Default constructor.
Creates an empty rectangle (it is equivalent to calling Rect(0, 0, 0, 0)
).
|
inlinenoexcept |
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 |
|
inlinenoexcept |
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 |
|
inlinenoexcept |
Check if a point is inside the rectangle's area.
point | Point to test |
|
inlinenoexcept |
Check if a rectangle is inside the rectangle's area.
other | Rectangle to test |
Extend the rectangle.
value | The amount to extend |
Get the bottom left corner.
Get the bottom right corner.
Get the center of the rectangle.
Get the position of the rectangle.
Get the top left corner.
Get the top right corner.
|
inlinenoexcept |
Check the intersection between two rectangles.
other | Rectangle to test |
|
inlinenoexcept |
Check the intersection between two rectangles.
This overload returns the overlapped rectangle in the result
parameter.
other | Rectangle to test |
result | Rectangle to be filled with the intersection |
Check if the rectangle is empty.
An empty rectangle is a rectangle that has one of its size coordinates that is zero.
Default copy assignment.
|
inlinenoexcept |
Set the position of the rectangle.
position | The new position of the rectangle |
Set the size of the rectangle.
size | The new size of the rectangle |
Shrink the rectangle.
value | The amount to shrink |
Inequality operator.
lhs | First rectangle |
rhs | Second rectangle |
Equality operator.
lhs | First rectangle |
rhs | Second rectangle |