Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Infinity::Types::Math::Rect Struct Reference

Axis-aligned rectangle defined by left, right, top, and bottom edges. More...

#include <Rect.hpp>

Public Member Functions

constexpr Rect ()
 Default constructor.
 
constexpr Rect (const Rect &r)
 Copy constructor.
 
constexpr Rect (float l, float r, float t, float b)
 Constructs a rectangle from edge positions.
 
constexpr Rectoperator= (const Rect &)=default
 Copy assignment operator.
 
void set (float in_l, float in_r, float in_t, float in_b)
 Sets all four edge positions.
 
Vector2 topLeft () const
 Gets the top-left corner position.
 
Vector2 topRight () const
 Gets the top-right corner position.
 
Vector2 bottomLeft () const
 Gets the bottom-left corner position.
 
Vector2 bottomRight () const
 Gets the bottom-right corner position.
 
float width () const
 Computes the width of the rectangle.
 
float height () const
 Computes the height of the rectangle.
 
float area () const
 Computes the area of the rectangle.
 
bool contains (const Vector2 &p) const
 Tests if a point is contained within the rectangle.
 
Rect translated (const Vector2 &offset) const
 Creates a translated copy of this rectangle.
 
Rect scaled (float factor) const
 Creates a scaled copy of this rectangle.
 
Vector2 center () const
 Computes the center point of the rectangle.
 
Vector2 size () const
 Gets the size of the rectangle as a vector.
 
Rectoperator+= (const Rect &rhs)
 Addition assignment with another rectangle.
 
Rectoperator+= (const Vector2 &rhs)
 Addition assignment with a vector.
 
Rectoperator-= (const Rect &rhs)
 Subtraction assignment with another rectangle.
 
Rectoperator-= (const Vector2 &rhs)
 Subtraction assignment with a vector.
 
 operator bool () const noexcept
 Boolean conversion operator.
 

Static Public Member Functions

static Rect fromSize (float x, float y, float width, float height)
 Creates a rectangle from position and size.
 
static constexpr Rect zero ()
 Creates a zero-sized rectangle at the origin.
 

Public Attributes

float l
 Left edge position.
 
float r
 Right edge position.
 
float t
 Top edge position.
 
float b
 Bottom edge position.
 

Detailed Description

Axis-aligned rectangle defined by left, right, top, and bottom edges.

Rect represents a 2D axis-aligned rectangular region, storing the positions of its four edges (left, right, top, bottom). This representation is flexible and supports various coordinate conventions depending on the application.

Common uses in procedural generation:

  • Bounding boxes for 2D geometry
  • UV texture coordinate regions
  • Screen space regions for UI and rendering
  • Tile boundaries in procedural maps
  • Sampling regions for texture generation
  • Spatial partitioning bounds
  • Viewport and clipping rectangles

The rectangle can be interpreted in different coordinate systems:

  • Screen space: top < bottom (Y increases downward)
  • Cartesian space: top > bottom (Y increases upward)
  • UV space: typically [0,1] range with various conventions

Example usage:

// Create a rectangle from edges
Rect region(0.0f, 100.0f, 0.0f, 50.0f); // l=0, r=100, t=0, b=50
// Create from position and size
Rect tile = Rect::fromSize(10.0f, 20.0f, 32.0f, 32.0f);
// Query properties
float w = region.width();
float h = region.height();
float area = region.area();
Vector2 center = region.center();
// Test containment
Vector2 point(50.0f, 25.0f);
if (region.contains(point)) {
// Point is inside rectangle
}
// Transform rectangles
Rect moved = region.translated(Vector2(10, 10));
Rect scaled = region.scaled(2.0f);
// Get corner positions
Vector2 topLeft = region.topLeft();
Vector2 bottomRight = region.bottomRight();
t_Vector2< float > Vector2
Alias for t_Vector2<float>, the default 2D vector type.
Definition Vector2.hpp:487
Axis-aligned rectangle defined by left, right, top, and bottom edges.
Definition Rect.hpp:70
float area() const
Computes the area of the rectangle.
Vector2 center() const
Computes the center point of the rectangle.
static Rect fromSize(float x, float y, float width, float height)
Creates a rectangle from position and size.
Rect scaled(float factor) const
Creates a scaled copy of this rectangle.
Vector2 bottomRight() const
Gets the bottom-right corner position.
Rect translated(const Vector2 &offset) const
Creates a translated copy of this rectangle.
Vector2 topLeft() const
Gets the top-left corner position.
Template structure representing a 2-component vector.
Definition Vector2.hpp:75
Note
The rectangle is defined by edge positions, not by position + size. Use fromSize() if you prefer position/size construction.
No assumptions are made about coordinate system conventions. The caller determines whether top < bottom or top > bottom based on their coordinate system.
See also
Vector2

