libSDL2pp  0.16.0
C++11 bindings/wrapper for SDL2
Public Member Functions | Static Public Member Functions | List of all members
SDL2pp::Rect Class Reference

2D rectangle More...

#include <SDL2pp/Rect.hh>

Inheritance diagram for SDL2pp::Rect:

Public Member Functions

constexpr Rect ()
 Default constructor. More...
 
constexpr Rect (const SDL_Rect &rect)
 Construct a rect from existing SDL_Rect. More...
 
constexpr Rect (const Point &corner, const Point &size)
 Construct the rect from given corner coordinates, and size. More...
 
constexpr Rect (int x, int y, int w, int h)
 Construct the rect from given corner coordinates, width and height. More...
 
 Rect (const Rect &) noexcept=default
 Copy constructor.
 
 Rect (Rect &&) noexcept=default
 Move constructor.
 
Rectoperator= (const Rect &) noexcept=default
 Assignment operator. More...
 
Rectoperator= (Rect &&) noexcept=default
 Move assignment operator. More...
 
constexpr int GetX () const
 Get X coordinate of the rect corner. More...
 
RectSetX (int nx)
 Set X coordinate of the rect corner. More...
 
constexpr int GetY () const
 Get Y coordinate of the rect corner. More...
 
RectSetY (int ny)
 Set Y coordinate of the rect corner. More...
 
constexpr int GetW () const
 Get width of the rect. More...
 
RectSetW (int nw)
 Set width of the rect. More...
 
constexpr int GetH () const
 Get height of the rect. More...
 
RectSetH (int nh)
 Set height of the rect. More...
 
constexpr int GetX2 () const
 Get X coordinate of the rect second corner. More...
 
RectSetX2 (int x2)
 Set X coordinate of the rect second corner. More...
 
constexpr int GetY2 () const
 Get Y coordinate of the rect second corner. More...
 
RectSetY2 (int y2)
 Set Y coordinate of the rect second corner. More...
 
constexpr Point GetTopLeft () const
 Get top left corner of the rect. More...
 
constexpr Point GetTopRight () const
 Get top right corner of the rect. More...
 
constexpr Point GetBottomLeft () const
 Get bottom left corner of the rect. More...
 
constexpr Point GetBottomRight () const
 Get bottom right corner of the rect. More...
 
constexpr Point GetSize () const
 Get size of the rect. More...
 
constexpr Point GetCentroid () const
 Get centroid of the rect. More...
 
constexpr bool Contains (int px, int py) const
 Check whether the rect contains given point. More...
 
constexpr bool Contains (const Point &point) const
 Check whether the rect contains given point. More...
 
constexpr bool Contains (const Rect &rect) const
 Check whether the rect contains another rect. More...
 
constexpr bool Intersects (const Rect &rect) const
 Check whether the rect intersects another rect. More...
 
Rect GetUnion (const Rect &rect) const
 Calculate union with another rect. More...
 
RectUnion (const Rect &rect)
 Union rect with another rect. More...
 
Rect GetExtension (unsigned int amount) const
 Get a rect extended by specified amount of pixels. More...
 
Rect GetExtension (unsigned int hamount, unsigned int vamount) const
 Get a rect extended by specified amount of pixels. More...
 
RectExtend (unsigned int amount)
 Extend a rect by specified amount of pixels. More...
 
RectExtend (unsigned int hamount, unsigned int vamount)
 Extend a rect by specified amount of pixels. More...
 
Optional< RectGetIntersection (const Rect &rect) const
 Calculate intersection with another rect. More...
 
bool IntersectLine (int &x1, int &y1, int &x2, int &y2) const
 Calculate the intersection of a rectangle and line segment. More...
 
bool IntersectLine (Point &p1, Point &p2) const
 Calculate the intersection of a rectangle and line segment. More...
 
constexpr Rect operator+ (const Point &offset) const
 Get rectangle moved by a given offset. More...
 
constexpr Rect operator- (const Point &offset) const
 Get rectangle moved by an opposite of given offset. More...
 
Rectoperator+= (const Point &offset)
 Move by then given offset. More...
 
