Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
Public Member Functions | Static Public Member Functions | Public Attributes | 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>

Public Member Functions

constexpr Rect () noexcept
 Default constructor. More...
 
constexpr Vector< T, 2 > getPosition () const noexcept
 Get the position of the rectangle. More...
 
constexpr Vector< T, 2 > getSize () const noexcept
 Get 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 bool contains (Vector< T, 2 > point) const noexcept
 Check if a point is inside the rectangle. More...
 
constexpr bool contains (const Rect &other) const noexcept
 Check if a rectangle is totally inside the rectangle. More...
 
constexpr bool intersects (const Rect &other) const noexcept
 Check if two rectangles interset. More...
 
constexpr bool intersects (const Rect &other, Rect &result) const noexcept
 Check if two rectangles interset and get the intersetion rectangle. More...
 
constexpr Rect getIntersection (const Rect &other) const noexcept
 Compute the intersection of two rectangles. More...
 
constexpr T getIntersectionVolume (const Rect &other) const noexcept
 Get the volume of the intersection. More...
 
constexpr T getIntersectionExtentLength (const Rect &other) const noexcept
 Get the extent length of the intersection. More...
 
constexpr void extend (const T(&point)[2]) noexcept
 Extend the rectangle with a point (as array) More...
 
constexpr void extend (Vector< T, 2 > point) noexcept
 Extend the rectangle with a point. More...
 
constexpr void extend (const Rect &other) noexcept
 Extend the rectangle with a rectangle. More...
 
constexpr Rect getExtended (const Rect &other) const noexcept
 Get the rectangle extended by another rectangle. More...
 
constexpr T getVolume () const noexcept
 Get the volume of the rectangle. More...
 
constexpr T getExtentLength () const noexcept
 Get the extent length of the rectangle. More...
 
constexpr T getMinimumEdge () const noexcept
 Get the minimum edge of the rectangle. More...
 
constexpr Rect grow (T value) const noexcept
 Grow the rectangle. More...
 
constexpr Rect shrink (T value) const noexcept
 Shrink the rectangle. More...
 
constexpr void normalize () noexcept
 Ensures that min coordinates are less than max coordinates. 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...
 

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...
 
static constexpr Rect< Tempty () noexcept
 Create an empty rectangle. More...
 

Public Attributes

Vector< T, 2 > min
 The minimum point of the rectangle. More...
 
Vector< T, 2 > max
 The maximum point 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...
 
template<typename Archive , typename T >
Archive & operator| (Archive &ar, Rect< T > &rect)
 Serialize a rectangle. 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()

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

Default constructor.

Creates a rectangle of size zero located at the origin.

Member Function Documentation

◆ contains() [1/2]

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

Check if a point is inside the rectangle.

Parameters
pointThe point to test
Returns
True if the point is inside the rectangle

◆ contains() [2/2]

template<typename T>
constexpr bool gf::Rect< T >::contains ( const Rect< T > &  other) const
inlinenoexcept

Check if a rectangle is totally inside the rectangle.

Parameters
otherAnother rectangle to test
Returns
True if the tested rectangle is inside the current rectangle

◆ empty()

template<typename T>
static constexpr Rect<T> gf::Rect< T >::empty ( )
inlinestaticnoexcept

Create an empty rectangle.

Returns
An empty rectangle

◆ extend() [1/3]

template<typename T>
constexpr void gf::Rect< T >::extend ( const T(&)  point[2])
inlinenoexcept

Extend the rectangle with a point (as array)

Parameters
pointThe point of extension

◆ extend() [2/3]

template<typename T>
constexpr void gf::Rect< T >::extend ( Vector< T, 2 >  point)
inlinenoexcept

Extend the rectangle with a point.

Parameters
pointThe point of extension

◆ extend() [3/3]

template<typename T>
constexpr void gf::Rect< T >::extend ( const Rect< T > &  other)
inlinenoexcept

Extend the rectangle with a rectangle.

Parameters
otherThe rectangle of extension

◆ 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

◆ getCenter()

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

Get the center of the rectangle.

Returns
The center of the rectangle

◆ getExtended()

template<typename T>
constexpr Rect gf::Rect< T >::getExtended ( const Rect< T > &  other) const
inlinenoexcept

Get the rectangle extended by another rectangle.

Parameters
otherThe rectangle of extension

◆ getExtentLength()

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

Get the extent length of the rectangle.

Returns
The extent length of the rectangle

◆ getHeight()

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

Get the height of the rectangle.

Returns
The height of the rectangle

◆ getIntersection()

template<typename T>
constexpr Rect gf::Rect< T >::getIntersection ( const Rect< T > &  other) const
inlinenoexcept

Compute the intersection of two rectangles.

Parameters
otherAnother rectangle to test
Returns
The result of the intersection

◆ getIntersectionExtentLength()

template<typename T>
constexpr T gf::Rect< T >::getIntersectionExtentLength ( const Rect< T > &  other) const
inlinenoexcept

Get the extent length of the intersection.

Parameters
otherAnother rectangle to test
Returns
The extent length of the intersection or 0 in case there is no intersection

◆ getIntersectionVolume()

template<typename T>
constexpr T gf::Rect< T >::getIntersectionVolume ( const Rect< T > &  other) const
inlinenoexcept

Get the volume of the intersection.

Parameters
otherAnother rectangle to test
Returns
The volume of the intersection or 0 in case there is no intersection

◆ getMinimumEdge()

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

Get the minimum edge of the rectangle.

Returns
The length of the minimum edge of the rectangle

◆ getPosition()

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

Get the position of the rectangle.

This is the same as min.

Returns
The position 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

◆ getSize()

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

Get the size of the rectangle.

Returns
The size of 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

◆ getVolume()

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

Get the volume of the rectangle.

Returns
The volume of the rectangle

◆ getWidth()

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

Get the width of the rectangle.

Returns
The width of the rectangle

◆ grow()

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

Grow the rectangle.

Parameters
valueThe amount to grow
Returns
A new extended rectangle
See also
shrink()

◆ intersects() [1/2]

template<typename T>
constexpr bool gf::Rect< T >::intersects ( const Rect< T > &  other) const
inlinenoexcept

Check if two rectangles interset.

Parameters
otherAnother rectangle to test
Returns
True if the two rectangles interset

◆ intersects() [2/2]

template<typename T>
constexpr bool gf::Rect< T >::intersects ( const Rect< T > &  other,
Rect< T > &  result 
) const
inlinenoexcept

Check if two rectangles interset and get the intersetion rectangle.

Parameters
otherAnother rectangle to test
resultA reference to put the result of the intersection (in case there is any)
Returns
True if the two rectangles interset

◆ isEmpty()

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

Check if the rectangle is empty.

Returns
True if the rectangle is empty

◆ normalize()

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

Ensures that min coordinates are less than max coordinates.

◆ shrink()

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

Shrink the rectangle.

Parameters
valueThe amount to shrink
Returns
A new shrinked rectangle
See also
grow()

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

Member Data Documentation

◆ max

template<typename T>
Vector<T, 2> gf::Rect< T >::max

The maximum point of the rectangle.

◆ min

template<typename T>
Vector<T, 2> gf::Rect< T >::min

The minimum point of the rectangle.