Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
donner::Box< T > Struct Template Reference

A 2D axis-aligned bounding box. More...

#include "donner/base/Box.h"

Public Member Functions

 Box ()
 Default constructor: Creates an empty box centered on (0, 0).
 
 Box (const Vector2< T > &topLeft, const Vector2< T > &bottomRight)
 Construct a new box with the given top-left and bottom-right corners.
 
 ~Box ()=default
 Destructor.
 
 Box (const Box< T > &other)=default
 Copy constructor.
 
Box< T > & operator= (const Box< T > &other)=default
 Copy assignment operator.
 
 Box (Box< T > &&other) noexcept=default
 Move constructor.
 
Box< T > & operator= (Box< T > &&other) noexcept=default
 Move assignment operator.
 
void addPoint (const Vector2< T > &point)
 Expand to include the provided point.
 
void addBox (const Box< T > &box)
 Adds a bounding box inside this bounding box.
 
Box toOrigin () const
 Return a box with the same size but moved to the origin, i.e.
 
width () const
 Returns the box width.
 
height () const
 Returns the box height.
 
Vector2< T > size () const
 Returns the box size.
 
bool isEmpty () const
 Returns true if the box has zero width or height.
 
bool contains (const Vector2< T > &point) const
 Returns true if the box contains the given point.
 
Box< T > inflatedBy (T amount) const
 Inflates the box size by the given amount in all directions.
 
Box< T > operator- (const Vector2< T > &vec) const
 Return the box moved by subtracting the given vector.
 
Box< T > & operator-= (const Vector2< T > &vec)
 Move the box by subtracting the given vector.
 
Box< T > operator+ (const Vector2< T > &vec) const
 Return the box moved by adding the given vector.
 
Box< T > & operator+= (const Vector2< T > &vec)
 Move the box by adding the given vector.
 
bool operator== (const Box< T > &other) const
 Equality operator.
 
bool operator!= (const Box< T > &rhs) const
 Inequality operator.
 

Static Public Member Functions

static Box< T > FromXYWH (T x, T y, T width, T height)
 Creates a Box from x, y, width, and height.
 
static Box< T > CreateEmpty (const Vector2< T > &point)
 Create an empty box that is centered on the given point.
 
static Box< T > WithSize (const Vector2< T > &size)
 Create a box with the given size, with the top-left corner at the origin.
 
static Box< T > Union (const Box< T > &a, const Box< T > &b)
 Create a new box that is expanded to include both boxes.
 

Public Attributes

Vector2< T > topLeft
 The top-left corner of the box.
 
Vector2< T > bottomRight
 The bottom-right corner of the box.
 

Friends

std::ostream & operator<< (std::ostream &os, const Box< T > &box)
 Ostream output operator.
 

Detailed Description

template<typename T>
struct donner::Box< T >

A 2D axis-aligned bounding box.

For example, to construct a box, add points to it, and query its size:

Boxd box(Vector2d(-1.0, -1.0), Vector2d(1.0, 1.0));
box.addPoint(Vector2d(2.0, 0.0));
assert(box == Boxd(Vector2d(-1.0, -1.0), Vector2d(2.0, 1.0)));
assert(box.size() == Vector2d(3.0, 2.0));
Vector2< double > Vector2d
Shorthand for Vector2<double>
Definition Vector2.h:394
Box< double > Boxd
Shorthand for Box<double>.
Definition Box.h:211
Template Parameters
TElement type, e.g. double, float, int, etc.

Constructor & Destructor Documentation

◆ Box() [1/3]

template<typename T >
donner::Box< T >::Box ( const Vector2< T > & topLeft,
const Vector2< T > & bottomRight )
inline

Construct a new box with the given top-left and bottom-right corners.

Parameters
topLeftTop-left corner.
bottomRightBottom-right corner.

◆ Box() [2/3]

template<typename T >
donner::Box< T >::Box ( const Box< T > & other)
default

Copy constructor.

Parameters
otherOther box.

◆ Box() [3/3]

template<typename T >
donner::Box< T >::Box ( Box< T > && other)
defaultnoexcept

Move constructor.

Parameters
otherOther box.

Member Function Documentation

◆ addBox()

template<typename T >
void donner::Box< T >::addBox ( const Box< T > & box)
inline

Adds a bounding box inside this bounding box.

Parameters
boxBox to add.

◆ addPoint()

template<typename T >
void donner::Box< T >::addPoint ( const Vector2< T > & point)
inline

Expand to include the provided point.

Parameters
pointPoint to include.

◆ contains()

template<typename T >
bool donner::Box< T >::contains ( const Vector2< T > & point) const
inline

Returns true if the box contains the given point.

Parameters
pointPoint to check.

◆ CreateEmpty()

template<typename T >
static Box< T > donner::Box< T >::CreateEmpty ( const Vector2< T > & point)
inlinestatic

Create an empty box that is centered on the given point.

Parameters
pointCenter point.

◆ FromXYWH()

template<typename T >
static Box< T > donner::Box< T >::FromXYWH ( T x,
T y,
T width,
T height )
inlinestatic

Creates a Box from x, y, width, and height.

Parameters
xX coordinate of the top-left corner.
yY coordinate of the top-left corner.
widthWidth of the box.
heightHeight of the box.

◆ inflatedBy()

template<typename T >
Box< T > donner::Box< T >::inflatedBy ( T amount) const
inline

Inflates the box size by the given amount in all directions.

Parameters
amountAmount to inflate the box by.

◆ operator=() [1/2]

template<typename T >
Box< T > & donner::Box< T >::operator= ( Box< T > && other)
defaultnoexcept

Move assignment operator.

Parameters
otherOther box.

◆ operator=() [2/2]

template<typename T >
Box< T > & donner::Box< T >::operator= ( const Box< T > & other)
default

Copy assignment operator.

Parameters
otherOther box.

◆ toOrigin()

template<typename T >
Box donner::Box< T >::toOrigin ( ) const
inline

Return a box with the same size but moved to the origin, i.e.

with the top-left corner at (0, 0).

◆ Union()

template<typename T >
static Box< T > donner::Box< T >::Union ( const Box< T > & a,
const Box< T > & b )
inlinestatic

Create a new box that is expanded to include both boxes.

Parameters
aFirst box.
bSecond box.

◆ WithSize()

template<typename T >
static Box< T > donner::Box< T >::WithSize ( const Vector2< T > & size)
inlinestatic

Create a box with the given size, with the top-left corner at the origin.

Parameters
sizeSize of the box.

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