Gamedev Framework (gf)  0.14.0
A C++14 framework for 2D games
Public Member Functions | Static Public Member Functions | Related Functions | List of all members
gf::Rect< T > Struct Template Reference

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

#include <gf/Rect.h>

Inheritance diagram for gf::Rect< T >:
Inheritance graph
[legend]

Public Member Functions

constexpr Rect () noexcept
 Default constructor. More...
 
constexpr Rect (const Box< T, 2 > &box)
 Constructor from a box. More...
 
constexpr Vector< T, 2 > getPositionFromAnchor (Anchor anchor) const noexcept
 Get a position from the rectangle and an anchor. 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 T getWidth () const noexcept
 Get the width of the rectangle. More...
 
constexpr T getHeight () const noexcept
 Get the height of the rectangle. More...
 
- Public Member Functions inherited from gf::Box< T, 2 >
constexpr Box () noexcept
 Default constructor. More...
 
constexpr Box (Vector< T, N > p0, Vector< T, N > p1) noexcept
 Constructor with two points. More...
 
constexpr Box (const T(&p0)[N], const T(&p1)[N]) noexcept
 Constructor with two points (as arrays) More...
 
constexpr Box (Vector< T, N > p) noexcept
 Constructor with one point. More...
 
constexpr Vector< T, N > getPosition () const noexcept
 Get the position of the box. More...
 
constexpr Vector< T, N > getSize () const noexcept
 Get the size of the box. More...
 
constexpr bool isEmpty () const noexcept
 Check if the box is empty. More...
 
constexpr Vector< T, N > getCenter () const noexcept
 Get the center of the box. More...
 
constexpr bool contains (Vector< T, N > point) const noexcept
 Check if a point is inside the box. More...
 
constexpr bool contains (const Box< T, N > &other) const noexcept
 Check if a box is totally inside the box. More...
 
constexpr bool intersects (const Box< T, N > &other) const noexcept
 Check if two boxes interset. More...
 
constexpr bool intersects (const Box< T, N > &other, Box< T, N > &result) const noexcept
 Check if two boxes interset and get the intersetion box. More...
 
constexpr Box< T, N > getIntersection (const Box< T, N > &other) const noexcept
 Compute the intersection of two boxes. More...
 
constexpr T getIntersectionVolume (const Box< T, N > &other) const noexcept
 Get the volume of the intersection. More...
 
constexpr T getIntersectionExtentLength (const Box< T, N > &other) const noexcept
 Get the extent length of the intersection. More...
 
constexpr void extend (const T(&point)[N]) noexcept
 Extend the box with a point (as array) More...
 
constexpr void extend (Vector< T, N > point) noexcept
 Extend the box with a point. More...
 
constexpr void extend (const Box< T, N > &other) noexcept
 Extend the box with a box. More...
 
constexpr Box< T, N > getExtended (const Box< T, N > &other) const noexcept
 Get the box extended by another box. More...
 
constexpr T getVolume () const noexcept
 Get the volume of the box. More...
 
constexpr T getExtentLength () const noexcept
 Get the extent length of the box. More...
 
constexpr T getMinimumEdge () const noexcept
 Get the minimum edge of the box. More...
 
constexpr Box< T, 2 > grow (T value) const noexcept
 Grow the box. More...
 
constexpr Box< T, 2 > shrink (T value) const noexcept
 Shrink the box. More...
 
constexpr void normalize () noexcept
 Ensures that min coordinates are less than max coordinates. More...
 

Static Public Member Functions

static constexpr Rect< TfromMinMax (Vector< T, 2 > min, Vector< T, 2 > max) noexcept
 Create a rectangle from a min point and a max point. More...
 
static constexpr Rect< TfromPositionSize (Vector< T, 2 > position, Vector< T, 2 > size) noexcept
 Create a rectangle from a position (top-left) and a size. More...
 
static constexpr Rect< TfromCenterSize (Vector< T, 2 > center, Vector< T, 2 > size) noexcept
 Create a rectangle from a center and a size. 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...
 
template<typename Archive , typename T >
Archive & operator| (Archive &ar, Rect< T > &rect)
 Serialize a rectangle. More...
 

Additional Inherited Members

- Public Attributes inherited from gf::Box< T, 2 >
Vector< T, N > min
 The minimum point of the box. More...
 
Vector< T, N > max
 The maximum point of the box. More...
 

Detailed Description

