-
Notifications
You must be signed in to change notification settings - Fork 0
Core.Data.Rect
Dennis Steffen edited this page Jan 4, 2026
·
1 revision
The Rect struct is a core data structure in the Meatcorps.Engine.Core.Data namespace. It represents a 2D rectangle defined by an integer-based position (X, Y) and size (Width, Height). It is designed for efficiency and is commonly used for UI layouts, collision detection, and spatial partitioning.
The Rect struct provides a comprehensive set of properties and methods to manipulate and query rectangular areas. It is marked with [DataContract] and [DataMember] attributes, making it compatible with serialization.
-
Coordinates:
X,Y,Width,Height. -
Edges:
Left,Right(X + Width),Top,Bottom(Y + Height). -
Spatial Helpers:
-
Location/Position: Returns the top-left corner as aPointInt. -
Size: Returns the dimensions as aPointInt. -
Center: Calculates the middle point of the rectangle. -
IsEmpty: Returnstrueif all fields are zero.
-
Determines if a point (int, float, PointInt, or Vector2) or another Rect lies within the boundaries of this rectangle.
- Intersects: Returns a boolean indicating if two rectangles overlap.
-
Intersect (Static): Returns a new
Rectrepresenting the overlapping area between two rectangles.
- Inflate: Expands or shrinks the rectangle from its center by a specified horizontal and vertical amount.
- Offset: Moves the rectangle by a specific amount without changing its size.
- Union (Static): Creates the smallest possible rectangle that contains both input rectangles (a bounding box).
-
Clamp: Restricts a
PointIntto stay within the bounds of the rectangle. -
Deconstruct: Allows for easy tuple-like deconstruction:
var (x, y, w, h) = myRect;. -
Implicit Conversion: Automatically converts to a
RectFwhen required.
- UI Layout: Defining the hitboxes for buttons, panels, or windows in a graphical user interface.
- AABB Collision: Basic "Axis-Aligned Bounding Box" detection for 2D game objects or sprites.
- Camera Viewports: Defining the visible area of a game world.
- Spatial Filtering: Checking if objects are within a specific region of a map to determine if they should be rendered or updated.
using Meatcorps.Engine.Core.Data;
// Create a rectangle at (10, 10) with size 50x50
Rect playerBounds = new Rect(10, 10, 50, 50);
// Check if a point is inside
PointInt mousePos = new PointInt(20, 20);
if (playerBounds.Contains(mousePos))
{
Console.WriteLine("Mouse is hovering over the player!");
}Rect rectA = new Rect(0, 0, 100, 100);
Rect rectB = new Rect(50, 50, 100, 100);
// Find the overlapping area
if (rectA.Intersects(rectB))
{
Rect overlap = Rect.Intersect(rectA, rectB);
// overlap will be at (50, 50) with size 50x50
}
// Expand the rectangle by 5 pixels on all sides
rectA.Inflate(5, 5);
// rectA is now at (-5, -5) with size 110x110Rect area = new Rect(10, 20, 200, 150);
// Move the area
area.Offset(10, -5);
// Extract values easily
var (x, y, width, height) = area;
Console.WriteLine($"Position: {x}, {y}");