using System; namespace LinearAlgebra { public struct Vector2Int { public int horizontal; public int vertical; public Vector2Int(int horizontal, int vertical) { this.horizontal = horizontal; this.vertical = vertical; } /// /// A vector with zero for all axis /// public static readonly Vector2Int zero = new(0, 0); /// /// A vector with values (1, 1) /// public static readonly Vector2Int one = new(1, 1); /// /// A vector with values (0, 1) /// public static readonly Vector2Int up = new(0, 1); /// /// A vector with values (0, -1) /// public static readonly Vector2Int down = new(0, -1); /// /// A vector with values (0, 1) /// public static readonly Vector2Int forward = new(0, 1); /// /// A vector with values (0, -1) /// public static readonly Vector2Int back = new(0, -1); /// /// A vector3 with values (-1, 0) /// public static readonly Vector2Int left = new(-1, 0); /// /// A vector with values (1, 0) /// public static readonly Vector2Int right = new(1, 0); /* /// /// Get an hash code for the vector /// /// The hash code public override int GetHashCode() { return (this.horizontal, this.vertical).GetHashCode(); } /// /// Tests if the vector has equal values as the given vector /// /// The vector to compare to /// true if the vector values are equal public readonly bool Equals(Vector2Int v) => this.horizontal == v.horizontal && vertical == v.vertical; */ /// /// Tests if the two vectors have equal values /// /// The first vector /// The second vector /// truewhen the vectors have equal values /// Note that this uses a Float equality check which cannot be not exact in all cases. /// In most cases it is better to check if the Vector2.Distance between the vectors is smaller than Float.epsilon /// Or more efficient: (v1 - v2).sqrMagnitude < Float.sqrEpsilon public static bool operator ==(Vector2Int v1, Vector2Int v2) { return (v1.horizontal == v2.horizontal && v1.vertical == v2.vertical); } /// /// Tests if two vectors have different values /// /// The first vector /// The second vector /// truewhen the vectors have different values /// Note that this uses a Float equality check which cannot be not exact in all case. /// In most cases it is better to check if the Vector2.Distance between the vectors is smaller than Float.epsilon. /// Or more efficient: (v1 - v2).sqrMagnitude < Float.sqrEpsilon public static bool operator !=(Vector2Int v1, Vector2Int v2) { return (v1.horizontal != v2.horizontal || v1.vertical != v2.vertical); } /// /// Tests if the vector is equal to the given object /// /// The object to compare to /// false when the object is not a Vector2 or does not have equal values public override readonly bool Equals(object obj) { if (obj is not Vector2Int v) return false; return (this.horizontal == v.horizontal && this.vertical == v.vertical); } /// /// Get an hash code for the vector /// /// The hash code public override readonly int GetHashCode() { return HashCode.Combine(horizontal, vertical); } public readonly float sqrMagnitude => this.horizontal * this.horizontal + this.vertical * this.vertical; public static float SqrMagnitudeOf(Vector2Int v) { return v.sqrMagnitude; } public readonly float magnitude => MathF.Sqrt(this.horizontal * this.horizontal + this.vertical * this.vertical); public static float MagnitudeOf(Vector2Int v) { return v.magnitude; } /// @brief Convert the vector to a length of 1 /// @return The vector normalized to a length of 1 public readonly Vector2Float normalized { get { float l = magnitude; Vector2Float v = Vector2Float.zero; if (l > Float.epsilon) v = new Vector2Float(this) / l; return v; } } /// @brief Convert the vector to a length of 1 /// @param v The vector to convert /// @return The vector normalized to a length of 1 public static Vector2Float Normalize(Vector2Int v) { float num = v.magnitude; Vector2Float result = Vector2Float.zero; if (num > Float.epsilon) result = new Vector2Float(v) / num; return result; } public static Vector2Int operator -(Vector2Int v) { return new Vector2Int(-v.horizontal, -v.vertical); } public static Vector2Int operator -(Vector2Int v1, Vector2Int v2) { return new Vector2Int(v1.horizontal - v2.horizontal, v1.vertical - v2.vertical); } public static Vector2Int operator +(Vector2Int v1, Vector2Int v2) { return new Vector2Int(v1.horizontal + v2.horizontal, v1.vertical + v2.vertical); } public static Vector2Int operator /(Vector2Int v, int f) { return new Vector2Int(v.horizontal / f, v.vertical / f); } public static Vector2Int operator *(Vector2Int v1, int d) { return new Vector2Int(v1.horizontal * d, v1.vertical * d); } public static Vector2Int operator *(int d, Vector2Int v1) { return new Vector2Int(d * v1.horizontal, d * v1.vertical); } public static Vector2Int Scale(Vector2Int v1, Vector2Int v2) { return new Vector2Int(v1.horizontal * v2.horizontal, v1.vertical * v2.vertical); } /// @brief The dot product of two vectors /// @param v1 The first vector /// @param v2 The second vector /// @return The dot product of the two vectors public static int Dot(Vector2Int v1, Vector2Int v2) { return v1.horizontal * v2.horizontal + v1.vertical * v2.vertical; } public static float Distance(Vector2Int v1, Vector2Int v2) { return (v1 - v2).magnitude; } } }