template<typename T>
struct gf::Rect< T >

Utility class for manipulating 2D axis aligned rectangles.

A rectangle is defined by its top-left corner (called min) and its bottom-right corner (called `max').

gf::Rect uses the usual rules for its boundaries:

gf::Rect is a template and may be used with any numeric type, but for simplicity, some common typedef are defined:

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
gf::RectI r1(0, 0, 20, 5);
// Define another rectangle, located at (4, 2) with a size of 18x10
gf::Vector2i position(4, 2);
gf::Vector2i size(18, 10);
gf::RectI 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
gf::RectI result;
bool b3 = r1.intersects(r2, result); // true
// result == (4, 2, 16, 3)

Constructor & Destructor Documentation

◆ Rect() [1/2]

template<typename T>
constexpr gf::Rect< T >::Rect ( )
inlinenoexcept

Default constructor.

Creates an empty rectangle.

◆ Rect() [2/2]

template<typename T>
constexpr gf::Rect< T >::Rect ( const Box< T, 2 > &  box)
inline

Constructor from a box.

Parameters
boxThe orignal box

Member Function Documentation

◆ fromCenterSize()

template<typename T>
static constexpr Rect<T> gf::Rect< T >::fromCenterSize ( Vector< T, 2 >  center,
Vector< T, 2 >  size 
)
inlinestaticnoexcept

Create a rectangle from a center and a size.

Parameters
centerThe center of the rectangle
sizeThe size of the rectangle
Returns
A new rectangle

◆ fromMinMax()

template<typename T>
static constexpr Rect<T> gf::Rect< T >::fromMinMax ( Vector< T, 2 >  min,
Vector< T, 2 >  max 
)
inlinestaticnoexcept

Create a rectangle from a min point and a max point.

Parameters
minThe minimum point in the rectangle
maxThe maximum point in the rectangle
Returns
A new rectangle

◆ fromPositionSize()

template<typename T>
static constexpr Rect<T> gf::Rect< T >::fromPositionSize ( Vector< T, 2 >  position,
Vector< T, 2 >  size 
)
inlinestaticnoexcept

Create a rectangle from a position (top-left) and a size.

Parameters
positionThe top-left position of the rectangle
sizeThe size of the rectangle
Returns
A new rectangle

◆ getBottomLeft()

template<typename T>
constexpr Vector<T, 2> gf::Rect< T >::getBottomLeft ( ) const
inlinenoexcept

Get the bottom left corner.

Returns
The bottom left corner

◆ getBottomRight()

template<typename T>
constexpr Vector<T, 2> gf::Rect< T >::getBottomRight ( ) const
inlinenoexcept

Get the bottom right corner.

Returns
The bottom right corner

◆ getHeight()

template<typename T>
constexpr T gf::Rect< T >::getHeight ( ) const
inlinenoexcept

Get the height of the rectangle.

Returns
The height of the rectangle

◆ getPositionFromAnchor()

template<typename T>
constexpr Vector<T, 2> gf::Rect< T >::getPositionFromAnchor ( Anchor  anchor) const
inlinenoexcept

Get a position from the rectangle and an anchor.

Parameters
anchorAn anchor
Returns
A position in the rectangle

◆ getTopLeft()

template<typename T>
constexpr Vector<T, 2> gf::Rect< T >::getTopLeft ( ) const
inlinenoexcept

Get the top left corner.

Returns
The top left corner

◆ getTopRight()

template<typename T>
constexpr Vector<T, 2> gf::Rect< T >::getTopRight ( ) const
inlinenoexcept

Get the top right corner.

Returns
The top right corner

◆ getWidth()

template<typename T>
constexpr T gf::Rect< T >::getWidth ( ) const
inlinenoexcept

Get the width of the rectangle.

Returns
The width of the rectangle

Friends And Related Function Documentation

◆ operator!=()

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

Inequality operator.

Parameters
lhsFirst rectangle
rhsSecond rectangle
Returns
True if the two rectangles are different

◆ operator==()

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

Equality operator.

Parameters
lhsFirst rectangle
rhsSecond rectangle
Returns
True if the two rectangles are the same

◆ operator|()

template<typename Archive , typename T >
Archive & operator| ( Archive &  ar,
Rect< T > &  rect 
)
related

Serialize a rectangle.

Parameters
arThe archive
rectThe rectangle to serialize
See also
gf::Serialize, gf::Deserialiser