Vector2I
A 2-component int vector for coordinates and indices.
A C# struct with 2 int components: X and Y. Suitable for integer coordinates and indices; components have Int32 range. Assignment copies the value. This is not a long-component vector.
Keep intermediate sums, differences and products within range. Conversion to a float vector cannot preserve every large integer exactly; changing type does not repair an overflow that already happened.
Vector2I has no C# conversion to bool. Compare with Vector2I.Zero for an exact zero-vector test; a nonzero vector alone does not establish a valid coordinate or index.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
Vector2IConstructor
public Vector2I(int x, int y);Stores x and y without rounding, normalization or clamping. Each component is int; default(Vector2I) is (0, 0). This is a struct: assignment copies the value.
| Name | Type | Default |
|---|---|---|
x | int | — |
y | int | — |
xInitial signed int component X.
yInitial signed int component Y.
Properties9
DownProperty
public static Vector2I Down { get; }(0, 1). Down in 2D: positive Y. Returns a value copy: changing a local copy does not change the constant.
ItemProperty
public int this[int index] { get; set; }Index 0 reads or writes X; index 1 reads or writes Y. Any other index throws ArgumentOutOfRangeException in both getter and setter. Mutates this struct variable; a previously assigned copy remains unchanged.
LeftProperty
public static Vector2I Left { get; }(-1, 0). Negative X. Returns a value copy: changing a local copy does not change the constant.
MaxValueProperty
public static Vector2I MaxValue { get; }(int.MaxValue, int.MaxValue). Finite maximum ints, not infinity. Arithmetic using them can overflow. Returns a value copy: changing a local copy does not change the constant.
MinValueProperty
public static Vector2I MinValue { get; }(int.MinValue, int.MinValue). Finite minimum ints, not negative infinity. Abs() on this value throws OverflowException. Returns a value copy: changing a local copy does not change the constant.
OneProperty
public static Vector2I One { get; }(1, 1). Both components are one; this is not unit length. Returns a value copy: changing a local copy does not change the constant.
RightProperty
public static Vector2I Right { get; }(1, 0). Positive X. Returns a value copy: changing a local copy does not change the constant.
UpProperty
public static Vector2I Up { get; }(0, -1). Up in 2D: negative Y. Returns a value copy: changing a local copy does not change the constant.
ZeroProperty
public static Vector2I Zero { get; }(0, 0). Zero vector, equal to default(Vector2I). Returns a value copy: changing a local copy does not change the constant.
Methods42
AbsVector2I
public Vector2I Abs();Returns a new struct with Math.Abs applied to each component, leaving the source unchanged. int.MinValue has no positive int counterpart and throws OverflowException.
Vector2I(abs(X), abs(Y)) when both absolute values fit int.
Aspectfloat
public float Aspect();Divides X by Y as float, not as integer division. (3, 2) gives 1.5. Y == 0 produces signed Infinity, or NaN for (0, 0), rather than DivideByZeroException. Large int values lose precision when converted to float.
floatX / (float)Y; the result can be infinite or NaN.
Clamp2 overloads
public Vector2I Clamp(Vector2I min, Vector2I max);Returns a copy with components clamped to inclusive bounds. Each axis has its own min and max. Throws ArgumentException if any lower bound exceeds its upper bound; bounds are not swapped.
| Name | Type | Default |
|---|---|---|
min | Vector2I | — |
max | Vector2I | — |
minInclusive lower bound, no greater than its corresponding upper bound.
maxInclusive upper bound, no less than its corresponding lower bound.
Vector2IVector with each component inside its specified range.
public Vector2I Clamp(int min, int max);Returns a copy with components clamped to inclusive bounds. The same min and max apply to both axes. Throws ArgumentException if any lower bound exceeds its upper bound; bounds are not swapped.
| Name | Type | Default |
|---|---|---|
min | int | — |
max | int | — |
minInclusive lower bound, no greater than its corresponding upper bound.
maxInclusive upper bound, no less than its corresponding lower bound.
Vector2IVector with each component inside its specified range.
Deconstructvoid
public void Deconstruct(out int x, out int y);Copies X and Y into the output arguments, enabling var (x, y) = vector. Leaves the vector unchanged.
| Name | Type | Default |
|---|---|---|
x | int | — |
y | int | — |
xOutput copy of component X.
yOutput copy of component Y.
DistanceSquaredToint
public int DistanceSquaredTo(Vector2I to);Computes (to - this).LengthSquared() using int. Overflow can occur in subtraction and then in squaring or addition; the result can be negative or misleadingly small. Do not use for large coordinates without range checks. For a wide range, convert components to double before subtraction; see the DistanceTo example.
| Name | Type | Default |
|---|---|---|
to | Vector2I | — |
toOther point coordinates; the difference from this point is first computed as int.
intSquared distance only when every intermediate integer operation fits int.
DistanceTofloat
public float DistanceTo(Vector2I to);Computes (to - this).Length(). A float return type does not prevent int overflow during subtraction or squaring. Between (int.MinValue, 0) and (int.MaxValue, 0), the current SDK returns 1 instead of 4294967295.
WideDistance converts components to double before subtraction. CellAt uses Floor: position (-1, 33) with cell size 16 gives (-1, 2). A plain Vector2I cast truncates toward zero and is unsuitable for negative cell coordinates.
using System;
using Reckfell;
public static class IntegerGridExamples
{
public static Vector2I CellAt(Vector2 position, int cellSize)
{
if (cellSize <= 0) throw new ArgumentOutOfRangeException(nameof(cellSize));
Vector2 cell = (position / cellSize).Floor();
if (!cell.IsFinite() || (double)cell.X < int.MinValue || (double)cell.X > int.MaxValue
|| (double)cell.Y < int.MinValue || (double)cell.Y > int.MaxValue)
throw new ArgumentOutOfRangeException(nameof(position));
return (Vector2I)cell;
}
public static double WideDistance(Vector2I from, Vector2I to)
{
double dx = (double)to.X - from.X;
double dy = (double)to.Y - from.Y;
return Math.Sqrt(dx * dx + dy * dy);
}
}| Name | Type | Default |
|---|---|---|
to | Vector2I | — |
toOther point coordinates; the difference from this point is first computed as int.
floatEuclidean distance as float when intermediates do not overflow; otherwise it may be incorrect or NaN.
Equals2 overloads
public bool Equals(Vector2I other);Compares X and Y exactly. Does not compare approximately or compare lengths.
| Name | Type | Default |
|---|---|---|
other | Vector2I | — |
otherOther Vector2I for exact comparison of X and Y.
booltrue only when both components match and the type is compatible.
public override bool Equals(object? obj);Compares X and Y exactly. The object must contain a Vector2I: Vector2 with matching coordinates, null and other types return false.
| Name | Type | Default |
|---|---|---|
obj | object | — |
objObject to test; only a boxed Vector2I with matching X and Y can match.
booltrue only when both components match and the type is compatible.
GetHashCodeint
public override int GetHashCode();Uses HashCode.Combine(X, Y). Equal vectors have equal hashes, but a matching hash does not prove equality. Suitable for Dictionary<Vector2I, T> keys; do not persist the hash as an identifier across runs.
intHash of both integer components.
Lengthfloat
public float Length();Computes XX and YY and their sum as int before taking a float square root. (3, -4) yields 5. In the current SDK, (50000, 0) yields NaN and (65536, 0) yields 0 due to overflow. For a wide range, use Math.Sqrt((double)X * X + (double)Y * Y).
floatLength when intermediate values fit; overflow makes the result unreliable.
LengthSquaredint
public int LengthSquared();Computes XX + YY as int without a square root. Both squaring and addition can overflow: (50000, 0) yields a negative result and (65536, 0) yields 0. This is not a safe Length alternative for arbitrary ints. For a wide range, promote operands to double before multiplication.
intSquared length if the products and sum fit int.
Max2 overloads
public Vector2I Max(Vector2I with);Larger values are selected separately for X and Y, returning a copy. The argument supplies per-axis values. This does not select an entire vector using < or > ordering.
| Name | Type | Default |
|---|---|---|
with | Vector2I | — |
withComponents, or one shared value, for per-component comparison.
Vector2IComponent-wise maxima.
public Vector2I Max(int with);Larger values are selected separately for X and Y, returning a copy. One value is compared with both components. This does not select an entire vector using < or > ordering.
| Name | Type | Default |
|---|---|---|
with | int | — |
withComponents, or one shared value, for per-component comparison.
Vector2IComponent-wise maxima.
MaxAxisIndexVector2I.Axis
public Vector2I.Axis MaxAxisIndex();Selects the axis of the larger signed component, not the larger magnitude. Returns Axis.X when X == Y. For example, (-9, 2) yields Axis.Y.
Vector2I.AxisAxis.X when X >= Y; otherwise Axis.Y.
Min2 overloads
public Vector2I Min(Vector2I with);Smaller values are selected separately for X and Y, returning a copy. The argument supplies per-axis values. This does not select an entire vector using < or > ordering.
| Name | Type | Default |
|---|---|---|
with | Vector2I | — |
withComponents, or one shared value, for per-component comparison.
Vector2IComponent-wise minima.
public Vector2I Min(int with);Smaller values are selected separately for X and Y, returning a copy. One value is compared with both components. This does not select an entire vector using < or > ordering.
| Name | Type | Default |
|---|---|---|
with | int | — |
withComponents, or one shared value, for per-component comparison.
Vector2IComponent-wise minima.
MinAxisIndexVector2I.Axis
public Vector2I.Axis MinAxisIndex();Selects the axis of the smaller signed component, not the smaller magnitude. Returns Axis.Y when X == Y. For example, (-9, 2) yields Axis.X.
Vector2I.AxisAxis.X when X < Y; otherwise Axis.Y.
SignVector2I
public Vector2I Sign();Returns a new struct with each negative component replaced by -1, zero by 0 and each positive component by 1. int.MinValue yields -1 without taking its absolute value. This does not normalize length: (1, 1) stays (1, 1).
Vector2IComponent signs, each -1, 0 or 1.
Snapped2 overloads
public Vector2I Snapped(Vector2I step);Snaps each component using the double formula floor(value / step + 0.5) * step, then casts to int. A positive step rounds half ties toward the larger value: (6, -6).Snapped(4) gives (8, -4). A zero step preserves the component; a negative step changes half ties, so use a positive step. The result must fit int; range clamping is not performed.
| Name | Type | Default |
|---|---|---|
step | Vector2I | — |
stepPer-axis or shared step. A positive step is recommended; 0 preserves the corresponding component.
Vector2INew struct with snapped components; the source is unchanged.
public Vector2I Snapped(int step);Snaps each component using the double formula floor(value / step + 0.5) * step, then casts to int. A positive step rounds half ties toward the larger value: (6, -6).Snapped(4) gives (8, -4). A zero step preserves the component; a negative step changes half ties, so use a positive step. The result must fit int; range clamping is not performed.
| Name | Type | Default |
|---|---|---|
step | int | — |
stepPer-axis or shared step. A positive step is recommended; 0 preserves the corresponding component.
Vector2INew struct with snapped components; the source is unchanged.
ToString2 overloads
public override string ToString();Returns "(X, Y)" using CultureInfo.InvariantCulture regardless of the current culture. Equivalent to ToString(null).
stringBoth components as a string in the selected numeric format.
public string ToString(string? format);Returns "(X, Y)" using CultureInfo.InvariantCulture regardless of the current culture. Applies format to each int separately: (3, -4) with "D4" yields "(0003, -0004)". Invalid formatting throws FormatException.
| Name | Type | Default |
|---|---|---|
format | string | — |
formatint numeric format applied to both components, such as "D4"; null uses default formatting. An invalid format throws FormatException.
stringBoth components as a string in the selected numeric format.
op_AdditionVector2I
public static Vector2I operator +(Vector2I left, Vector2I right);Returns (left.X + right.X, left.Y + right.Y) as a new struct. In the installed SDK, int overflow wraps without saturation; checked around the call does not change the compiled operator. Negating int.MinValue leaves int.MinValue.
| Name | Type | Default |
|---|---|---|
left | Vector2I | — |
right | Vector2I | — |
leftLeft vector; the operation does not mutate the caller’s variable.
rightRight vector; the operation does not mutate the caller’s variable.
Vector2IComponent-wise result; caller variables are unchanged.
op_Division2 overloads
public static Vector2I operator /(Vector2I vec, Vector2I divisorv);Divides each integer component, truncating toward zero: (-7, 7) / 2 gives (-3, 3). A zero divisor throws DivideByZeroException. A component containing int.MinValue with divisor -1 throws OverflowException in the installed SDK. Divisors are supplied per axis.
| Name | Type | Default |
|---|---|---|
vec | Vector2I | — |
divisorv | Vector2I | — |
vecVector operand; a new struct value is returned.
divisorvNonzero divisors for the corresponding X and Y components.
Vector2IInteger quotients for X and Y.
public static Vector2I operator /(Vector2I vec, int divisor);Divides each integer component, truncating toward zero: (-7, 7) / 2 gives (-3, 3). A zero divisor throws DivideByZeroException. A component containing int.MinValue with divisor -1 throws OverflowException in the installed SDK. One divisor applies to both axes.
| Name | Type | Default |
|---|---|---|
vec | Vector2I | — |
divisor | int | — |
vecVector operand; a new struct value is returned.
divisorShared nonzero divisor for X and Y.
Vector2IInteger quotients for X and Y.
op_Equalitybool
public static bool operator ==(Vector2I left, Vector2I right);Compares both integer components exactly. Does not compare lengths or use approximate tolerance.
| Name | Type | Default |
|---|---|---|
left | Vector2I | — |
right | Vector2I | — |
leftLeft vector; the operation does not mutate the caller’s variable.
rightRight vector; the operation does not mutate the caller’s variable.
booltrue if both X and Y match.
op_ExplicitVector2I
public static explicit operator Vector2I(Vector2 value);Casts Vector2 components to int by truncating fractions toward zero: (-1.9, 1.9) gives (-1, 1), not (-2, 1). For cells with negative coordinates, use Floor first. Check finiteness and int range before casting; do not rely on portable results for NaN, Infinity or out-of-range values.
| Name | Type | Default |
|---|---|---|
value | Vector2 | — |
valueSource vector; precision and range restrictions are described by the conversion.
Vector2IInteger components truncated toward zero for inputs within the valid range.
op_GreaterThanbool
public static bool operator >(Vector2I left, Vector2I right);Lexicographic comparison: X first, then Y when X matches. This is neither a component-wise bounds test nor a length comparison. For example, (1, 100) < (2, -100) is true. For rectangular bounds, compare axes explicitly or use Rect2I.HasPoint.
| Name | Type | Default |
|---|---|---|
left | Vector2I | — |
right | Vector2I | — |
leftLeft vector; the operation does not mutate the caller’s variable.
rightRight vector; the operation does not mutate the caller’s variable.
booltrue when left > right holds in the stated order.
op_GreaterThanOrEqualbool
public static bool operator >=(Vector2I left, Vector2I right);Lexicographic comparison: X first, then Y when X matches. This is neither a component-wise bounds test nor a length comparison. For example, (1, 100) < (2, -100) is true. For rectangular bounds, compare axes explicitly or use Rect2I.HasPoint.
| Name | Type | Default |
|---|---|---|
left | Vector2I | — |
right | Vector2I | — |
leftLeft vector; the operation does not mutate the caller’s variable.
rightRight vector; the operation does not mutate the caller’s variable.
booltrue when left >= right holds in the stated order.
op_ImplicitVector2
public static implicit operator Vector2(Vector2I value);Converts each int component to the installed SDK’s float Vector2 component. Large integers can lose precision: 16777217 becomes 16777216. Keep Vector2I for exact grid arithmetic until the presentation step.
| Name | Type | Default |
|---|---|---|
value | Vector2I | — |
valueSource vector; precision and range restrictions are described by the conversion.
Vector2Vector2 with converted components; large int precision is not guaranteed.
op_Inequalitybool
public static bool operator !=(Vector2I left, Vector2I right);Negates ==. Does not compare lengths or use approximate tolerance.
| Name | Type | Default |
|---|---|---|
left | Vector2I | — |
right | Vector2I | — |
leftLeft vector; the operation does not mutate the caller’s variable.
rightRight vector; the operation does not mutate the caller’s variable.
booltrue if at least one component differs.
op_LessThanbool
public static bool operator <(Vector2I left, Vector2I right);Lexicographic comparison: X first, then Y when X matches. This is neither a component-wise bounds test nor a length comparison. For example, (1, 100) < (2, -100) is true. For rectangular bounds, compare axes explicitly or use Rect2I.HasPoint.
| Name | Type | Default |
|---|---|---|
left | Vector2I | — |
right | Vector2I | — |
leftLeft vector; the operation does not mutate the caller’s variable.
rightRight vector; the operation does not mutate the caller’s variable.
booltrue when left < right holds in the stated order.
op_LessThanOrEqualbool
public static bool operator <=(Vector2I left, Vector2I right);Lexicographic comparison: X first, then Y when X matches. This is neither a component-wise bounds test nor a length comparison. For example, (1, 100) < (2, -100) is true. For rectangular bounds, compare axes explicitly or use Rect2I.HasPoint.
| Name | Type | Default |
|---|---|---|
left | Vector2I | — |
right | Vector2I | — |
leftLeft vector; the operation does not mutate the caller’s variable.
rightRight vector; the operation does not mutate the caller’s variable.
booltrue when left <= right holds in the stated order.
op_Modulus2 overloads
public static Vector2I operator %(Vector2I vec, Vector2I divisorv);Computes C# remainders from truncated division: (10, -20) % 7 gives (3, -6). This is not positive cyclic wrapping; use Mathf.PosMod with a positive modulus for that. A zero divisor throws DivideByZeroException. A component containing int.MinValue with divisor -1 throws OverflowException in the installed SDK. Divisors are supplied per axis.
| Name | Type | Default |
|---|---|---|
vec | Vector2I | — |
divisorv | Vector2I | — |
vecVector operand; a new struct value is returned.
divisorvNonzero divisors for the corresponding X and Y components.
Vector2IRemainders for X and Y, with the dividend’s sign or zero.
public static Vector2I operator %(Vector2I vec, int divisor);Computes C# remainders from truncated division: (10, -20) % 7 gives (3, -6). This is not positive cyclic wrapping; use Mathf.PosMod with a positive modulus for that. A zero divisor throws DivideByZeroException. A component containing int.MinValue with divisor -1 throws OverflowException in the installed SDK. One divisor applies to both axes.
| Name | Type | Default |
|---|---|---|
vec | Vector2I | — |
divisor | int | — |
vecVector operand; a new struct value is returned.
divisorShared nonzero divisor for X and Y.
Vector2IRemainders for X and Y, with the dividend’s sign or zero.
op_Multiply3 overloads
public static Vector2I operator *(Vector2I left, Vector2I right);Returns (left.X * right.X, left.Y * right.Y) as a new struct. An int scales both axes; two vectors multiply component-wise, not as a dot product. In the installed SDK, int overflow wraps without saturation; checked around the call does not change the compiled operator. Negating int.MinValue leaves int.MinValue.
| Name | Type | Default |
|---|---|---|
left | Vector2I | — |
right | Vector2I | — |
leftLeft vector; the operation does not mutate the caller’s variable.
rightRight vector; the operation does not mutate the caller’s variable.
Vector2IComponent-wise result; caller variables are unchanged.
public static Vector2I operator *(Vector2I vec, int scale);Returns (vec.X * scale, vec.Y * scale) as a new struct. An int scales both axes; two vectors multiply component-wise, not as a dot product. In the installed SDK, int overflow wraps without saturation; checked around the call does not change the compiled operator. Negating int.MinValue leaves int.MinValue.
| Name | Type | Default |
|---|---|---|
vec | Vector2I | — |
scale | int | — |
vecVector operand; a new struct value is returned.
scaleInteger multiplier for both components; each product must fit int.
Vector2IComponent-wise result; caller variables are unchanged.
public static Vector2I operator *(int scale, Vector2I vec);Returns (vec.X * scale, vec.Y * scale) as a new struct. An int scales both axes; two vectors multiply component-wise, not as a dot product. In the installed SDK, int overflow wraps without saturation; checked around the call does not change the compiled operator. Negating int.MinValue leaves int.MinValue.
| Name | Type | Default |
|---|---|---|
scale | int | — |
vec | Vector2I | — |
scaleInteger multiplier for both components; each product must fit int.
vecVector operand; a new struct value is returned.
Vector2IComponent-wise result; caller variables are unchanged.
op_SubtractionVector2I
public static Vector2I operator -(Vector2I left, Vector2I right);Returns (left.X - right.X, left.Y - right.Y) as a new struct. In the installed SDK, int overflow wraps without saturation; checked around the call does not change the compiled operator. Negating int.MinValue leaves int.MinValue.
| Name | Type | Default |
|---|---|---|
left | Vector2I | — |
right | Vector2I | — |
leftLeft vector; the operation does not mutate the caller’s variable.
rightRight vector; the operation does not mutate the caller’s variable.
Vector2IComponent-wise result; caller variables are unchanged.
op_UnaryNegationVector2I
public static Vector2I operator -(Vector2I vec);Returns (-X, -Y) as a new struct. In the installed SDK, int overflow wraps without saturation; checked around the call does not change the compiled operator. Negating int.MinValue leaves int.MinValue.
| Name | Type | Default |
|---|---|---|
vec | Vector2I | — |
vecVector operand; a new struct value is returned.
Vector2IComponent-wise result; caller variables are unchanged.
op_UnaryPlusVector2I
public static Vector2I operator +(Vector2I vec);Returns (X, Y) as a new struct. Unary plus leaves the value unchanged.
| Name | Type | Default |
|---|---|---|
vec | Vector2I | — |
vecVector operand; a new struct value is returned.
Vector2IComponent-wise result; caller variables are unchanged.
Fields and constants2
XField
public int X;Mutable signed 32-bit X component, also accessible through [0]. Assignment does not clamp the coordinate or change Y.
YField
public int Y;Mutable signed 32-bit Y component, also accessible through [1]. In conventional 2D screen coordinates, positive Y points down.
Vector2I.Axis
| Value | Description |
|---|---|
X = 0 | Enumerated value for the X axis. Returned by |
Y = 1 | Enumerated value for the Y axis. Returned by |