API Rect2 - shmellyorc/Box GitHub Wiki

Rect2

Namespace: Box.Systems

Represents a 2D rectangle used for pixel detection, collision, area calculations, and other utility methods.


Constructors

public Rect2(Rect2 value);
public Rect2(Vect2 position, Vect2 size);
public Rect2(float x, float y, float width, float height);

Deep-clones an existing rectangle.


Static Properties

Name Type Description
Empty Rect2 (0,0,0,0) empty rectangle
One Rect2 (0,0,1,1) unit rectangle

Instance Properties

Name Type Description
float X float X-coordinate of top-left corner
float Y float Y-coordinate of top-left corner
float Width float Width of the rectangle
float Height float Height of the rectangle
float Top float Same as Y
float Left float Same as X
float Right float X + Width
float Bottom float Y + Height
bool IsEmpty bool True if width or height ≤ 0
Vect2 Center Vect2 Center point (X + Width/2, Y + Height/2)
Vect2 Position Vect2 Gets/sets top-left corner as a Vect2 (clamps both)
Vect2 Size Vect2 Gets/sets (Width, Height) as a Vect2

Operators

Operator Description
==, != Equality/Inequality by (X, Y, Width, Height)
+ Vect2, - Vect2 Expand/shrink by vector
<, >, <=, >= Compare areas

Methods

Signature Description
float Area() / static float Area(Rect2 rect) Computes area (Width * Height).
Rect2 Expand(float w, float h) / static Rect2 Expand(Rect2 r, float w, float h) Expands or shrinks edges by (w, h).
bool Contains(float x, float y) / Contains(Vect2 p) Point-in-rect test
bool Intersects(Rect2 other) / static bool Intersects(Rect2 a, Rect2 b) Rect-rect intersection
Rect2 Merge(Rect2 other) / static Rect2 Merge(Rect2 a, Rect2 b) Encompasses both rectangles

Usage Example

// create at (10,20), size 100x50
var rect = new Rect2(10, 20, 100, 50);

// move it
rect.Position = new Vect2(50, 75);

// expand by 5 pixels each side
var expanded = rect + new Vect2(5);

// check if a point lies within
bool inside = rect.Contains(60, 80);

// compute area and intersection
float area = rect.Area();
var other = new Rect2(new Vect2(40,60), new Vect2(20,20));
bool hit = rect.Intersects(other);

// merge two rectangles
auto union = Rect2.Merge(rect, other);
⚠️ **GitHub.com Fallback** ⚠️