-
Notifications
You must be signed in to change notification settings - Fork 1
Rect2
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.
| Field | Type | Description |
|---|---|---|
Position |
Vect2 |
The position (top-left corner) of the rectangle |
Size |
Vect2 |
The size (width and height) of the rectangle |
| 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 |
// 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);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); // trueDetermines 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); // trueDetermines 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); // trueGets 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)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 circleGets 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)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)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)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)Calculates the area of the rectangle.
var rect = new Rect2(10f, 20f, 100f, 50f);
float area = rect.Area(); // 5000| 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 |
- MathHelper
- FileHelper
- HashHelper
- JsonHelper
- MapHelper
- TextHelper
- SoundHelper
- AlignHelpers
- Instance Helper
- Enum Extensions
- String Extensions
- Int Extensions
- Float Extensions
- IEnumerable Extensions
- Random Extensions
- Sound Extensions
- Font Extensions