Constructor & Destructor Documentation

◆ Rect() [1/3]

constexpr Infinity::Types::Math::Rect::Rect ( )
inlineconstexpr

Default constructor.

Creates a zero-sized rectangle at the origin (all edges at 0).

◆ Rect() [2/3]

constexpr Infinity::Types::Math::Rect::Rect ( const Rect r)
inlineconstexpr

Copy constructor.

Parameters
rRectangle to copy from.

◆ Rect() [3/3]

constexpr Infinity::Types::Math::Rect::Rect ( float  l,
float  r,
float  t,
float  b 
)
inlineconstexpr

Constructs a rectangle from edge positions.

Parameters
lLeft edge position.
rRight edge position.
tTop edge position.
bBottom edge position.
// Screen space rectangle (top < bottom)
Rect screen(0.0f, 800.0f, 0.0f, 600.0f);
// Cartesian space rectangle (top > bottom)
Rect cartesian(0.0f, 10.0f, 10.0f, 0.0f);

Member Function Documentation

◆ area()

float Infinity::Types::Math::Rect::area ( ) const

Computes the area of the rectangle.

Returns
Area (width * height).
Note
May be negative depending on edge ordering.

◆ bottomLeft()

Vector2 Infinity::Types::Math::Rect::bottomLeft ( ) const

Gets the bottom-left corner position.

Returns
Vector2 containing (left, bottom).

◆ bottomRight()

Vector2 Infinity::Types::Math::Rect::bottomRight ( ) const

Gets the bottom-right corner position.

Returns
Vector2 containing (right, bottom).

◆ center()

Vector2 Infinity::Types::Math::Rect::center ( ) const

Computes the center point of the rectangle.

Returns
Vector2 containing the midpoint ((left+right)/2, (top+bottom)/2).
Rect bounds(0, 100, 0, 50);
Vector2 center = bounds.center(); // (50, 25)

◆ contains()

bool Infinity::Types::Math::Rect::contains ( const Vector2 p) const

Tests if a point is contained within the rectangle.

Tests if the point's coordinates are within the bounds defined by the rectangle's edges. The test assumes left < right and top < bottom.

Parameters
pPoint to test.
Returns
true if the point is inside or on the rectangle boundary.
Rect bounds(0, 100, 0, 100);
Vector2 inside(50, 50);
Vector2 outside(150, 150);
if (bounds.contains(inside)) {
// Point is inside
}

◆ fromSize()

static Rect Infinity::Types::Math::Rect::fromSize ( float  x,
float  y,
float  width,
float  height 
)
static

Creates a rectangle from position and size.

Constructs a rectangle given a top-left corner position and dimensions. Assumes a coordinate system where X increases rightward and Y increases downward (typical screen space convention).

Parameters
xLeft edge position (X coordinate of top-left corner).
yTop edge position (Y coordinate of top-left corner).
widthWidth of the rectangle (right = x + width).
heightHeight of the rectangle (bottom = y + height).
Returns
Rectangle with the specified position and size.
// Create a 100x50 rectangle at position (10, 20)
Rect tile = Rect::fromSize(10.0f, 20.0f, 100.0f, 50.0f);
// Result: l=10, r=110, t=20, b=70

◆ height()

float Infinity::Types::Math::Rect::height ( ) const

Computes the height of the rectangle.

Returns
Height (bottom - top).
Note
May be negative if bottom < top (e.g., in Cartesian coordinates).