Rectoperator-= (const Point &offset)
 Move by an opposite of the given offset. More...
 

Static Public Member Functions

static constexpr Rect FromCenter (int cx, int cy, int w, int h)
 Construct the rect from given center coordinates, width and height. More...
 
static constexpr Rect FromCenter (const Point &center, const Point &size)
 Construct the rect from given center coordinates and size. More...
 
static constexpr Rect FromCorners (int x1, int y1, int x2, int y2)
 Construct the rect from given corners coordinates. More...
 
static constexpr Rect FromCorners (const Point &p1, const Point &p2)
 Construct the rect from given centers coordinates. More...
 

Detailed Description

2D rectangle

This class is public-derived from SDL_Rect structure, may generally used as it if passed via pointer or reference. It also supports direct access to x, y, w and h members.

See also
http://wiki.libsdl.org/SDL_Rect

Constructor & Destructor Documentation

◆ Rect() [1/4]

constexpr SDL2pp::Rect::Rect ( )
inline

Default constructor.

Creates a Rect(0, 0, 0, 0)

◆ Rect() [2/4]

constexpr SDL2pp::Rect::Rect ( const SDL_Rect &  rect)
inline

Construct a rect from existing SDL_Rect.

Parameters
[in]rectExisting SDL_Rect

◆ Rect() [3/4]

constexpr SDL2pp::Rect::Rect ( const Point corner,
const Point size 
)
inline

Construct the rect from given corner coordinates, and size.

Parameters
[in]cornerCoordinates of the top left rectangle corner
[in]sizeDimensions of the rectangle

◆ Rect() [4/4]

constexpr SDL2pp::Rect::Rect ( int  x,
int  y,
int  w,
int  h 
)
inline

Construct the rect from given corner coordinates, width and height.

Parameters
[in]xX coordinate of the top left rectangle corner
[in]yY coordinate of the top left rectangle corner
[in]wWidth of the rectangle
[in]hHeight of the rectangle

Member Function Documentation

◆ Contains() [1/3]

constexpr bool SDL2pp::Rect::Contains ( int  px,
int  py 
) const
inline

Check whether the rect contains given point.

Parameters
[in]pxX coordinate of a point
[in]pyY coordinate of a point
Returns
True if the point is contained in the rect

◆ Contains() [2/3]

constexpr bool SDL2pp::Rect::Contains ( const Point point) const
inline

Check whether the rect contains given point.

Parameters
[in]pointPoint to check
Returns
True if the point is contained in the rect

◆ Contains() [3/3]

constexpr bool SDL2pp::Rect::Contains ( const Rect rect) const
inline

Check whether the rect contains another rect.

Parameters
[in]rectRect to check
Returns
True if the checked rect is contained in this rect

◆ Extend() [1/2]

Rect & SDL2pp::Rect::Extend ( unsigned int  amount)

Extend a rect by specified amount of pixels.

Parameters
[in]amountNumber of pixels to extend by
Returns
Reference to self

◆ Extend() [2/2]

Rect & SDL2pp::Rect::Extend ( unsigned int  hamount,
unsigned int  vamount 
)

Extend a rect by specified amount of pixels.

Parameters
[in]hamountNumber of pixels to extend by in horizontal direction
[in]vamountNumber of pixels to extend by in vertical direction
Returns
Reference to self

◆ FromCenter() [1/2]

static constexpr Rect SDL2pp::Rect::FromCenter ( int  cx,
int  cy,
int  w,
int  h 
)
inlinestatic

Construct the rect from given center coordinates, width and height.

Parameters
[in]cxX coordinate of the rectangle center
[in]cyY coordinate of the rectangle center
[in]wWidth of the rectangle
[in]hHeight of the rectangle

◆ FromCenter() [2/2]

static constexpr Rect SDL2pp::Rect::FromCenter ( const Point center,
const Point size 
)
inlinestatic

Construct the rect from given center coordinates and size.

Parameters
[in]centerCoordinates of the rectangle center
[in]sizeDimensions of the rectangle

◆ FromCorners() [1/2]

