// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. using System; using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace FlaxEngine { /// /// Represents a two dimensional mathematical vector (signed integers). /// [Serializable] #if FLAX_EDITOR [System.ComponentModel.TypeConverter(typeof(TypeConverters.Int2Converter))] #endif partial struct Int2 : IEquatable, IFormattable { private static readonly string _formatString = "X:{0} Y:{1}"; /// /// The size of the type, in bytes. /// public static readonly int SizeInBytes = Marshal.SizeOf(typeof(Int2)); /// /// A with all of its components set to zero. /// public static readonly Int2 Zero; /// /// The X unit (1, 0). /// public static readonly Int2 UnitX = new Int2(1, 0); /// /// The Y unit (0, 1). /// public static readonly Int2 UnitY = new Int2(0, 1); /// /// A with all of its components set to one. /// public static readonly Int2 One = new Int2(1, 1); /// /// A with all components equal to . /// public static readonly Int2 Minimum = new Int2(int.MinValue); /// /// A with all components equal to . /// public static readonly Int2 Maximum = new Int2(int.MaxValue); /// /// Initializes a new instance of the struct. /// /// The value that will be assigned to all components. public Int2(int value) { X = value; Y = value; } /// /// Initializes a new instance of the struct. /// /// Initial value for the X component of the vector. /// Initial value for the Y component of the vector. public Int2(int x, int y) { X = x; Y = y; } /// /// Initializes a new instance of the struct. /// /// A vector containing the values with which to initialize the X and Y components. public Int2(Int3 value) { X = value.X; Y = value.Y; } /// /// Initializes a new instance of the struct. /// /// A vector containing the values with which to initialize the X and Y components. public Int2(Int4 value) { X = value.X; Y = value.Y; } /// /// Initializes a new instance of the struct. /// /// The values to assign to the X and Y components of the vector. This must be an array with two elements. /// Thrown when is null. /// Thrown when contains more or less than two elements. /// public Int2(int[] values) { if (values == null) throw new ArgumentNullException(nameof(values)); if (values.Length != 2) throw new ArgumentOutOfRangeException(nameof(values), "There must be two and only two input values for Int2."); X = values[0]; Y = values[1]; } /// /// Gets a value indicting whether this vector is zero /// public bool IsZero => X == 0 && Y == 0; /// /// Gets a minimum component value /// public int MinValue => Mathf.Min(X, Y); /// /// Gets a maximum component value /// public int MaxValue => Mathf.Max(X, Y); /// /// Gets an arithmetic average value of all vector components. /// public float AvgValue => (X + Y) * (1.0f / 2.0f); /// /// Gets a sum of the component values. /// public int ValuesSum => X + Y; /// /// Gets or sets the component at the specified index. /// /// The value of the X or Y component, depending on the index. /// The index of the component to access. Use 0 for the X component and 1 for the Y component. /// The value of the component at the specified index. /// Thrown when the is out of the range [0, 1]. public int this[int index] { get { switch (index) { case 0: return X; case 1: return Y; } throw new ArgumentOutOfRangeException(nameof(index), "Indices for Int2 run from 0 to 1, inclusive."); } set { switch (index) { case 0: X = value; break; case 1: Y = value; break; default: throw new ArgumentOutOfRangeException(nameof(index), "Indices for Int2 run from 0 to 1, inclusive."); } } } /// /// Calculates the length of the vector. /// /// The length of the vector. /// may be preferred when only the relative length is needed and speed is of the essence. public float Length => (float)Math.Sqrt(X * X + Y * Y); /// /// Calculates the squared length of the vector. /// /// The squared length of the vector. /// This method may be preferred to when only a relative length is needed and speed is of the essence. public int LengthSquared => X * X + Y * Y; /// /// Creates an array containing the elements of the vector. /// /// A two-element array containing the components of the vector. public int[] ToArray() { return new[] { X, Y }; } /// /// Adds two vectors. /// /// The first vector to add. /// The second vector to add. /// When the method completes, contains the sum of the two vectors. public static void Add(ref Int2 left, ref Int2 right, out Int2 result) { result = new Int2(left.X + right.X, left.Y + right.Y); } /// /// Adds two vectors. /// /// The first vector to add. /// The second vector to add. /// The sum of the two vectors. public static Int2 Add(Int2 left, Int2 right) { return new Int2(left.X + right.X, left.Y + right.Y); } /// /// Perform a component-wise addition /// /// The input vector /// The scalar value to be added to elements /// The vector with added scalar for each element. public static void Add(ref Int2 left, ref int right, out Int2 result) { result = new Int2(left.X + right, left.Y + right); } /// /// Perform a component-wise addition /// /// The input vector /// The scalar value to be added to elements /// The vector with added scalar for each element. public static Int2 Add(Int2 left, int right) { return new Int2(left.X + right, left.Y + right); } /// /// Subtracts two vectors. /// /// The first vector to subtract. /// The second vector to subtract. /// When the method completes, contains the difference of the two vectors. public static void Subtract(ref Int2 left, ref Int2 right, out Int2 result) { result = new Int2(left.X - right.X, left.Y - right.Y); } /// /// Subtracts two vectors. /// /// The first vector to subtract. /// The second vector to subtract. /// The difference of the two vectors. public static Int2 Subtract(Int2 left, Int2 right) { return new Int2(left.X - right.X, left.Y - right.Y); } /// /// Perform a component-wise subtraction /// /// The input vector /// The scalar value to be subtracted from elements /// The vector with subtracted scalar for each element. public static void Subtract(ref Int2 left, ref int right, out Int2 result) { result = new Int2(left.X - right, left.Y - right); } /// /// Perform a component-wise subtraction /// /// The input vector /// The scalar value to be subtracted from elements /// The vector with subtracted scalar for each element. public static Int2 Subtract(Int2 left, int right) { return new Int2(left.X - right, left.Y - right); } /// /// Perform a component-wise subtraction /// /// The scalar value to be subtracted from elements /// The input vector /// The vector with subtracted scalar for each element. public static void Subtract(ref int left, ref Int2 right, out Int2 result) { result = new Int2(left - right.X, left - right.Y); } /// /// Perform a component-wise subtraction /// /// The scalar value to be subtracted from elements /// The input vector /// The vector with subtracted scalar for each element. public static Int2 Subtract(int left, Int2 right) { return new Int2(left - right.X, left - right.Y); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// When the method completes, contains the scaled vector. public static void Multiply(ref Int2 value, int scale, out Int2 result) { result = new Int2(value.X * scale, value.Y * scale); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// The scaled vector. public static Int2 Multiply(Int2 value, int scale) { return new Int2(value.X * scale, value.Y * scale); } /// /// Multiplies a vector with another by performing component-wise multiplication. /// /// The first vector to multiply. /// The second vector to multiply. /// When the method completes, contains the multiplied vector. public static void Multiply(ref Int2 left, ref Int2 right, out Int2 result) { result = new Int2(left.X * right.X, left.Y * right.Y); } /// /// Multiplies a vector with another by performing component-wise multiplication. /// /// The first vector to multiply. /// The second vector to multiply. /// The multiplied vector. public static Int2 Multiply(Int2 left, Int2 right) { return new Int2(left.X * right.X, left.Y * right.Y); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// When the method completes, contains the scaled vector. public static void Divide(ref Int2 value, int scale, out Int2 result) { result = new Int2(value.X / scale, value.Y / scale); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// The scaled vector. public static Int2 Divide(Int2 value, int scale) { return new Int2(value.X / scale, value.Y / scale); } /// /// Scales a vector by the given value. /// /// The amount by which to scale the vector. /// The vector to scale. /// When the method completes, contains the scaled vector. public static void Divide(int scale, ref Int2 value, out Int2 result) { result = new Int2(scale / value.X, scale / value.Y); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// The scaled vector. public static Int2 Divide(int scale, Int2 value) { return new Int2(scale / value.X, scale / value.Y); } /// /// Reverses the direction of a given vector. /// /// The vector to negate. /// When the method completes, contains a vector facing in the opposite direction. public static void Negate(ref Int2 value, out Int2 result) { result = new Int2(-value.X, -value.Y); } /// /// Reverses the direction of a given vector. /// /// The vector to negate. /// A vector facing in the opposite direction. public static Int2 Negate(Int2 value) { return new Int2(-value.X, -value.Y); } /// /// Restricts a value to be within a specified range. /// /// The value to clamp. /// The minimum value. /// The maximum value. /// When the method completes, contains the clamped value. public static void Clamp(ref Int2 value, ref Int2 min, ref Int2 max, out Int2 result) { int x = value.X; x = x > max.X ? max.X : x; x = x < min.X ? min.X : x; int y = value.Y; y = y > max.Y ? max.Y : y; y = y < min.Y ? min.Y : y; result = new Int2(x, y); } /// /// Restricts a value to be within a specified range. /// /// The value to clamp. /// The minimum value. /// The maximum value. /// The clamped value. public static Int2 Clamp(Int2 value, Int2 min, Int2 max) { Clamp(ref value, ref min, ref max, out Int2 result); return result; } /// /// Calculates the distance between two vectors. /// /// The first vector. /// The second vector. /// When the method completes, contains the distance between the two vectors. /// may be preferred when only the relative distance is needed and speed is of the essence. public static void Distance(ref Int2 value1, ref Int2 value2, out float result) { int x = value1.X - value2.X; int y = value1.Y - value2.Y; result = (float)Math.Sqrt(x * x + y * y); } /// /// Calculates the distance between two vectors. /// /// The first vector. /// The second vector. /// The distance between the two vectors. /// may be preferred when only the relative distance is needed and speed is of the essence. public static float Distance(Int2 value1, Int2 value2) { int x = value1.X - value2.X; int y = value1.Y - value2.Y; return (float)Math.Sqrt(x * x + y * y); } /// /// Calculates the squared distance between two vectors. /// /// The first vector. /// The second vector /// When the method completes, contains the squared distance between the two vectors. /// /// Distance squared is the value before taking the square root. /// Distance squared can often be used in place of distance if relative comparisons are being made. /// For example, consider three points A, B, and C. To determine whether B or C is further from A, /// compare the distance between A and B to the distance between A and C. Calculating the two distances /// involves two square roots, which are computationally expensive. However, using distance squared /// provides the same information and avoids calculating two square roots. /// public static void DistanceSquared(ref Int2 value1, ref Int2 value2, out int result) { int x = value1.X - value2.X; int y = value1.Y - value2.Y; result = x * x + y * y; } /// /// Calculates the squared distance between two vectors. /// /// The first vector. /// The second vector. /// The squared distance between the two vectors. /// /// Distance squared is the value before taking the square root. /// Distance squared can often be used in place of distance if relative comparisons are being made. /// For example, consider three points A, B, and C. To determine whether B or C is further from A, /// compare the distance between A and B to the distance between A and C. Calculating the two distances /// involves two square roots, which are computationally expensive. However, using distance squared /// provides the same information and avoids calculating two square roots. /// public static int DistanceSquared(Int2 value1, Int2 value2) { int x = value1.X - value2.X; int y = value1.Y - value2.Y; return x * x + y * y; } /// /// Calculates the dot product of two vectors. /// /// First source vector. /// Second source vector. /// When the method completes, contains the dot product of the two vectors. public static void Dot(ref Int2 left, ref Int2 right, out int result) { result = left.X * right.X + left.Y * right.Y; } /// /// Calculates the dot product of two vectors. /// /// First source vector. /// Second source vector. /// The dot product of the two vectors. public static int Dot(Int2 left, Int2 right) { return left.X * right.X + left.Y * right.Y; } /// /// Returns a vector containing the largest components of the specified vectors. /// /// The first source vector. /// The second source vector. /// When the method completes, contains an new vector composed of the largest components of the source vectors. public static void Max(ref Int2 left, ref Int2 right, out Int2 result) { result.X = left.X > right.X ? left.X : right.X; result.Y = left.Y > right.Y ? left.Y : right.Y; } /// /// Returns a vector containing the largest components of the specified vectors. /// /// The first source vector. /// The second source vector. /// A vector containing the largest components of the source vectors. public static Int2 Max(Int2 left, Int2 right) { Max(ref left, ref right, out Int2 result); return result; } /// /// Returns a vector containing the smallest components of the specified vectors. /// /// The first source vector. /// The second source vector. /// When the method completes, contains an new vector composed of the smallest components of the source vectors. public static void Min(ref Int2 left, ref Int2 right, out Int2 result) { result.X = left.X < right.X ? left.X : right.X; result.Y = left.Y < right.Y ? left.Y : right.Y; } /// /// Returns a vector containing the smallest components of the specified vectors. /// /// The first source vector. /// The second source vector. /// A vector containing the smallest components of the source vectors. public static Int2 Min(Int2 left, Int2 right) { Min(ref left, ref right, out Int2 result); return result; } /// /// Returns the absolute value of a vector. /// /// The value. /// A vector which components are less or equal to 0. public static Int2 Abs(Int2 v) { return new Int2(Math.Abs(v.X), Math.Abs(v.Y)); } /// /// Adds two vectors. /// /// The first vector to add. /// The second vector to add. /// The sum of the two vectors. public static Int2 operator +(Int2 left, Int2 right) { return new Int2(left.X + right.X, left.Y + right.Y); } /// /// Multiplies a vector with another by performing component-wise multiplication equivalent to /// . /// /// The first vector to multiply. /// The second vector to multiply. /// The multiplication of the two vectors. public static Int2 operator *(Int2 left, Int2 right) { return new Int2(left.X * right.X, left.Y * right.Y); } /// /// Assert a vector (return it unchanged). /// /// The vector to assert (unchanged). /// The asserted (unchanged) vector. public static Int2 operator +(Int2 value) { return value; } /// /// Subtracts two vectors. /// /// The first vector to subtract. /// The second vector to subtract. /// The difference of the two vectors. public static Int2 operator -(Int2 left, Int2 right) { return new Int2(left.X - right.X, left.Y - right.Y); } /// /// Reverses the direction of a given vector. /// /// The vector to negate. /// A vector facing in the opposite direction. public static Int2 operator -(Int2 value) { return new Int2(-value.X, -value.Y); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// The scaled vector. public static Int2 operator *(int scale, Int2 value) { return new Int2(value.X * scale, value.Y * scale); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// The scaled vector. public static Int2 operator *(Int2 value, int scale) { return new Int2(value.X * scale, value.Y * scale); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// The scaled vector. public static Int2 operator /(Int2 value, int scale) { return new Int2(value.X / scale, value.Y / scale); } /// /// Scales a vector by the given value. /// /// The amount by which to scale the vector. /// The vector to scale. /// The scaled vector. public static Int2 operator /(int scale, Int2 value) { return new Int2(scale / value.X, scale / value.Y); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// The scaled vector. public static Int2 operator /(Int2 value, Int2 scale) { return new Int2(value.X / scale.X, value.Y / scale.Y); } /// /// Remainder of value divided by scale. /// /// The vector to scale. /// The amount by which to scale the vector. /// The remained vector. public static Int2 operator %(Int2 value, float scale) { return new Int2((int)(value.X % scale), (int)(value.Y % scale)); } /// /// Remainder of value divided by scale. /// /// The amount by which to scale the vector. /// The vector to scale. /// The remained vector. public static Int2 operator %(float value, Int2 scale) { return new Int2((int)(value % scale.X), (int)(value % scale.Y)); } /// /// Remainder of value divided by scale. /// /// The vector to scale. /// The amount by which to scale the vector. /// The remained vector. public static Int2 operator %(Int2 value, Int2 scale) { return new Int2(value.X % scale.X, value.Y % scale.Y); } /// /// Perform a component-wise addition /// /// The input vector. /// The scalar value to be added on elements /// The vector with added scalar for each element. public static Int2 operator +(Int2 value, int scalar) { return new Int2(value.X + scalar, value.Y + scalar); } /// /// Perform a component-wise addition /// /// The input vector. /// The scalar value to be added on elements /// The vector with added scalar for each element. public static Int2 operator +(int scalar, Int2 value) { return new Int2(scalar + value.X, scalar + value.Y); } /// /// Perform a component-wise subtraction /// /// The input vector. /// The scalar value to be subtracted from elements /// The vector with subtracted scalar from each element. public static Int2 operator -(Int2 value, int scalar) { return new Int2(value.X - scalar, value.Y - scalar); } /// /// Perform a component-wise subtraction /// /// The input vector. /// The scalar value to be subtracted from elements /// The vector with subtracted scalar from each element. public static Int2 operator -(int scalar, Int2 value) { return new Int2(scalar - value.X, scalar - value.Y); } /// /// Tests for equality between two objects. /// /// The first value to compare. /// The second value to compare. /// true if has the same value as ; otherwise, false. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(Int2 left, Int2 right) { return left.Equals(ref right); } /// /// Tests for inequality between two objects. /// /// The first value to compare. /// The second value to compare. /// true if has a different value than ; otherwise, false. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(Int2 left, Int2 right) { return !left.Equals(ref right); } /// /// Performs an explicit conversion from to . /// /// The value. /// The result of the conversion. public static explicit operator Int3(Int2 value) { return new Int3(value, 0); } /// /// Performs an explicit conversion from to . /// /// The value. /// The result of the conversion. public static explicit operator Int4(Int2 value) { return new Int4(value, 0, 0); } /// /// Performs an explicit conversion from to . /// /// The value. /// The result of the conversion. public static explicit operator Float2(Int2 value) { return new Float2(value.X, value.Y); } /// /// Performs an explicit conversion from to . /// /// The value. /// The result of the conversion. public static explicit operator Float3(Int2 value) { return new Float3(value.X, value.Y, 0); } /// /// Performs an explicit conversion from to . /// /// The value. /// The result of the conversion. public static explicit operator Float4(Int2 value) { return new Float4(value.X, value.Y, 0, 0); } /// /// Performs an explicit conversion from to . /// /// The value. /// The result of the conversion. public static explicit operator Vector2(Int2 value) { return new Vector2(value.X, value.Y); } /// /// Performs an explicit conversion from to . /// /// The value. /// The result of the conversion. public static explicit operator Vector3(Int2 value) { return new Vector3(value.X, value.Y, 0); } /// /// Performs an explicit conversion from to . /// /// The value. /// The result of the conversion. public static explicit operator Vector4(Int2 value) { return new Vector4(value.X, value.Y, 0, 0); } /// /// Returns a that represents this instance. /// /// A that represents this instance. public override string ToString() { return string.Format(CultureInfo.CurrentCulture, _formatString, X, Y); } /// /// Returns a that represents this instance. /// /// The format. /// A that represents this instance. public string ToString(string format) { if (format == null) return ToString(); return string.Format(CultureInfo.CurrentCulture, _formatString, X.ToString(format, CultureInfo.CurrentCulture), Y.ToString(format, CultureInfo.CurrentCulture)); } /// /// Returns a that represents this instance. /// /// The format provider. /// A that represents this instance. public string ToString(IFormatProvider formatProvider) { return string.Format(formatProvider, _formatString, X, Y); } /// /// Returns a that represents this instance. /// /// The format. /// The format provider. /// A that represents this instance. public string ToString(string format, IFormatProvider formatProvider) { if (format == null) return ToString(formatProvider); return string.Format(formatProvider, _formatString, X.ToString(format, formatProvider), Y.ToString(format, formatProvider)); } /// /// Returns a hash code for this instance. /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. public override int GetHashCode() { unchecked { return (X.GetHashCode() * 397) ^ Y.GetHashCode(); } } /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// true if the specified is equal to this instance; otherwise, false. [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(ref Int2 other) { return other.X == X && other.Y == Y; } /// /// Determines whether the specified are equal. /// public static bool Equals(ref Int2 a, ref Int2 b) { return a.X == b.X && a.Y == b.Y; } /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// true if the specified is equal to this instance; otherwise, false. [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(Int2 other) { return Equals(ref other); } /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// true if the specified is equal to this instance; otherwise, false. public override bool Equals(object value) { return value is Int2 other && Equals(ref other); } } }