◆ operator bool()

Infinity::Types::Math::Rect::operator bool ( ) const
explicitnoexcept

Boolean conversion operator.

Returns true if the rectangle is non-zero (any edge is non-zero).

Returns
true if rectangle is non-zero, false if zero rectangle.

◆ operator+=() [1/2]

Rect & Infinity::Types::Math::Rect::operator+= ( const Rect rhs)

Addition assignment with another rectangle.

Adds the edges of another rectangle to this rectangle's edges.

Parameters
rhsRectangle to add.
Returns
Reference to this rectangle.

◆ operator+=() [2/2]

Rect & Infinity::Types::Math::Rect::operator+= ( const Vector2 rhs)

Addition assignment with a vector.

Translates the rectangle by adding the vector to all edges.

Parameters
rhsTranslation offset.
Returns
Reference to this rectangle.

◆ operator-=() [1/2]

Rect & Infinity::Types::Math::Rect::operator-= ( const Rect rhs)

Subtraction assignment with another rectangle.

Subtracts the edges of another rectangle from this rectangle's edges.

Parameters
rhsRectangle to subtract.
Returns
Reference to this rectangle.

◆ operator-=() [2/2]

Rect & Infinity::Types::Math::Rect::operator-= ( const Vector2 rhs)

Subtraction assignment with a vector.

Translates the rectangle by subtracting the vector from all edges.

Parameters
rhsTranslation offset.
Returns
Reference to this rectangle.

◆ operator=()

constexpr Rect & Infinity::Types::Math::Rect::operator= ( const Rect )
constexprdefault

Copy assignment operator.

◆ scaled()

Rect Infinity::Types::Math::Rect::scaled ( float  factor) const

Creates a scaled copy of this rectangle.

Returns a new rectangle with all edge positions scaled by the given factor. This scales around the origin (0, 0), not around the rectangle's center.

Parameters
factorScale factor to apply to all edges.
Returns
New rectangle with scaled edge positions.
Rect original(10, 20, 10, 20);
Rect scaled = original.scaled(2.0f);
// scaled: l=20, r=40, t=20, b=40

◆ set()

void Infinity::Types::Math::Rect::set ( float  in_l,
float  in_r,
float  in_t,
float  in_b 
)

Sets all four edge positions.

Parameters
in_lLeft edge position.
in_rRight edge position.
in_tTop edge position.
in_bBottom edge position.

◆ size()

Vector2 Infinity::Types::Math::Rect::size ( ) const

Gets the size of the rectangle as a vector.

Returns
Vector2 containing (width, height).

◆ topLeft()

Vector2 Infinity::Types::Math::Rect::topLeft ( ) const

Gets the top-left corner position.

Returns
Vector2 containing (left, top).

◆ topRight()

Vector2 Infinity::Types::Math::Rect::topRight ( ) const

Gets the top-right corner position.

Returns
Vector2 containing (right, top).

◆ translated()

Rect Infinity::Types::Math::Rect::translated ( const Vector2 offset) const

Creates a translated copy of this rectangle.

Returns a new rectangle with all edges offset by the given vector.

Parameters
offsetTranslation offset to apply.
Returns
New rectangle translated by offset.
Rect original(0, 10, 0, 10);
Rect moved = original.translated(Vector2(5, 5));
// moved: l=5, r=15, t=5, b=15

◆ width()

float Infinity::Types::Math::Rect::width ( ) const

Computes the width of the rectangle.

Returns
Width (right - left).
Note
May be negative if right < left.

◆ zero()

static constexpr Rect Infinity::Types::Math::Rect::zero ( )
inlinestaticconstexpr

Creates a zero-sized rectangle at the origin.

Returns
Rectangle with all edges at 0.

Member Data Documentation

◆ b

float Infinity::Types::Math::Rect::b

Bottom edge position.

◆ l

float Infinity::Types::Math::Rect::l

Left edge position.

◆ r

float Infinity::Types::Math::Rect::r

Right edge position.

◆ t

float Infinity::Types::Math::Rect::t

Top edge position.