static constexpr Rect SDL2pp::Rect::FromCorners ( int  x1,
int  y1,
int  x2,
int  y2 
)
inlinestatic

Construct the rect from given corners coordinates.

Parameters
[in]x1X coordinate of the top left rectangle corner
[in]y1Y coordinate of the top left rectangle corner
[in]x2X coordinate of the bottom right rectangle corner
[in]y2Y coordinate of the bottom right rectangle corner

◆ FromCorners() [2/2]

static constexpr Rect SDL2pp::Rect::FromCorners ( const Point p1,
const Point p2 
)
inlinestatic

Construct the rect from given centers coordinates.

Parameters
[in]p1Coordinates of the top left rectangle corner
[in]p2Coordinates of the bottom right rectangle corner

◆ GetBottomLeft()

constexpr Point SDL2pp::Rect::GetBottomLeft ( ) const
inline

Get bottom left corner of the rect.

Returns
bottom left corner of the rect

◆ GetBottomRight()

constexpr Point SDL2pp::Rect::GetBottomRight ( ) const
inline

Get bottom right corner of the rect.

Returns
Bottom right corner of the rect

◆ GetCentroid()

constexpr Point SDL2pp::Rect::GetCentroid ( ) const
inline

Get centroid of the rect.

Returns
Centroid of the rect

◆ GetExtension() [1/2]

Rect SDL2pp::Rect::GetExtension ( unsigned int  amount) const

Get a rect extended by specified amount of pixels.

Parameters
[in]amountNumber of pixels to extend by
Returns
Extended rect

◆ GetExtension() [2/2]

Rect SDL2pp::Rect::GetExtension ( unsigned int  hamount,
unsigned int  vamount 
) const

Get a rect extended by specified amount of pixels.

Parameters
[in]hamountNumber of pixels to extend by in horizontal direction
[in]vamountNumber of pixels to extend by in vertical direction
Returns
Extended rect

◆ GetH()

constexpr int SDL2pp::Rect::GetH ( ) const
inline

Get height of the rect.

Returns
Height of the rect

◆ GetIntersection()

Optional< Rect > SDL2pp::Rect::GetIntersection ( const Rect rect) const

Calculate intersection with another rect.

Parameters
[in]rectRect to intersect with
Returns
Rect representing intersection area or NullOpt if there was no intersection

◆ GetSize()

constexpr Point SDL2pp::Rect::GetSize ( ) const
inline

Get size of the rect.

Returns
Size of the rect

◆ GetTopLeft()

constexpr Point SDL2pp::Rect::GetTopLeft ( ) const
inline

Get top left corner of the rect.

Returns
Top left corner of the rect

◆ GetTopRight()

constexpr Point SDL2pp::Rect::GetTopRight ( ) const
inline

Get top right corner of the rect.

Returns
Top right corner of the rect

◆ GetUnion()

Rect SDL2pp::Rect::GetUnion ( const Rect rect) const

Calculate union with another rect.

Parameters
[in]rectRect to union with
Returns
Rect representing union of two rectangles

◆ GetW()

constexpr int SDL2pp::Rect::GetW ( ) const
inline

Get width of the rect.

Returns
Width of the rect

◆ GetX()

constexpr int SDL2pp::Rect::GetX ( ) const
inline

Get X coordinate of the rect corner.

Returns
X coordinate of the rect corner

◆ GetX2()

constexpr int SDL2pp::Rect::GetX2 ( ) const
inline

Get X coordinate of the rect second corner.

Returns
X coordinate of the rect second corner

◆ GetY()

constexpr int SDL2pp::Rect::GetY ( ) const
inline

Get Y coordinate of the rect corner.

Returns
Y coordinate of the rect corner

◆ GetY2()

constexpr int SDL2pp::Rect::GetY2 ( ) const
inline

Get Y coordinate of the rect second corner.

Returns
Y coordinate of the rect second corner

◆ IntersectLine() [1/2]

bool SDL2pp::Rect::IntersectLine ( int &  x1,
int &  y1,
int &  x2,
int &  y2 
) const

Calculate the intersection of a rectangle and line segment.

