GameKit  0.0.1a
C++ gamedev tools
Public Member Functions | Public Attributes | List of all members
gk::Rect< T > Class Template Reference

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

#include <Rect.hpp>

Public Member Functions

 Rect ()=default
 Default constructor. More...
 
 Rect (T _x, T _y, T _width, T _height)
 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 > &rect)
 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 > &rect) const
 Check the intersection between two rectangles. More...
 
intersectionDirection (const Rect< T > &rect) const
 Check the intersection direction between two rectangles. More...
 
bool operator== (const Rect< T > &rect) const
 Overload of binary operator ==. More...
 
bool operator!= (const Rect< T > &rect) const
 Overload of binary operator !=. More...
 

Public Attributes

x = 0
 Left coordinate of the rectangle. More...
 
y = 0
 Top coordinate of the rectangle. More...
 
width = 0
 Width of the rectangle. More...
 
height = 0
 Height of the rectangle. More...
 

Detailed Description

template<typename T>
class gk::Rect< T >

Utility class for manipulating 2D axis aligned rectangles.

This part of the documentation has been taken from SFML Once the migration to SFML 2.6 is done, this file will be removed

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 (x, y, width and height) are public and can be accessed directly, just like the vector classes (Vector2 and Vector3).

To keep things simple, gk::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.

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

This means that gk::IntRect(0, 0, 1, 1) and gk::IntRect(1, 1, 1, 1) don't intersect.

gk::Rect is a template and may be used with any numeric type, but for simplicity the instantiations used by GameKit are typedef'd:

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

Definition at line 28 of file Rect.hpp.

Constructor & Destructor Documentation

§ Rect() [1/4]

template<typename T>
gk::Rect< T >::Rect ( )
default

Default constructor.

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

§ Rect() [2/4]

template<typename T>
gk::Rect< T >::Rect ( _x,
_y,
_width,
_height 
)
inline

Construct the rectangle from its coordinates.

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

Parameters
_xLeft coordinate of the rectangle
_yTop coordinate of the rectangle
_widthWidth of the rectangle
_heightHeight of the rectangle

Definition at line 51 of file Rect.hpp.

§ Rect() [3/4]

template<typename T>
gk::Rect< T >::Rect ( const Vector2< T > &  position,
const Vector2< T > &  size 
)
inline

Construct the rectangle from position and size.

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

Parameters
positionPosition of the top-left corner of the rectangle
sizeSize of the rectangle

Definition at line 64 of file Rect.hpp.

§ Rect() [4/4]

template<typename T>
template<typename U >
gk::Rect< T >::Rect ( const Rect< U > &  rect)
inlineexplicit

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.

Parameters
rectRectangle to convert

Definition at line 79 of file Rect.hpp.

Member Function Documentation

§ contains() [1/2]

template<typename T>
bool gk::Rect< T >::contains ( _x,
_y 
) const
inline

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

This check is non-inclusive. If the point lies on the edge of the rectangle, this function will return false.

Parameters
_xX coordinate of the point to test
_yY coordinate of the point to test
Returns
True if the point is inside, false otherwise
See also
intersects

Definition at line 96 of file Rect.hpp.

§ contains() [2/2]

template<typename T>
bool gk::Rect< T >::contains ( const Vector2< T > &  point) const
inline

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

This check is non-inclusive. If the point lies on the edge of the rectangle, this function will return false.

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

Definition at line 118 of file Rect.hpp.

§ intersectionDirection()

template<typename T>
T gk::Rect< T >::intersectionDirection ( const Rect< T > &  rect) const
inline

Check the intersection direction between two rectangles.

Parameters
rectRectangle to test
Returns
1 for horizontal, 2 for vertical, 0 for no intersection
See also
contains, intersects

Definition at line 161 of file Rect.hpp.

§ intersects()

template<typename T>
bool gk::Rect< T >::intersects ( const Rect< T > &  rect) const
inline

Check the intersection between two rectangles.

Parameters
rectRectangle to test
Returns
True if rectangles overlap, false otherwise
See also
contains, intersectionDirection

Definition at line 132 of file Rect.hpp.

§ operator!=()

template<typename T>
bool gk::Rect< T >::operator!= ( const Rect< T > &  rect) const
inline

Overload of binary operator !=.

This operator compares strict difference between two rectangles.

Parameters
rectRectangle to compare with
Returns
True if this rectangle is not equal to rect

Definition at line 210 of file Rect.hpp.

§ operator==()

template<typename T>
bool gk::Rect< T >::operator== ( const Rect< T > &  rect) const
inline

Overload of binary operator ==.

This operator compares strict equality between two rectangles.

Parameters
rectRectangle to compare with
Returns
True if this rectangle is equal to rect

Definition at line 198 of file Rect.hpp.

Member Data Documentation

§ height

template<typename T>
T gk::Rect< T >::height = 0

Height of the rectangle.

Definition at line 218 of file Rect.hpp.

§ width

template<typename T>
T gk::Rect< T >::width = 0

Width of the rectangle.

Definition at line 217 of file Rect.hpp.

§ x

template<typename T>
T gk::Rect< T >::x = 0

Left coordinate of the rectangle.

Definition at line 215 of file Rect.hpp.

§ y

template<typename T>
T gk::Rect< T >::y = 0

Top coordinate of the rectangle.

Definition at line 216 of file Rect.hpp.


The documentation for this class was generated from the following file: