Skip to content

Core.Data.Rect

Dennis Steffen edited this page Jan 4, 2026 · 1 revision

Rect Struct Documentation

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.

Overview

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.

Key Properties

  • Coordinates: X, Y, Width, Height.
  • Edges: Left, Right (X + Width), Top, Bottom (Y + Height).
  • Spatial Helpers:
    • Location / Position: Returns the top-left corner as a PointInt.
    • Size: Returns the dimensions as a PointInt.
    • Center: Calculates the middle point of the rectangle.
    • IsEmpty: Returns true if all fields are zero.

Core Functionality

1. Containment Checks (Contains)

Determines if a point (int, float, PointInt, or Vector2) or another Rect lies within the boundaries of this rectangle.

2. Collision & Overlap (Intersects & Intersect)

  • Intersects: Returns a boolean indicating if two rectangles overlap.
  • Intersect (Static): Returns a new Rect representing the overlapping area between two rectangles.

3. Transformation

  • 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).

4. Utility

  • Clamp: Restricts a PointInt to 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 RectF when required.

Use Cases

  1. UI Layout: Defining the hitboxes for buttons, panels, or windows in a graphical user interface.
  2. AABB Collision: Basic "Axis-Aligned Bounding Box" detection for 2D game objects or sprites.
  3. Camera Viewports: Defining the visible area of a game world.
  4. Spatial Filtering: Checking if objects are within a specific region of a map to determine if they should be rendered or updated.

Code Examples

Basic Usage and Containment

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!");
}

Intersection and Inflation

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 110x110

Deconstruction and Offset

Rect 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}");

Clone this wiki locally