Skip to content
Shmellyorc edited this page Aug 31, 2026 · 3 revisions

Rect2 is a 2D axis-aligned rectangle structure defined by a position and size. It is used extensively for collision detection, viewport calculations, UI layout, and spatial partitioning.


Fields

Field Type Description
Position Vect2 The position (top-left corner) of the rectangle
Size Vect2 The size (width and height) of the rectangle

Properties

Property Description
X The X-coordinate of the rectangle's position
Y The Y-coordinate of the rectangle's position
Width The width of the rectangle
Height The height of the rectangle
Top The Y-coordinate of the top edge
Left The X-coordinate of the left edge
Right The X-coordinate of the right edge
Bottom The Y-coordinate of the bottom edge
Center The center point of the rectangle
TopLeft The top-left corner of the rectangle
TopRight The top-right corner of the rectangle
BottomLeft The bottom-left corner of the rectangle
BottomRight The bottom-right corner of the rectangle
Empty An empty rectangle with position (0,0) and size (0,0)
IsEmpty Returns true if the rectangle has zero size

Constructors

// Create from position and size
var rect1 = new Rect2(new Vect2(10f, 20f), new Vect2(100f, 50f));

// Create from component values
var rect2 = new Rect2(10f, 20f, 100f, 50f);

Contains

Determines whether a point or rectangle is inside the rectangle.

var rect = new Rect2(10f, 20f, 100f, 50f);
var point = new Vect2(50f, 30f);

bool contains = rect.Contains(point);  // true

var other = new Rect2(20f, 30f, 50f, 20f);
bool containsRect = rect.Contains(other);  // true

Intersects

Determines whether two rectangles intersect.

var rect1 = new Rect2(10f, 10f, 50f, 50f);
var rect2 = new Rect2(30f, 30f, 50f, 50f);

bool intersects = rect1.Intersects(rect2);  // true

Circle Intersection

Determines whether the rectangle intersects with a circle.

var rect = new Rect2(10f, 10f, 50f, 50f);
var center = new Vect2(35f, 35f);
float radius = 20f;

bool intersects = rect.Intersects(center, radius);  // true

Intersection

Gets the intersection rectangle between two rectangles.

var rect1 = new Rect2(10f, 10f, 50f, 50f);
var rect2 = new Rect2(30f, 30f, 50f, 50f);

var intersection = rect1.Intersection(rect2);  // (30, 30, 30, 30)

Circle Intersection

Gets the intersection rectangle between a rectangle and a circle.

var rect = new Rect2(10f, 10f, 50f, 50f);
var center = new Vect2(35f, 35f);
float radius = 20f;

var intersection = rect.Intersection(center, radius);
// Returns the overlapping region between the rectangle and circle

Union

Gets the smallest rectangle that contains both rectangles.

var rect1 = new Rect2(10f, 10f, 50f, 50f);
var rect2 = new Rect2(30f, 30f, 50f, 50f);

var union = rect1.Union(rect2);  // (10, 10, 70, 70)

Inflate

Expands the rectangle by a specified amount on all sides.

var rect = new Rect2(10f, 20f, 100f, 50f);
var inflated = rect.Inflate(5f);  // (5, 15, 110, 60)

var inflated2 = rect.Inflate(5f, 10f);  // (5, 10, 110, 70)

Offset

Offsets the rectangle by a specified vector.

var rect = new Rect2(10f, 20f, 100f, 50f);
var offset = rect.Offset(new Vect2(5f, -5f));  // (15, 15, 100, 50)

Move

Moves the rectangle to a new position while maintaining its size.

var rect = new Rect2(10f, 20f, 100f, 50f);
var moved = rect.Move(new Vect2(50f, 50f));  // (50, 50, 100, 50)

Area

Calculates the area of the rectangle.

var rect = new Rect2(10f, 20f, 100f, 50f);
float area = rect.Area();  // 5000

Summary

Method Description
Contains Determines if a point or rectangle is inside
Intersects Determines if two rectangles intersect
Intersects(center, radius) Determines if rectangle intersects with a circle
Intersection Returns the intersection rectangle
Intersection(center, radius) Returns intersection between rectangle and circle
Union Returns the union rectangle
Inflate Expands the rectangle by an amount
Offset Offsets the rectangle by a vector
Move Moves the rectangle to a new position
Area Calculates the area of the rectangle

Back to Math Library

Clone this wiki locally