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

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.
 
static constexpr Rect FromCenter (const Point &center, const Point &size)
 Construct the rect from given center coordinates and size.
 
static constexpr Rect FromCorners (int x1, int y1, int x2, int y2)
 Construct the rect from given corners coordinates.
 
static constexpr Rect FromCorners (const Point &p1, const Point &p2)
 Construct the rect from given centers coordinates.
 

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

constexpr SDL2pp::Rect::Rect ( )
inlineconstexpr

Default constructor.

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

◆ Rect() [2/6]

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

Construct a rect from existing SDL_Rect.

Parameters
[in]rectExisting SDL_Rect

◆ Rect() [3/6]

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

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

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

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

◆ Rect() [5/6]

SDL2pp::Rect::Rect ( const Rect )
defaultnoexcept

Copy constructor.

◆ Rect() [6/6]

SDL2pp::Rect::Rect ( Rect &&  )
defaultnoexcept

Move constructor.

Member Function Documentation

◆ Contains() [1/3]

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

Check whether the rect contains given point.

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

◆ Contains() [2/3]

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

Check whether the rect contains another rect.

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

◆ Contains() [3/3]

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

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

◆ 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 ( const Point center,
const Point size 
)
inlinestaticconstexpr

Construct the rect from given center coordinates and size.

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

◆ FromCenter() [2/2]

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

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

◆ FromCorners() [1/2]

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

Construct the rect from given centers coordinates.

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

◆ FromCorners() [2/2]

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

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

◆ GetBottomLeft()

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

Get bottom left corner of the rect.

Returns
bottom left corner of the rect

◆ GetBottomRight()

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

Get bottom right corner of the rect.

Returns
Bottom right corner of the rect

◆ GetCentroid()

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

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
inlineconstexpr

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
inlineconstexpr

Get size of the rect.

Returns
Size of the rect

◆ GetTopLeft()

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

Get top left corner of the rect.

Returns
Top left corner of the rect

◆ GetTopRight()

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

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
inlineconstexpr

Get width of the rect.

Returns
Width of the rect

◆ GetX()

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

Get X coordinate of the rect corner.

Returns
X coordinate of the rect corner

◆ GetX2()

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

Get X coordinate of the rect second corner.

Returns
X coordinate of the rect second corner

◆ GetY()

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

Get Y coordinate of the rect corner.

Returns
Y coordinate of the rect corner

◆ GetY2()

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

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
inlineconstexpr

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
inlineconstexpr

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
inlineconstexpr

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: