#if !UNITY_5_3_OR_NEWER using System; namespace LinearAlgebra { public class Vector3Of { public T x; public T y; public T z; public Vector3Of(T x, T y, T z) { this.x = x; this.y = y; this.z = z; } // public uint magnitude { // get => (float)Math.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z); // } } public class Vector3Int : Vector3Of { public Vector3Int(int x, int y, int z) : base(x, y, z) { } } public class Vector3Float : Vector3Of { public Vector3Float(float x, float y, float z) : base(x, y, z) { } public float magnitude { get => (float)Math.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z); } } /// /// 3-dimensional vectors /// /// This uses the right-handed coordinate system. public struct Vector3 : IEquatable { /// /// The right axis of the vector /// public float x; //> left/right /// /// The upward axis of the vector /// public float y; //> up/down /// /// The forward axis of the vector /// public float z; //> forward/backward /// /// Create a new 3-dimensional vector /// /// x axis value /// y axis value /// z axis value public Vector3(float x, float y, float z) { this.x = x; this.y = y; this.z = z; } /// /// A vector with zero for all axis /// public static readonly Vector3 zero = new Vector3(0, 0, 0); /// /// A vector with one for all axis /// public static readonly Vector3 one = new Vector3(1, 1, 1); /// /// A vector3 with values (-1, 0, 0) /// public static readonly Vector3 left = new Vector3(-1, 0, 0); /// /// A vector with values (1, 0, 0) /// public static readonly Vector3 right = new Vector3(1, 0, 0); /// /// A vector with values (0, -1, 0) /// public static readonly Vector3 down = new Vector3(0, -1, 0); /// /// A vector with values (0, 1, 0) /// public static readonly Vector3 up = new Vector3(0, 1, 0); /// /// A vector with values (0, 0, -1) /// public static readonly Vector3 back = new Vector3(0, -1, 0); /// /// A vector with values (0, 0, 1) /// public static readonly Vector3 forward = new Vector3(0, 1, 0); public readonly float magnitude { get { float d = (float)Math.Sqrt(x * x + y * y + z * z); return d; } } public Vector3 normalized { get { float l = magnitude; Vector3 v = zero; if (l > Float.epsilon) v = this / l; return v; } } public static Vector3 operator +(Vector3 v1, Vector3 v2) { Vector3 v = new Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); return v; } public static Vector3 operator -(Vector3 v1, Vector3 v2) { Vector3 v = new Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z); return v; } public static Vector3 operator -(Vector3 v1) { Vector3 v = new Vector3(-v1.x, -v1.y, -v1.z); return v; } public static Vector3 operator *(Vector3 v1, float d) { Vector3 v = new Vector3(v1.x * d, v1.y * d, v1.z * d); return v; } public static Vector3 operator *(float d, Vector3 v1) { Vector3 v = new Vector3(d * v1.x, d * v1.y, d * v1.z); return v; } public static Vector3 operator /(Vector3 v1, float d) { Vector3 v = new Vector3(v1.x / d, v1.y / d, v1.z / d); return v; } public bool Equals(Vector3 v) => (x == v.x && y == v.y && z == v.z); public override bool Equals(object obj) { if (!(obj is Vector3 v)) return false; return (x == v.x && y == v.y && z == v.z); } public static bool operator ==(Vector3 v1, Vector3 v2) { return (v1.x == v2.x && v1.y == v2.y && v1.z == v2.z); } public static bool operator !=(Vector3 v1, Vector3 v2) { return (v1.x != v2.x || v1.y != v2.y || v1.z != v2.z); } public override int GetHashCode() { return (x, y, z).GetHashCode(); } public static float Distance(Vector3 v1, Vector3 v2) { return (v2 - v1).magnitude; } public static float Dot(Vector3 v1, Vector3 v2) { return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; } public static Vector3 Lerp(Vector3 v1, Vector3 v2, float f) { Vector3 v = v1 + (v2 - v1) * f; return v; } } } #endif