Skip to content

Core.Data.SizeF

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

SizeF Structure

The SizeF structure is a part of the Meatcorps.Engine.Core.Data namespace. It represents a 2D size defined by a floating-point Width and Height. It is designed for use in scenarios like UI layout, game object dimensions, and rendering where non-negative dimensions are required.

Key Features

  • Non-negative Enforcement: The Width and Height properties automatically clamp values to a minimum of 0 using Math.Max(0, value). This prevents "negative sizes" which can cause issues in rendering and layout logic.
  • Vector2 Integration: It provides seamless interoperability with System.Numerics.Vector2, including implicit conversions and mathematical operators.
  • Serialization Ready: Decorated with [DataContract] and [DataMember] attributes, making it compatible with various .NET serializers.
  • Comprehensive Operators: Supports addition, subtraction, multiplication, and division with other SizeF instances, float scalars, and Vector2 offsets.

Use Cases

  1. UI Layout: Defining the dimensions of buttons, panels, or text containers where dimensions cannot be less than zero.
  2. Sprite/Texture Dimensions: Storing the logical size of a game entity independent of its screen resolution.
  3. Scaling Logic: Using scalars to uniformly resize elements (e.g., doubling the size of a window).
  4. Collision Bounds: Representing the extent of an Axis-Aligned Bounding Box (AABB) when paired with a position vector.

Examples

1. Basic Initialization and Safety

The structure ensures that dimensions never drop below zero.

using Meatcorps.Engine.Core.Data;

// Normal initialization
var size = new SizeF(100.5f, 50.0f);

// Attempting to set negative values results in 0
size.Width = -10.0f; 
Console.WriteLine(size.Width); // Output: 0

2. Mathematical Operations

You can easily scale or adjust sizes using standard operators.

var baseSize = new SizeF(10.0f, 10.0f);

// Multiplication by scalar (Scaling)
var doubleSize = baseSize * 2.0f; // {Width:20 Height:20}

// Addition with a Vector2 (Adjusting width/height by specific amounts)
var offset = new Vector2(5.0f, -2.0f);
var adjustedSize = baseSize + offset; // {Width:15 Height:8}

3. Vector2 Interoperability

Since SizeF behaves similarly to a vector, it can be implicitly converted.

SizeF size = new SizeF(1920, 1080);

// Implicit conversion to Vector2
Vector2 vec = size; 

// Explicit conversion back
SizeF sizeFromVec = (SizeF)new Vector2(800, 600);

4. Comparison and Constants

Standard equality checks and predefined constants are available.

if (mySize == SizeF.Zero) 
{
    // Handle empty size
}

bool isSame = new SizeF(5, 5).Equals(SizeF.One * 5f); // True

Technical Summary

Member Description
Width / Height Properties that clamp input values to [0, float.MaxValue].
ToVector2() Converts the size to a standard Vector2.
Implicit Operators Allows seamless transition between SizeF and Vector2.
SizeF.Zero Static shortcut for new SizeF(0, 0).
SizeF.One Static shortcut for new SizeF(1, 1).

Clone this wiki locally