Parameters
[in,out]x1Starting X-coordinate of the line
[in,out]y1Starting Y-coordinate of the line
[in,out]x2Ending X-coordinate of the line
[in,out]y2Ending Y-coordinate of the line
Returns
True if there is an intersection, false otherwise

This function is used to clip a line segment to a rectangle. A line segment contained entirely within the rectangle or that does not intersect will remain unchanged. A line segment that crosses the rectangle at either or both ends will be clipped to the boundary of the rectangle and the new coordinates saved in x1, y1, x2, and/or y2 as necessary.

◆ IntersectLine() [2/2]

bool SDL2pp::Rect::IntersectLine ( Point p1,
Point p2 
) const

Calculate the intersection of a rectangle and line segment.

Parameters
[in,out]p1Starting coordinates of the line
[in,out]p2Ending coordinates of the line
Returns
True if there is an intersection, false otherwise

This function is used to clip a line segment to a rectangle. A line segment contained entirely within the rectangle or that does not intersect will remain unchanged. A line segment that crosses the rectangle at either or both ends will be clipped to the boundary of the rectangle and the new coordinates saved in p1 and/or p2 as necessary.

◆ Intersects()

constexpr bool SDL2pp::Rect::Intersects ( const Rect rect) const
inline

Check whether the rect intersects another rect.

Parameters
[in]rectRect to check
Returns
True if rectangles intersect

◆ operator+()

constexpr Rect SDL2pp::Rect::operator+ ( const Point offset) const
inline

Get rectangle moved by a given offset.

Parameters
[in]offsetPoint specifying an offset
Returns
Moved rectangle

◆ operator+=()

Rect& SDL2pp::Rect::operator+= ( const Point offset)
inline

Move by then given offset.

Parameters
[in]offsetPoint specifying an offset
Returns
Reference to self

◆ operator-()

constexpr Rect SDL2pp::Rect::operator- ( const Point offset) const
inline

Get rectangle moved by an opposite of given offset.

Parameters
[in]offsetPoint specifying an offset
Returns
Moved rectangle

◆ operator-=()

Rect& SDL2pp::Rect::operator-= ( const Point offset)
inline

Move by an opposite of the given offset.

Parameters
[in]offsetPoint specifying an offset
Returns
Reference to self

◆ operator=() [1/2]

Rect& SDL2pp::Rect::operator= ( const Rect )
defaultnoexcept

Assignment operator.

Returns
Reference to self

◆ operator=() [2/2]

Rect& SDL2pp::Rect::operator= ( Rect &&  )
defaultnoexcept

Move assignment operator.

Returns
Reference to self

◆ SetH()

Rect& SDL2pp::Rect::SetH ( int  nh)
inline

Set height of the rect.

Parameters
[in]nhNew height of the rect
Returns
Reference to self

◆ SetW()

Rect& SDL2pp::Rect::SetW ( int  nw)
inline

Set width of the rect.

Parameters
[in]nwNew width of the rect
Returns
Reference to self

◆ SetX()

Rect& SDL2pp::Rect::SetX ( int  nx)
inline

Set X coordinate of the rect corner.

Parameters
[in]nxNew X coordinate value
Returns
Reference to self

◆ SetX2()

Rect& SDL2pp::Rect::SetX2 ( int  x2)
inline

Set X coordinate of the rect second corner.

Parameters
[in]x2New X coordinate value

This modifies rectangle width internally

Returns
Reference to self

◆ SetY()

Rect& SDL2pp::Rect::SetY ( int  ny)
inline

Set Y coordinate of the rect corner.

Parameters
[in]nyNew Y coordinate value
Returns
Reference to self

◆ SetY2()

Rect& SDL2pp::Rect::SetY2 ( int  y2)
inline

Set Y coordinate of the rect second corner.

Parameters
[in]y2New Y coordinate value

This modifies rectangle height internally

Returns
Reference to self

◆ Union()

Rect & SDL2pp::Rect::Union ( const Rect rect)

Union rect with another rect.

Parameters
[in]rectRect to union with
Returns
Reference to self

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