Skip to content

Core.Data.RectF

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

RectF Structure Documentation

The RectF structure, located in the Meatcorps.Engine.Core.Data namespace, is a lightweight, floating-point representation of a 2D rectangle. It is a fundamental data type used throughout the engine for spatial calculations, UI layout, and collision detection.

Overview

RectF defines a rectangular area using four primary components:

  • X: The horizontal position of the top-left corner.
  • Y: The vertical position of the top-left corner.
  • Width: The horizontal span of the rectangle.
  • Height: The vertical span of the rectangle.

The structure is marked with [DataContract], making it suitable for serialization, and implements IEquatable<RectF> for efficient comparisons.


Key Properties

Spatial Edges

  • Left / Top: Returns the X and Y coordinates respectively.
  • Right / Bottom: Calculates the far edges (X + Width and Y + Height).

Coordinate Helpers

  • Position: Gets or sets the top-left corner as a Vector2.
  • Size: Gets or sets the dimensions as a SizeF.
  • Center: Returns the Vector2 representing the exact middle of the rectangle.
  • Corners: Provides quick access to TopLeft, TopRight, BottomLeft, and BottomRight as Vector2 points.

Common Use Cases

  1. Collision Detection: Determining if two objects overlap or if a point (like a mouse cursor) is within a specific boundary.
  2. UI Layout: Defining the bounds of buttons, panels, or text areas.
  3. Camera/Frustum Culling: Calculating the bounding box of a group of objects to determine if they are visible on screen.
  4. Physics & Spatial Queries: Finding the closest point on a boundary or measuring distances to objects.

Examples

1. Basic Creation and Containment

Checking if a point (e.g., a mouse click) is inside a UI element.

using Meatcorps.Engine.Core.Data;
using System.Numerics;

// Create a rectangle at (10, 10) with a size of 100x50
RectF buttonBounds = new RectF(10f, 10f, 100f, 50f);

Vector2 clickPosition = new Vector2(50f, 25f);

if (buttonBounds.Contains(clickPosition))
{
    Console.WriteLine("Button clicked!");
}

2. Collision Check (Intersection)

Checking if two game objects are overlapping.

RectF playerBounds = new RectF(0f, 0f, 32f, 32f);
RectF enemyBounds = new RectF(20f, 20f, 32f, 32f);

if (playerBounds.Intersects(enemyBounds))
{
    Console.WriteLine("Collision detected!");
}

// Get the actual overlapping area
RectF overlap = RectF.Intersection(playerBounds, enemyBounds);

3. Expanding a Rectangle

Using Inflate to grow a rectangle outward in all directions.

RectF zone = new RectF(100f, 100f, 50f, 50f);

// Expand by 10 units on both sides (Total width becomes 70, height 70)
// The X and Y will shift to (90, 90) to keep it centered
zone.Inflate(10f, 10f);

4. Bounding Multiple Rectangles

Finding a single rectangle that encompasses multiple other rectangles.

RectF rect1 = new RectF(0, 0, 10, 10);
RectF rect2 = new RectF(50, 50, 10, 10);

RectF combined = RectF.RectBounds(rect1, rect2);
// combined will start at (0,0) and have a width/height of 60

Technical Notes

  • Precision: The structure uses float. For comparisons, it utilizes an extension method EqualsSafe (likely handling small floating-point epsilon differences) to ensure stability.
  • Memory: As a struct, it is passed by value. For performance-critical code, many methods offer ref and out overloads (e.g., Intersects(ref RectF first, ref RectF second)) to avoid unnecessary copying.
  • Conversion: It provides implicit conversion from Rect (integer-based) and explicit conversion back to Rect.

Clone this wiki locally