-
Notifications
You must be signed in to change notification settings - Fork 0
Core.Data.PointInt
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.
-
Serializable: Decorated with
[DataContract]and[DataMember]for easy integration with serialization frameworks. -
Operator Overloading: Supports standard arithmetic (
+,-,*,/) for bothPointIntpairs and scalarintvalues. -
Deconstruction: Supports C# deconstruction syntax for easy access to
XandYcomponents. -
Vector Conversion: Includes an optimized
ToVector2()method for interoperability withSystem.Numerics.Vector2. - Inlining: Mathematical operations and conversions are optimized for performance.
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.
Used to define positions or sizes in a UI system where elements must align perfectly with screen pixels.
Useful for "chunking" systems in game engines, where the world is divided into discrete sectors (e.g., Sector 1, 1, Sector 1, 2).
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}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}");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);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);| 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}. |