Struct IntVector2
A 2-dimensional vector with integer coordinates.
public readonly struct IntVector2 : IEquatable<IntVector2>
- Implements
- Inherited Members
- Extension Methods
Remarks
Can be implicitly cast to/from (int x, int y)
. This allows writing IntVector2s in a more readable way.
IntVector2 point = (5, 3);
instead of
IntVector2 point = new IntVector2(5, 3);
Constructors
IntVector2(int, int)
Creates a new vector with the given x and y coordinates.
public IntVector2(int x, int y)
Parameters
Fields
down
The vector (0, -1).
public static readonly IntVector2 down
Field Value
- See Also
downLeft
The vector (-1, -1).
public static readonly IntVector2 downLeft
Field Value
- See Also
downRight
The vector (1, -1).
public static readonly IntVector2 downRight
Field Value
- See Also
left
The vector (-1, 0).
public static readonly IntVector2 left
Field Value
- See Also
one
The vector (1, 1).
public static readonly IntVector2 one
Field Value
Remarks
An alias for upRight.
right
The vector (1, 0).
public static readonly IntVector2 right
Field Value
- See Also
up
The vector (0, 1).
public static readonly IntVector2 up
Field Value
- See Also
upDownLeftRight
An IEnumerable<T> over the vectors (0, 1), (0, -1), (-1, 0), (1, 0)
.
public static readonly IEnumerable<IntVector2> upDownLeftRight
Field Value
- See Also
upLeft
The vector (-1, 1).
public static readonly IntVector2 upLeft
Field Value
- See Also
upRight
The vector (1, 1).
public static readonly IntVector2 upRight
Field Value
Remarks
An alias for one.
- See Also
x
The x coordinate.
public readonly int x
Field Value
- See Also
y
The y coordinate.
public readonly int y
Field Value
- See Also
zero
The vector (0, 0).
public static readonly IntVector2 zero
Field Value
Properties
l1Norm
public int l1Norm { get; }
Property Value
- See Also
magnitude
public float magnitude { get; }
Property Value
- See Also
sign
The sign of each component of the vector.
public IntVector2 sign { get; }
Property Value
Remarks
The sign of a component that is 0 is defined to be 0.
- See Also
sqrMagnitude
public int sqrMagnitude { get; }
Property Value
Remarks
This is faster than squaring magnitude.
- See Also
supNorm
The supremum norm of the vector, which is max(abs(x), abs(y))
.
Also known as the Chebyshev norm or l-infinity norm.
public int supNorm { get; }
Property Value
- See Also
Methods
Abs(IntVector2)
Takes the absolute value component-wise.
public static IntVector2 Abs(IntVector2 a)
Parameters
Returns
AreColinear(IntVector2, IntVector2)
Returns whether the two vectors lie on the same real line through (0, 0)
. This is equivalent to whether there is an IntVector2 dividing both of them.
public static bool AreColinear(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
Remarks
For a proof that the two statements given in the summary are logically equivalent, see the source code for this method.
- See Also
ArePerpendicular(IntVector2, IntVector2)
Determines whether the two vectors are perpendicular to each other.
public static bool ArePerpendicular(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
Remarks
Being perpendicular is also known as being orthogonal.
- See Also
CeilToIntVector2(Vector2)
Ceils the vector component-wise.
public static IntVector2 CeilToIntVector2(Vector2 vector2)
Parameters
vector2
Vector2
Returns
Remarks
Uses Mathf.CeilToInt(float).
- See Also
Deconstruct(out int, out int)
Deconstructs the vector into its x and y coordinates.
public void Deconstruct(out int x, out int y)
Parameters
Distance(IntVector2, IntVector2)
Computes the standard Euclidean distance between the two vectors - i.e. the Magnitude(IntVector2) of
.a
- b
public static float Distance(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
- See Also
Divides(IntVector2)
Returns whether dividend
is an integer multiple of this vector.
(1, 2)
divides (3, 6)
as (3, 6) = 3 * (1, 2)
.
public bool Divides(IntVector2 dividend)
Parameters
dividend
IntVector2
Returns
Remarks
More precisely, this determines whether there exists an integer n
such that
.
This means, for example, that dividend
= n * this(0, 3)
divides (0, 6)
, and that everything divides (0, 0)
.
- See Also
Dot(IntVector2, IntVector2)
Computes the dot product of the two vectors.
public static int Dot(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
Equals(IntVector2)
The same as operator ==(IntVector2, IntVector2)
public bool Equals(IntVector2 other)
Parameters
other
IntVector2
Returns
Equals(object)
The same as Equals(IntVector2)
public override bool Equals(object obj)
Parameters
obj
object
Returns
Flip(CardinalOrdinalAxis)
Returns the vector flipped across the given axis.
public IntVector2 Flip(CardinalOrdinalAxis axis)
Parameters
axis
CardinalOrdinalAxis
Returns
Exceptions
- ArgumentNullException
axis
is null.
FloorToIntVector2(Vector2)
Floors the vector component-wise.
public static IntVector2 FloorToIntVector2(Vector2 vector2)
Parameters
vector2
Vector2
Returns
Remarks
Uses Mathf.FloorToInt(float).
- See Also
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
IsMultipleOf(IntVector2)
Returns whether this vector is an integer multiple of divisor
.
(3, 6)
is a multiple of (1, 2)
as (3, 6) = 3 * (1, 2)
.
public bool IsMultipleOf(IntVector2 divisor)
Parameters
divisor
IntVector2
Returns
Remarks
More precisely, this determines whether there exists an integer n
such that this = n *
.
This means, for example, that divisor
(0, 6)
is a multiple of (0, 3)
, and that (0, 0)
is a multiple of everything.
- See Also
L1Distance(IntVector2, IntVector2)
Computes the l1 distance between the two vectors - i.e. the L1Norm(IntVector2) of
.
Also known as the taxicab/Manhattan distance or rectilinear distance.a
- b
public static int L1Distance(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
L1Norm(IntVector2)
public static int L1Norm(IntVector2 a)
Parameters
Returns
- See Also
Magnitude(IntVector2)
public static float Magnitude(IntVector2 a)
Parameters
Returns
- See Also
Max(IntVector2, IntVector2)
Takes the maximum of the two vectors component-wise.
public static IntVector2 Max(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
- See Also
Max(params IntVector2[])
Takes the maximum of the vectors component-wise.
public static IntVector2 Max(params IntVector2[] vectors)
Parameters
vectors
IntVector2[]
Returns
Exceptions
- ArgumentNullException
vectors
is null.- ArgumentException
vectors
is empty.
- See Also
Max(IEnumerable<IntVector2>)
Takes the maximum of the vectors component-wise.
public static IntVector2 Max(IEnumerable<IntVector2> vectors)
Parameters
vectors
IEnumerable<IntVector2>
Returns
Remarks
This is not provided as an IEnumerable<T> extension method since it seems C# will favour using the LINQ Max<TSource>(IEnumerable<TSource>) instead, which causes errors.
Exceptions
- ArgumentNullException
vectors
is null.- ArgumentException
vectors
is empty.
- See Also
Min(IntVector2, IntVector2)
Takes the minimum of the two vectors component-wise.
public static IntVector2 Min(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
- See Also
Min(params IntVector2[])
Takes the minmum of the vectors component-wise.
public static IntVector2 Min(params IntVector2[] vectors)
Parameters
vectors
IntVector2[]
Returns
Exceptions
- ArgumentNullException
vectors
is null.- ArgumentException
vectors
is empty.
- See Also
Min(IEnumerable<IntVector2>)
Takes the minmum of the vectors component-wise.
public static IntVector2 Min(IEnumerable<IntVector2> vectors)
Parameters
vectors
IEnumerable<IntVector2>
Returns
Remarks
This is not provided as an IEnumerable<T> extension method since it seems C# will favour using the LINQ Min<TSource>(IEnumerable<TSource>) instead, which causes errors.
Exceptions
- ArgumentNullException
vectors
is null.- ArgumentException
vectors
is empty.
- See Also
Rotate(QuadrantalAngle)
Returns the vector rotated by the given angle.
public IntVector2 Rotate(QuadrantalAngle angle)
Parameters
angle
QuadrantalAngle
Returns
RoundToIntVector2(Vector2)
Rounds the vector component-wise.
public static IntVector2 RoundToIntVector2(Vector2 vector2)
Parameters
vector2
Vector2
Returns
Remarks
Uses Mathf.RoundToInt(float).
- See Also
Sign(IntVector2)
Returns the sign of each component of the vector.
public static IntVector2 Sign(IntVector2 a)
Parameters
Returns
Remarks
The sign of a component that is 0 is defined to be 0.
- See Also
Simplify(IntVector2)
Scales the vector down so its components are coprime (have no common divisors). In other words, it computes the smallest IntVector2 dividing a
.
public static IntVector2 Simplify(IntVector2 a)
Parameters
Returns
Remarks
Preserves signs.
SqrDistance(IntVector2, IntVector2)
Computes the square of the Euclidean distance between the two vectors - i.e. the SqrMagnitude(IntVector2) of
.a
- b
public static int SqrDistance(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
Remarks
This is faster than squaring Distance(IntVector2, IntVector2).
SqrMagnitude(IntVector2)
public static int SqrMagnitude(IntVector2 a)
Parameters
Returns
Remarks
This is faster than squaring Magnitude(IntVector2).
- See Also
SupDistance(IntVector2, IntVector2)
Computes the supremum distance between the two vectors - i.e. the SupNorm(IntVector2) of
.
Also known as the Chebyshev distance or l-infinity distance.a
- b
public static int SupDistance(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
SupNorm(IntVector2)
The supremum norm of the vector, which is max(abs(x), abs(y))
.
Also known as the Chebyshev norm or l-infinity norm.
public static int SupNorm(IntVector2 a)
Parameters
Returns
- See Also
ToString()
Represents the vector as a string in the form "(x, y)"
.
public override string ToString()
Returns
Operators
operator +(IntVector2, IntVector2)
Adds the two vectors component-wise.
public static IntVector2 operator +(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
operator /(IntVector2, IntVector2)
Divides (integer division) the two vectors component-wise.
public static IntVector2 operator /(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
operator /(IntVector2, int)
Divides (integer division) each component of the vector by scalar
.
public static IntVector2 operator /(IntVector2 vector, int scalar)
Parameters
vector
IntVector2scalar
int
Returns
operator ==(IntVector2, IntVector2)
Whether the two vectors have identical x and y coords.
public static bool operator ==(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
- See Also
explicit operator IntVector2(Vector2)
Casts from Unity's Vector2, by casting each coordinate to int, which truncates them towards zero.
public static explicit operator IntVector2(Vector2 vector)
Parameters
vector
Vector2
Returns
explicit operator IntVector2(Vector3)
Casts from Unity's Vector3, by casting each coordinate to int, which truncates them towards zero. The z coordinate is ignored.
public static explicit operator IntVector2(Vector3 vector)
Parameters
vector
Vector3
Returns
operator >(IntVector2, IntVector2)
Returns true iff > holds for both components.
public static bool operator >(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
operator >=(IntVector2, IntVector2)
Returns true iff >= holds for both components.
public static bool operator >=(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
implicit operator (int x, int y)(IntVector2)
public static implicit operator (int x, int y)(IntVector2 vector)
Parameters
vector
IntVector2
Returns
Remarks
This conversion should be inlined by the JIT compiler.
implicit operator Vector2(IntVector2)
Casts to Unity's Vector2, by casting each coordinate to float.
public static implicit operator Vector2(IntVector2 vector)
Parameters
vector
IntVector2
Returns
- Vector2
implicit operator Vector2Int(IntVector2)
Casts to Unity's Vector2Int.
public static implicit operator Vector2Int(IntVector2 vector)
Parameters
vector
IntVector2
Returns
- Vector2Int
implicit operator Vector3(IntVector2)
Casts to Unity's Vector3, by casting each coordinate to float, with a 0 in the z-coord.
public static implicit operator Vector3(IntVector2 vector)
Parameters
vector
IntVector2
Returns
- Vector3
implicit operator IntVector2((int x, int y))
Allows writing IntVector2s in a more readable way
IntVector2 point = (5, 3);
instead of
IntVector2 point = new IntVector2(5, 3);
public static implicit operator IntVector2((int x, int y) tuple)
Parameters
Returns
Remarks
This conversion should be inlined by the JIT compiler.
implicit operator IntVector2(Vector2Int)
Casts from Unity's Vector2Int.
public static implicit operator IntVector2(Vector2Int vector)
Parameters
vector
Vector2Int
Returns
operator !=(IntVector2, IntVector2)
Whether the two vectors differ in their x and/or y coords.
public static bool operator !=(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
- See Also
operator <(IntVector2, IntVector2)
Returns true iff < holds for both components.
public static bool operator <(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
operator <=(IntVector2, IntVector2)
Returns true iff <= holds for both components.
public static bool operator <=(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
operator *(IntVector2, IntVector2)
Multiplies the two vectors component-wise.
public static IntVector2 operator *(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
operator *(IntVector2, int)
Multiplies each component of the vector by scalar
.
public static IntVector2 operator *(IntVector2 vector, int scalar)
Parameters
vector
IntVector2scalar
int
Returns
operator *(int, IntVector2)
Multiplies each component of the vector by scalar
.
public static IntVector2 operator *(int scalar, IntVector2 vector)
Parameters
scalar
intvector
IntVector2
Returns
operator -(IntVector2, IntVector2)
Subtracts the two vectors component-wise.
public static IntVector2 operator -(IntVector2 a, IntVector2 b)
Parameters
a
IntVector2b
IntVector2
Returns
operator -(IntVector2)
Negates each component of the vector.
public static IntVector2 operator -(IntVector2 a)