Pascal Serrarens 8e87e4ea77 Squashed 'NanoBrain/' content from commit b3423b9
git-subtree-dir: NanoBrain
git-subtree-split: b3423b99a752cdabbc4e7c51565fb54425481feb
2026-04-07 09:12:29 +02:00

185 lines
7.3 KiB
C#

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;
}
/// <summary>
/// A vector with zero for all axis
/// </summary>
public static readonly Vector2Int zero = new(0, 0);
/// <summary>
/// A vector with values (1, 1)
/// </summary>
public static readonly Vector2Int one = new(1, 1);
/// <summary>
/// A vector with values (0, 1)
/// </summary>
public static readonly Vector2Int up = new(0, 1);
/// <summary>
/// A vector with values (0, -1)
/// </summary>
public static readonly Vector2Int down = new(0, -1);
/// <summary>
/// A vector with values (0, 1)
/// </summary>
public static readonly Vector2Int forward = new(0, 1);
/// <summary>
/// A vector with values (0, -1)
/// </summary>
public static readonly Vector2Int back = new(0, -1);
/// <summary>
/// A vector3 with values (-1, 0)
/// </summary>
public static readonly Vector2Int left = new(-1, 0);
/// <summary>
/// A vector with values (1, 0)
/// </summary>
public static readonly Vector2Int right = new(1, 0);
/*
/// <summary>
/// Get an hash code for the vector
/// </summary>
/// <returns>The hash code</returns>
public override int GetHashCode() {
return (this.horizontal, this.vertical).GetHashCode();
}
/// <summary>
/// Tests if the vector has equal values as the given vector
/// </summary>
/// <param name="v1">The vector to compare to</param>
/// <returns><em>true</em> if the vector values are equal</returns>
public readonly bool Equals(Vector2Int v) => this.horizontal == v.horizontal && vertical == v.vertical;
*/
/// <summary>
/// Tests if the two vectors have equal values
/// </summary>
/// <param name="v1">The first vector</param>
/// <param name="v2">The second vector</param>
/// <returns><em>true</em>when the vectors have equal values</returns>
/// 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);
}
/// <summary>
/// Tests if two vectors have different values
/// </summary>
/// <param name="v1">The first vector</param>
/// <param name="v2">The second vector</param>
/// <returns><em>true</em>when the vectors have different values</returns>
/// 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);
}
/// <summary>
/// Tests if the vector is equal to the given object
/// </summary>
/// <param name="obj">The object to compare to</param>
/// <returns><em>false</em> when the object is not a Vector2 or does not have equal values</returns>
public override readonly bool Equals(object obj) {
if (obj is not Vector2Int v)
return false;
return (this.horizontal == v.horizontal && this.vertical == v.vertical);
}
/// <summary>
/// Get an hash code for the vector
/// </summary>
/// <returns>The hash code</returns>
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;
}
}
}