Nirtcpp 2.1.0
Nirtcpp is a high-performance c++ graphics engine.
Loading...
Searching...
No Matches
nirt::core::rect< T > Class Template Reference

Rectangle template. More...

#include <nirtcpp/core/engine/rect.hpp>

Public Member Functions

 rect ()
 Default constructor creating empty rectangle at (0,0)
 
 rect (T x, T y, T x2, T y2)
 Constructor with two corners.
 
 rect (const position2d< T > &upperLeft, const position2d< T > &lowerRight)
 Constructor with two corners.
 
template<class U >
 rect (const position2d< T > &pos, const dimension2d< U > &size)
 Constructor with upper left corner and dimension.
 
template<class U >
 rect (const dimension2d< U > &size)
 Constructor with upper left at 0,0 and lower right using dimension.
 
rect< Toperator+ (const position2d< T > &pos) const
 move right by given numbers
 
rect< T > & operator+= (const position2d< T > &pos)
 move right by given numbers
 
rect< Toperator- (const position2d< T > &pos) const
 move left by given numbers
 
rect< T > & operator-= (const position2d< T > &pos)
 move left by given numbers
 
bool operator== (const rect< T > &other) const
 equality operator
 
bool operator!= (const rect< T > &other) const
 inequality operator
 
bool operator< (const rect< T > &other) const
 compares size of rectangles
 
T getArea () const
 Returns size of rectangle.
 
bool isPointInside (const position2d< T > &pos) const
 Returns if a 2d point is within this rectangle.
 
bool isRectCollided (const rect< T > &other) const
 Check if the rectangle collides with another rectangle.
 
void clipAgainst (const rect< T > &other)
 Clips this rectangle with another one.
 
bool constrainTo (const rect< T > &other)
 Moves this rectangle to fit inside another one.
 
T getWidth () const
 Get width of rectangle.
 
T getHeight () const
 Get height of rectangle.
 
void repair ()
 If the lower right corner of the rect is smaller then the upper left, the points are swapped.
 
bool isValid () const
 Returns if the rect is valid to draw.
 
position2d< TgetCenter () const
 Get the center of the rectangle.
 
dimension2d< TgetSize () const
 Get the dimensions of the rectangle.
 
void addInternalPoint (const position2d< T > &p)
 Adds a point to the rectangle.
 
void addInternalPoint (T x, T y)
 Adds a point to the bounding rectangle.
 

Public Attributes

position2d< TUpperLeftCorner
 Upper left corner.
 
position2d< TLowerRightCorner
 Lower right corner.
 

Detailed Description

template<class T>
class nirt::core::rect< T >

Rectangle template.

Mostly used by 2D GUI elements and for 2D drawing methods. It has 2 positions instead of position and dimension and a fast method for collision detection with other rectangles and points.

Coordinates are (0,0) for top-left corner, and increasing to the right and to the bottom.

Member Function Documentation

◆ addInternalPoint() [1/2]

template<class T >
void nirt::core::rect< T >::addInternalPoint ( const position2d< T > &  p)
inline

Adds a point to the rectangle.

Causes the rectangle to grow bigger if point is outside of the box

Parameters
pPoint to add to the box.

◆ addInternalPoint() [2/2]

template<class T >
void nirt::core::rect< T >::addInternalPoint ( T  x,
T  y 
)
inline

Adds a point to the bounding rectangle.

Causes the rectangle to grow bigger if point is outside of the box

Parameters
xX-Coordinate of the point to add to this box.
yY-Coordinate of the point to add to this box.

◆ clipAgainst()

template<class T >
void nirt::core::rect< T >::clipAgainst ( const rect< T > &  other)
inline

Clips this rectangle with another one.

Parameters
otherRectangle to clip with

◆ constrainTo()

template<class T >
bool nirt::core::rect< T >::constrainTo ( const rect< T > &  other)
inline

Moves this rectangle to fit inside another one.

Returns
True on success, false if not possible

◆ isPointInside()

template<class T >
bool nirt::core::rect< T >::isPointInside ( const position2d< T > &  pos) const
inline

Returns if a 2d point is within this rectangle.

Parameters
posPosition to test if it lies within this rectangle.
Returns
True if the position is within the rectangle, false if not.

◆ isRectCollided()

template<class T >
bool nirt::core::rect< T >::isRectCollided ( const rect< T > &  other) const
inline

Check if the rectangle collides with another rectangle.

Parameters
otherRectangle to test collision with
Returns
True if the rectangles collide.

◆ isValid()

template<class T >
bool nirt::core::rect< T >::isValid ( ) const
inline

Returns if the rect is valid to draw.

It would be invalid if the UpperLeftCorner is lower or more right than the LowerRightCorner.


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

Nirtcpp    @cppfx.xyz

Esvcpp    esv::print