Skip to content

Core.Data.PointInt

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

PointInt Documentation

The PointInt struct is a lightweight, high-performance value type representing a 2D coordinate or vector using integer precision. It is designed for scenarios where floating-point math is unnecessary or undesirable, such as grid-based logic, pixel coordinates, or discrete spatial indexing.


Key Features

  • Serializable: Decorated with [DataContract] and [DataMember] for easy integration with serialization frameworks.
  • Operator Overloading: Supports standard arithmetic (+, -, *, /) for both PointInt pairs and scalar int values.
  • Deconstruction: Supports C# deconstruction syntax for easy access to X and Y components.
  • Vector Conversion: Includes an optimized ToVector2() method for interoperability with System.Numerics.Vector2.
  • Inlining: Mathematical operations and conversions are optimized for performance.

Use Cases

1. Grid-Based Systems

Perfect for representing indices in a 2D array, tilemap, or board game. Since it uses int, there are no rounding errors when calculating neighbors or offsets.

2. UI and Pixel Alignment

Used to define positions or sizes in a UI system where elements must align perfectly with screen pixels.

3. Spatial Indexing

Useful for "chunking" systems in game engines, where the world is divided into discrete sectors (e.g., Sector 1, 1, Sector 1, 2).


Examples

Basic Arithmetic

You can manipulate coordinates using standard operators.

using Meatcorps.Engine.Core.Data;

var start = new PointInt(10, 10);
var offset = new PointInt(5, -2);

// Point + Point
PointInt destination = start + offset; // {X:15 Y:8}

// Point * Scalar
PointInt scaled = destination * 2;      // {X:30 Y:16}

Deconstruction

Quickly extract values into local variables.

var position = new PointInt(100, 200);

// Use deconstruction syntax
var (x, y) = position;

Console.WriteLine($"X is {x}, Y is {y}");

Interop with Floating Point Math

When you need to move from discrete grid logic to physics or rendering, use ToVector2().

using System.Numerics;

PointInt gridPos = new PointInt(5, 5);

// Convert to Vector2 for distance calculations or rendering
Vector2 worldPos = gridPos.ToVector2(); 

float distance = Vector2.Distance(Vector2.Zero, worldPos);

Comparisons and Equality

The struct implements IEquatable<PointInt> for efficient comparisons, which is useful for using PointInt as a key in a Dictionary.

var p1 = new PointInt(5, 5);
var p2 = new PointInt(5, 5);

if (p1 == p2) 
{
    // Logic for matching coordinates
}

var pointsSeen = new HashSet<PointInt>();
pointsSeen.Add(p1);

API Summary

Member Description
X, Y The integer components of the point.
Zero Returns a PointInt with 0, 0.
new PointInt(int value) Creates a point where X and Y are both set to value.
Deconstruct(...) Allows var (x, y) = point;.
ToVector2() Converts the point to a System.Numerics.Vector2.
ToString() Returns a formatted string: {X:0 Y:0}.

Clone this wiki locally