Gamedev Framework (gf)  0.5.0
A C++11 framework for 2D games
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 Rect (T rectLeft, T rectTop, T rectWidth, T rectHeight) noexcept
 Construct the rectangle from its coordinates. More...
 
 Rect (const Vector< T, 2 > &rectPosition, const Vector< T, 2 > &rectSize) noexcept
 Construct the rectangle from position and size. 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 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< Textend (T value) const noexcept
 Extend the rectangle. More...
 
constexpr Rect< Tshrink (T value) const noexcept
 Shrink the rectangle. More...
 

Public Attributes

union {
   struct {
      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...
 
   } 
 
   struct {
      Vector< T, 2 >   position
 Position of the rectangle. More...
 
      Vector< T, 2 >   size
 Size 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...
 

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 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. You can also access the position and the size of the rectangle directly as gf::Vector.

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:

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
// 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/3]

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

Default constructor.

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

◆ Rect() [2/3]

template<typename T>
constexpr gf::Rect< T >::Rect ( T  rectLeft,
T  rectTop,
T  rectWidth,
T  rectHeight 
)
inlinenoexcept

Construct the rectangle from its coordinates.

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

Parameters
rectLeftLeft coordinate of the rectangle
rectTopTop coordinate of the rectangle
rectWidthWidth of the rectangle
rectHeightHeight of the rectangle

◆ Rect() [3/3]

template<typename T>
gf::Rect< T >::Rect ( const Vector< T, 2 > &  rectPosition,
const Vector< T, 2 > &  rectSize 
)
inlinenoexcept

Construct the rectangle from position and size.

Be careful, the last parameter is the size, not the bottom-right corner!

Parameters
rectPositionPosition of the top left corner of the rectangle
rectSizeSize of the rectangle

Member Function Documentation

◆ contains() [1/2]

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

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

Parameters
pointPoint to test
Returns
True if the point is inside, false otherwise
See also
intersects()

◆ contains() [2/2]

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

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

Parameters
otherRectangle to test
Returns
True if the rectangle is inside, false otherwise
See also
intersects()

◆ extend()

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

Extend the rectangle.

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

◆ 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

◆ getPosition()

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

Get the position of the rectangle.

It is a synonym for the position member

Returns
The position of the rectangle
See also
getSize()

◆ getSize()

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

Get the size of the rectangle.

It is a synonym for the size member

Returns
The size of the rectangle
See also
getPosition()

◆ 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

◆ intersects() [1/2]

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

Check the intersection between two rectangles.

Parameters
otherRectangle to test
Returns
True if rectangles overlap, false otherwise
See also
contains()

◆ intersects() [2/2]

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

Check the intersection between two rectangles.

This overload returns the overlapped rectangle in the result parameter.

Parameters
otherRectangle to test
resultRectangle to be filled with the intersection
Returns
True if rectangles overlap, false otherwise
See also
contains

◆ isEmpty()

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

Check if the rectangle is empty.

An empty rectangle is a rectangle that has one of its size coordinates that is zero.

Returns
True if the rectangle is empty

◆ shrink()

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

Shrink the rectangle.

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

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

Member Data Documentation

◆ @17

union { ... }

An anonymous union to handle the various representations

◆ height

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

Height of the rectangle.

◆ left

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

Left coordinate of the rectangle.

◆ position

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

Position of the rectangle.

◆ size

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

Size of the rectangle.

◆ top

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

Top coordinate of the rectangle.

◆ width

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

Width of the rectangle.