Restructuring
This commit is contained in:
parent
6a443e111c
commit
c7bce31f34
110
LinearAlgebra/src/Angle.cs
Normal file
110
LinearAlgebra/src/Angle.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using System;
|
||||
|
||||
namespace Passer.LinearAlgebra {
|
||||
|
||||
/// <summary>
|
||||
/// %Angle utilities
|
||||
/// </summary>
|
||||
public static class Angle {
|
||||
public const float pi = 3.1415927410125732421875F;
|
||||
// public static float Rad2Deg = 360.0f / ((float)Math.PI * 2);
|
||||
// public static float Deg2Rad = ((float)Math.PI * 2) / 360.0f;
|
||||
|
||||
public const float Deg2Rad = 360.0f / ((float)Math.PI * 2); //0.0174532924F;
|
||||
public const float Rad2Deg = ((float)Math.PI * 2) / 360.0f; //57.29578F;
|
||||
|
||||
/// <summary>
|
||||
/// Clamp the angle between the given min and max values
|
||||
/// </summary>
|
||||
/// <param name="angle">The angle to clamp</param>
|
||||
/// <param name="min">The minimum angle</param>
|
||||
/// <param name="max">The maximum angle</param>
|
||||
/// <returns>The clamped angle</returns>
|
||||
/// Angles are normalized
|
||||
public static float Clamp(float angle, float min, float max) {
|
||||
float normalizedAngle = Normalize(angle);
|
||||
return Float.Clamp(normalizedAngle, min, max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determine the angle difference, result is a normalized angle
|
||||
/// </summary>
|
||||
/// <param name="a">First first angle</param>
|
||||
/// <param name="b">The second angle</param>
|
||||
/// <returns>the angle between the two angles</returns>
|
||||
/// Angle values should be degrees
|
||||
public static float Difference(float a, float b) {
|
||||
float r = Normalize(b - a);
|
||||
return r;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Normalize an angle to the range -180 < angle <= 180
|
||||
/// </summary>
|
||||
/// <param name="angle">The angle to normalize</param>
|
||||
/// <returns>The normalized angle in interval (-180..180] </returns>
|
||||
/// Angle values should be in degrees
|
||||
public static float Normalize(float angle) {
|
||||
if (float.IsInfinity(angle))
|
||||
return angle;
|
||||
|
||||
while (angle <= -180) angle += 360;
|
||||
while (angle > 180) angle -= 360;
|
||||
return angle;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rotate from one angle to the other with a maximum degrees
|
||||
/// </summary>
|
||||
/// <param name="fromAngle">Starting angle</param>
|
||||
/// <param name="toAngle">Target angle</param>
|
||||
/// <param name="maxAngle">Maximum angle to rotate</param>
|
||||
/// <returns>The resulting angle</returns>
|
||||
/// This function is compatible with radian and degrees angles
|
||||
public static float MoveTowards(float fromAngle, float toAngle, float maxAngle) {
|
||||
float d = toAngle - fromAngle;
|
||||
d = Normalize(d);
|
||||
d = Math.Sign(d) * Float.Clamp(Math.Abs(d), 0, maxAngle);
|
||||
return fromAngle + d;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Map interval of angles between vectors [0..Pi] to interval [0..1]
|
||||
/// </summary>
|
||||
/// <param name="v1">The first vector</param>
|
||||
/// <param name="v2">The second vector</param>
|
||||
/// <returns>The resulting factor in interval [0..1]</returns>
|
||||
/// Vectors a and b must be normalized
|
||||
/// \deprecated Please use Vector2.ToFactor instead.
|
||||
[Obsolete("Please use Vector2.ToFactor instead.")]
|
||||
public static float ToFactor(Vector2 v1, Vector2 v2) {
|
||||
return (1 - Vector2.Dot(v1, v2)) / 2;
|
||||
}
|
||||
|
||||
// Normalize all vector angles to the range -180 < angle < 180
|
||||
//public static Vector3 Normalize(Vector3 angles) {
|
||||
// float x = Normalize(angles.x);
|
||||
// float y = Normalize(angles.y);
|
||||
// float z = Normalize(angles.z);
|
||||
// return new Vector3(x, y, z);
|
||||
//}
|
||||
|
||||
// Returns the signed angle in degrees between from and to.
|
||||
//public static float SignedAngle(Vector3 from, Vector3 to) {
|
||||
// float angle = Vector3.Angle(from, to);
|
||||
// Vector3 cross = Vector3.Cross(from, to);
|
||||
// if (cross.y < 0) angle = -angle;
|
||||
// return angle;
|
||||
//}
|
||||
|
||||
// Returns the signed angle in degrees between from and to.
|
||||
//public static float SignedAngle(Vector2 from, Vector2 to) {
|
||||
// float sign = Math.Sign(from.y * to.x - from.x * to.y);
|
||||
// return Vector2.Angle(from, to) * sign;
|
||||
//}
|
||||
|
||||
//public static Quaternion ToQuaternion(Rotation orientation) {
|
||||
// return new Quaternion(orientation.x, orientation.y, orientation.z, orientation.w);
|
||||
//}
|
||||
}
|
||||
}
|
41
LinearAlgebra/src/Float.cs
Normal file
41
LinearAlgebra/src/Float.cs
Normal file
@ -0,0 +1,41 @@
|
||||
namespace Passer.LinearAlgebra {
|
||||
|
||||
/// <summary>
|
||||
/// Float number utilities
|
||||
/// </summary>
|
||||
public class Float {
|
||||
/// <summary>
|
||||
/// The precision of float numbers
|
||||
/// </summary>
|
||||
public const float epsilon = 1E-05f;
|
||||
/// <summary>
|
||||
/// The square of the float number precision
|
||||
/// </summary>
|
||||
public const float sqrEpsilon = 1e-10f;
|
||||
|
||||
/// <summary>
|
||||
/// Clamp the value between the given minimum and maximum values
|
||||
/// </summary>
|
||||
/// <param name="f">The value to clamp</param>
|
||||
/// <param name="min">The minimum value</param>
|
||||
/// <param name="max">The maximum value</param>
|
||||
/// <returns>The clamped value</returns>
|
||||
public static float Clamp(float f, float min, float max) {
|
||||
if (f < min)
|
||||
return min;
|
||||
if (f > max)
|
||||
return max;
|
||||
return f;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clamp the value between to the interval [0..1]
|
||||
/// </summary>
|
||||
/// <param name="f">The value to clamp</param>
|
||||
/// <returns>The clamped value</returns>
|
||||
public static float Clamp01(float f) {
|
||||
return Clamp(f, 0, 1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
14
LinearAlgebra/src/LinearAlgebra.csproj
Normal file
14
LinearAlgebra/src/LinearAlgebra.csproj
Normal file
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
|
||||
<PackageReference Include="NUnit" Version="3.13.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
363
LinearAlgebra/src/Vector2.cs
Normal file
363
LinearAlgebra/src/Vector2.cs
Normal file
@ -0,0 +1,363 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Passer.LinearAlgebra {
|
||||
|
||||
public class Vector2Of<T> where T : IComparable<T> {
|
||||
public T x;
|
||||
public T y;
|
||||
|
||||
public Vector2Of(T x, T y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
|
||||
public class Vector2Int : Vector2Of<int> {
|
||||
public Vector2Int(int x, int y) : base(x, y) { }
|
||||
|
||||
public static Vector2Int operator -(Vector2Int v1, Vector2Int v2) {
|
||||
return new Vector2Int(v1.x - v2.x, v1.y - v2.y);
|
||||
}
|
||||
|
||||
public float magnitude {
|
||||
get {
|
||||
return (float)Math.Sqrt(this.x * this.x + this.y * this.y);
|
||||
}
|
||||
}
|
||||
|
||||
public static float Distance(Vector2Int v1, Vector2Int v2) {
|
||||
return (v1 - v2).magnitude;
|
||||
}
|
||||
}
|
||||
|
||||
public class Vector2Float : Vector2Of<float> {
|
||||
public Vector2Float(float x, float y) : base(x, y) { }
|
||||
|
||||
public static Vector2Float operator -(Vector2Float v1, Vector2Float v2) {
|
||||
return new Vector2Float(v1.x - v2.x, v1.y - v2.y);
|
||||
}
|
||||
|
||||
public float magnitude {
|
||||
get {
|
||||
return (float)Math.Sqrt(this.x * this.x + this.y * this.y);
|
||||
}
|
||||
}
|
||||
|
||||
public static float Distance(Vector2Float v1, Vector2Float v2) {
|
||||
return (v1 - v2).magnitude;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 2-dimensional vectors
|
||||
/// </summary>
|
||||
public struct Vector2 : IEquatable<Vector2> {
|
||||
|
||||
/// <summary>
|
||||
/// The right axis of the vector
|
||||
/// </summary>
|
||||
public float x; // left/right
|
||||
/// <summary>
|
||||
/// The upward/forward axis of the vector
|
||||
/// </summary>
|
||||
public float y; // forward/backward
|
||||
// directions are to be inline with Vector3 as much as possible...
|
||||
|
||||
/// <summary>
|
||||
/// Create a new 2-dimensional vector
|
||||
/// </summary>
|
||||
/// <param name="x">x axis value</param>
|
||||
/// <param name="y">y axis value</param>
|
||||
public Vector2(float x, float y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A vector with zero for all axis
|
||||
/// </summary>
|
||||
public static readonly Vector2 zero = new Vector2(0, 0);
|
||||
/// <summary>
|
||||
/// A vector with values (1, 1)
|
||||
/// </summary>
|
||||
public static readonly Vector2 one = new Vector2(1, 1);
|
||||
/// <summary>
|
||||
/// A vector with values (0, 1)
|
||||
/// </summary>
|
||||
public static readonly Vector2 up = new Vector2(0, 1);
|
||||
/// <summary>
|
||||
/// A vector with values (0, -1)
|
||||
/// </summary>
|
||||
public static readonly Vector2 down = new Vector2(0, -1);
|
||||
/// <summary>
|
||||
/// A vector with values (0, 1)
|
||||
/// </summary>
|
||||
public static readonly Vector2 forward = new Vector2(0, 1);
|
||||
/// <summary>
|
||||
/// A vector with values (0, -1)
|
||||
/// </summary>
|
||||
public static readonly Vector2 back = new Vector2(0, -1);
|
||||
/// <summary>
|
||||
/// A vector3 with values (-1, 0)
|
||||
/// </summary>
|
||||
public static readonly Vector2 left = new Vector2(-1, 0);
|
||||
/// <summary>
|
||||
/// A vector with values (1, 0)
|
||||
/// </summary>
|
||||
public static readonly Vector2 right = new Vector2(1, 0);
|
||||
|
||||
/// <summary>
|
||||
/// The squared length of this vector
|
||||
/// </summary>
|
||||
/// <returns>The squared length</returns>
|
||||
/// The squared length is computationally simpler than the real length.
|
||||
/// Think of Pythagoras A^2 + B^2 = C^2.
|
||||
/// This leaves out the calculation of the squared root of C.
|
||||
public float sqrMagnitude {
|
||||
get {
|
||||
float d = x * x + y * y;
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The length of this vector
|
||||
/// </summary>
|
||||
/// <returns>The length of this vector</returns>
|
||||
public float magnitude {
|
||||
get {
|
||||
float d = (float)Math.Sqrt(x * x + y * y);
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert the vector to a length of a 1
|
||||
/// </summary>
|
||||
/// <returns>The vector with length 1</returns>
|
||||
public Vector2 normalized {
|
||||
get {
|
||||
float l = magnitude;
|
||||
Vector2 v = zero;
|
||||
if (l > Float.epsilon)
|
||||
v = this / l;
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add two vectors
|
||||
/// </summary>
|
||||
/// <param name="v1">The first vector</param>
|
||||
/// <param name="v2">The second vector</param>
|
||||
/// <returns>The result of adding the two vectors</returns>
|
||||
public static Vector2 operator +(Vector2 v1, Vector2 v2) {
|
||||
Vector2 v = new Vector2(v1.x + v2.x, v1.y + v2.y);
|
||||
return v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subtract two vectors
|
||||
/// </summary>
|
||||
/// <param name="v1">The first vector</param>
|
||||
/// <param name="v2">The second vector</param>
|
||||
/// <returns>The result of adding the two vectors</returns>
|
||||
public static Vector2 operator -(Vector2 v1, Vector2 v2) {
|
||||
Vector2 v = new Vector2(v1.x - v2.x, v1.y - v2.y);
|
||||
return v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Negate the vector
|
||||
/// </summary>
|
||||
/// <param name="v1">The vector to negate</param>
|
||||
/// <returns>The negated vector</returns>
|
||||
/// This will result in a vector pointing in the opposite direction
|
||||
public static Vector2 operator -(Vector2 v1) {
|
||||
Vector2 v = new Vector2(-v1.x, -v1.y);
|
||||
return v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scale a vector uniformly up
|
||||
/// </summary>
|
||||
/// <param name="v1">The vector to scale</param>
|
||||
/// <param name="f">The scaling factor</param>
|
||||
/// <returns>The scaled vector</returns>
|
||||
/// Each component of the vector will be multipled with the same factor.
|
||||
public static Vector2 operator *(Vector2 v1, float f) {
|
||||
Vector2 v = new Vector2(v1.x * f, v1.y * f);
|
||||
return v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scale a vector uniformly up
|
||||
/// </summary>
|
||||
/// <param name="f">The scaling factor</param>
|
||||
/// <param name="v1">The vector to scale</param>
|
||||
/// <returns>The scaled vector</returns>
|
||||
/// Each component of the vector will be multipled with the same factor.
|
||||
public static Vector2 operator *(float f, Vector2 v1) {
|
||||
Vector2 v = new Vector2(f * v1.x, f * v1.y);
|
||||
return v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scale a vector uniformly down
|
||||
/// </summary>
|
||||
/// <param name="v1">The vector to scale</param>
|
||||
/// <param name="f">The scaling factor</param>
|
||||
/// <returns>The scaled vector</returns>
|
||||
/// Each component of the vector will be devided by the same factor.
|
||||
public static Vector2 operator /(Vector2 v1, float f) {
|
||||
Vector2 v = new Vector2(v1.x / f, v1.y / f);
|
||||
return v;
|
||||
}
|
||||
|
||||
/// <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 bool Equals(Vector2 v1) => x == v1.x && y == v1.y;
|
||||
|
||||
/// <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 bool Equals(object obj) {
|
||||
if (!(obj is Vector2 v))
|
||||
return false;
|
||||
|
||||
return (x == v.x && y == v.y);
|
||||
}
|
||||
|
||||
/// <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 ==(Vector2 v1, Vector2 v2) {
|
||||
return (v1.x == v2.x && v1.y == v2.y);
|
||||
}
|
||||
|
||||
/// <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 !=(Vector2 v1, Vector2 v2) {
|
||||
return (v1.x != v2.x || v1.y != v2.y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an hash code for the vector
|
||||
/// </summary>
|
||||
/// <returns>The hash code</returns>
|
||||
public override int GetHashCode() {
|
||||
return (x, y).GetHashCode();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the distance between two vectors
|
||||
/// </summary>
|
||||
/// <param name="v1">The first vector</param>
|
||||
/// <param name="v2">The second vector</param>
|
||||
/// <returns>The distance between the two vectors</returns>
|
||||
public static float Distance(Vector2 v1, Vector2 v2) {
|
||||
float x = v1.x - v2.x;
|
||||
float y = v1.y - v2.y;
|
||||
float d = (float)Math.Sqrt(x * x + y * y);
|
||||
return d;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The dot product of two vectors
|
||||
/// </summary>
|
||||
/// <param name="v1">The first vector</param>
|
||||
/// <param name="v2">The second vector</param>
|
||||
/// <returns>The dot product of the two vectors</returns>
|
||||
public static float Dot(Vector2 v1, Vector2 v2) {
|
||||
return v1.x * v2.x + v1.y * v2.y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lerp between two vectors
|
||||
/// </summary>
|
||||
/// <param name="v1">The from vector</param>
|
||||
/// <param name="v2">The to vector</param>
|
||||
/// <param name="f">The interpolation distance [0..1]</param>
|
||||
/// <returns>The lerped vector</returns>
|
||||
/// The factor f is unclamped. Value 0 matches the *v1* vector, Value 1
|
||||
/// matches the *v2* vector Value -1 is *v1* vector minus the difference
|
||||
/// between *v1* and *v2* etc.
|
||||
public static Vector2 Lerp(Vector2 v1, Vector2 v2, float f) {
|
||||
Vector2 v = v1 + (v2 - v1) * f;
|
||||
return v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculate the signed angle between two vectors.
|
||||
/// </summary>
|
||||
/// <param name="from">The starting vector</param>
|
||||
/// <param name="to">The ending vector</param>
|
||||
/// <param name="axis">The axis to rotate around</param>
|
||||
/// <returns>The signed angle in degrees</returns>
|
||||
public static float SignedAngle(Vector2 from, Vector2 to) {
|
||||
//float sign = Math.Sign(v1.y * v2.x - v1.x * v2.y);
|
||||
//return Vector2.Angle(v1, v2) * sign;
|
||||
|
||||
float sqrMagFrom = from.sqrMagnitude;
|
||||
float sqrMagTo = to.sqrMagnitude;
|
||||
|
||||
if (sqrMagFrom == 0 || sqrMagTo == 0)
|
||||
return 0;
|
||||
//if (!isfinite(sqrMagFrom) || !isfinite(sqrMagTo))
|
||||
// return nanf("");
|
||||
|
||||
float angleFrom = (float)Math.Atan2(from.y, from.x);
|
||||
float angleTo = (float)Math.Atan2(to.y, to.x);
|
||||
return (angleTo - angleFrom) * Angle.Rad2Deg;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rotates the vector with the given angle
|
||||
/// </summary>
|
||||
/// <param name="v1">The vector to rotate</param>
|
||||
/// <param name="angle">The angle in degrees</param>
|
||||
/// <returns></returns>
|
||||
public static Vector2 Rotate(Vector2 v1, float angle) {
|
||||
float sin = (float)Math.Sin(angle * Angle.Deg2Rad);
|
||||
float cos = (float)Math.Cos(angle * Angle.Deg2Rad);
|
||||
|
||||
float tx = v1.x;
|
||||
float ty = v1.y;
|
||||
Vector2 v = new Vector2() {
|
||||
x = (cos * tx) - (sin * ty),
|
||||
y = (sin * tx) + (cos * ty)
|
||||
};
|
||||
return v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Map interval of angles between vectors [0..Pi] to interval [0..1]
|
||||
/// </summary>
|
||||
/// <param name="v1">The first vector</param>
|
||||
/// <param name="v2">The second vector</param>
|
||||
/// <returns>The resulting factor in interval [0..1]</returns>
|
||||
/// Vectors a and b must be normalized
|
||||
public static float ToFactor(Vector2 v1, Vector2 v2) {
|
||||
return (1 - Vector2.Dot(v1, v2)) / 2;
|
||||
}
|
||||
}
|
||||
}
|
179
LinearAlgebra/src/Vector3.cs
Normal file
179
LinearAlgebra/src/Vector3.cs
Normal file
@ -0,0 +1,179 @@
|
||||
#if !UNITY_5_3_OR_NEWER
|
||||
using System;
|
||||
|
||||
namespace Passer.LinearAlgebra {
|
||||
public class Vector3Of<T> {
|
||||
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<int> {
|
||||
public Vector3Int(int x, int y, int z) : base(x, y, z) { }
|
||||
}
|
||||
public class Vector3Float : Vector3Of<float> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 3-dimensional vectors
|
||||
/// </summary>
|
||||
/// This uses the right-handed coordinate system.
|
||||
public struct Vector3 : IEquatable<Vector3> {
|
||||
|
||||
/// <summary>
|
||||
/// The right axis of the vector
|
||||
/// </summary>
|
||||
public float x; //> left/right
|
||||
/// <summary>
|
||||
/// The upward axis of the vector
|
||||
/// </summary>
|
||||
public float y; //> up/down
|
||||
/// <summary>
|
||||
/// The forward axis of the vector
|
||||
/// </summary>
|
||||
public float z; //> forward/backward
|
||||
|
||||
/// <summary>
|
||||
/// Create a new 3-dimensional vector
|
||||
/// </summary>
|
||||
/// <param name="x">x axis value</param>
|
||||
/// <param name="y">y axis value</param>
|
||||
/// <param name="z">z axis value</param>
|
||||
public Vector3(float x, float y, float z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A vector with zero for all axis
|
||||
/// </summary>
|
||||
public static readonly Vector3 zero = new Vector3(0, 0, 0);
|
||||
/// <summary>
|
||||
/// A vector with one for all axis
|
||||
/// </summary>
|
||||
public static readonly Vector3 one = new Vector3(1, 1, 1);
|
||||
/// <summary>
|
||||
/// A vector3 with values (-1, 0, 0)
|
||||
/// </summary>
|
||||
public static readonly Vector3 left = new Vector3(-1, 0, 0);
|
||||
/// <summary>
|
||||
/// A vector with values (1, 0, 0)
|
||||
/// </summary>
|
||||
public static readonly Vector3 right = new Vector3(1, 0, 0);
|
||||
/// <summary>
|
||||
/// A vector with values (0, -1, 0)
|
||||
/// </summary>
|
||||
public static readonly Vector3 down = new Vector3(0, -1, 0);
|
||||
/// <summary>
|
||||
/// A vector with values (0, 1, 0)
|
||||
/// </summary>
|
||||
public static readonly Vector3 up = new Vector3(0, 1, 0);
|
||||
/// <summary>
|
||||
/// A vector with values (0, 0, -1)
|
||||
/// </summary>
|
||||
public static readonly Vector3 back = new Vector3(0, -1, 0);
|
||||
/// <summary>
|
||||
/// A vector with values (0, 0, 1)
|
||||
/// </summary>
|
||||
public static readonly Vector3 forward = new Vector3(0, 1, 0);
|
||||
|
||||
public float magnitude {
|
||||
get {
|
||||
float d = (float)Math.Sqrt(x * x + y * y);
|
||||
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
|
162
LinearAlgebra/test/AngleTest.cs
Normal file
162
LinearAlgebra/test/AngleTest.cs
Normal file
@ -0,0 +1,162 @@
|
||||
#if NUNIT
|
||||
using NUnit.Framework;
|
||||
using Passer.LinearAlgebra;
|
||||
|
||||
namespace LinearAlgebraTest {
|
||||
public class AngleTest {
|
||||
|
||||
[Test]
|
||||
public void Normalize() {
|
||||
float r = 0;
|
||||
|
||||
r = Angle.Normalize(90);
|
||||
Assert.AreEqual(r, 90, "Normalize 90");
|
||||
|
||||
r = Angle.Normalize(-90);
|
||||
Assert.AreEqual(r, -90, "Normalize -90");
|
||||
|
||||
r = Angle.Normalize(270);
|
||||
Assert.AreEqual(r, -90, "Normalize 270");
|
||||
|
||||
r = Angle.Normalize(270 + 360);
|
||||
Assert.AreEqual(r, -90, "Normalize 270+360");
|
||||
|
||||
r = Angle.Normalize(-270);
|
||||
Assert.AreEqual(r, 90, "Normalize -270");
|
||||
|
||||
r = Angle.Normalize(-270 - 360);
|
||||
Assert.AreEqual(r, 90, "Normalize -270-360");
|
||||
|
||||
r = Angle.Normalize(0);
|
||||
Assert.AreEqual(r, 0, "Normalize 0");
|
||||
|
||||
r = Angle.Normalize(float.PositiveInfinity);
|
||||
Assert.AreEqual(r, float.PositiveInfinity, "Normalize INFINITY");
|
||||
|
||||
r = Angle.Normalize(float.NegativeInfinity);
|
||||
Assert.AreEqual(r, float.NegativeInfinity, "Normalize INFINITY");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Clamp() {
|
||||
float r = 0;
|
||||
|
||||
r = Angle.Clamp(1, 0, 2);
|
||||
Assert.AreEqual(r, 1, "Clamp 1 0 2");
|
||||
|
||||
r = Angle.Clamp(-1, 0, 2);
|
||||
Assert.AreEqual(r, 0, "Clamp -1 0 2");
|
||||
|
||||
r = Angle.Clamp(3, 0, 2);
|
||||
Assert.AreEqual(r, 2, "Clamp 3 0 2");
|
||||
|
||||
r = Angle.Clamp(1, 0, 0);
|
||||
Assert.AreEqual(r, 0, "Clamp 1 0 0");
|
||||
|
||||
r = Angle.Clamp(0, 0, 0);
|
||||
Assert.AreEqual(r, 0, "Clamp 0 0 0");
|
||||
|
||||
r = Angle.Clamp(0, 1, -1);
|
||||
Assert.AreEqual(r, 1, "Clamp 0 1 -1");
|
||||
|
||||
r = Angle.Clamp(1, 0, float.PositiveInfinity);
|
||||
Assert.AreEqual(r, 1, "Clamp 1 0 INFINITY");
|
||||
|
||||
r = Angle.Clamp(1, float.NegativeInfinity, 1);
|
||||
Assert.AreEqual(r, 1, "Clamp 1 -INFINITY 1");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Difference() {
|
||||
float r = 0;
|
||||
|
||||
r = Angle.Difference(0, 90);
|
||||
Assert.AreEqual(r, 90, "Difference 0 90");
|
||||
|
||||
r = Angle.Difference(0, -90);
|
||||
Assert.AreEqual(r, -90, "Difference 0 -90");
|
||||
|
||||
r = Angle.Difference(0, 270);
|
||||
Assert.AreEqual(r, -90, "Difference 0 270");
|
||||
|
||||
r = Angle.Difference(0, -270);
|
||||
Assert.AreEqual(r, 90, "Difference 0 -270");
|
||||
|
||||
r = Angle.Difference(90, 0);
|
||||
Assert.AreEqual(r, -90, "Difference 90 0");
|
||||
|
||||
r = Angle.Difference(-90, 0);
|
||||
Assert.AreEqual(r, 90, "Difference -90 0");
|
||||
|
||||
r = Angle.Difference(0, 0);
|
||||
Assert.AreEqual(r, 0, "Difference 0 0");
|
||||
|
||||
r = Angle.Difference(90, 90);
|
||||
Assert.AreEqual(r, 0, "Difference 90 90");
|
||||
|
||||
r = Angle.Difference(0, float.PositiveInfinity);
|
||||
Assert.AreEqual(r, float.PositiveInfinity, "Difference 0 INFINITY");
|
||||
|
||||
r = Angle.Difference(0, float.NegativeInfinity);
|
||||
Assert.AreEqual(r, float.NegativeInfinity, "Difference 0 -INFINITY");
|
||||
|
||||
r = Angle.Difference(float.NegativeInfinity, float.PositiveInfinity);
|
||||
Assert.AreEqual(r, float.PositiveInfinity, "Difference -INFINITY INFINITY");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MoveTowards() {
|
||||
float r = 0;
|
||||
|
||||
r = Angle.MoveTowards(0, 90, 30);
|
||||
Assert.AreEqual(r, 30, "MoveTowards 0 90 30");
|
||||
|
||||
r = Angle.MoveTowards(0, 90, 90);
|
||||
Assert.AreEqual(r, 90, "MoveTowards 0 90 90");
|
||||
|
||||
r = Angle.MoveTowards(0, 90, 180);
|
||||
Assert.AreEqual(r, 90, "MoveTowards 0 90 180");
|
||||
|
||||
r = Angle.MoveTowards(0, 90, 270);
|
||||
Assert.AreEqual(r, 90, "MoveTowrads 0 90 270");
|
||||
|
||||
r = Angle.MoveTowards(0, 90, -30);
|
||||
Assert.AreEqual(r, -30, "MoveTowards 0 90 -30");
|
||||
|
||||
r = Angle.MoveTowards(0, -90, -30);
|
||||
Assert.AreEqual(r, 30, "MoveTowards 0 -90 -30");
|
||||
|
||||
r = Angle.MoveTowards(0, -90, -90);
|
||||
Assert.AreEqual(r, 90, "MoveTowards 0 -90 -90");
|
||||
|
||||
r = Angle.MoveTowards(0, -90, -180);
|
||||
Assert.AreEqual(r, 180, "MoveTowards 0 -90 -180");
|
||||
|
||||
r = Angle.MoveTowards(0, -90, -270);
|
||||
Assert.AreEqual(r, 270, "MoveTowrads 0 -90 -270");
|
||||
|
||||
r = Angle.MoveTowards(0, 90, 0);
|
||||
Assert.AreEqual(r, 0, "MoveTowards 0 90 0");
|
||||
|
||||
r = Angle.MoveTowards(0, 0, 0);
|
||||
Assert.AreEqual(r, 0, "MoveTowards 0 0 0");
|
||||
|
||||
r = Angle.MoveTowards(0, 0, 30);
|
||||
Assert.AreEqual(r, 0, "MoveTowrads 0 0 30");
|
||||
|
||||
r = Angle.MoveTowards(0, 90, float.PositiveInfinity);
|
||||
Assert.AreEqual(r, 90, "MoveTowards 0 90 INFINITY");
|
||||
|
||||
r = Angle.MoveTowards(0, float.PositiveInfinity, 30);
|
||||
Assert.AreEqual(r, 30, "MoveTowrads 0 INFINITY 30");
|
||||
|
||||
r = Angle.MoveTowards(0, -90, float.NegativeInfinity);
|
||||
Assert.AreEqual(r, float.PositiveInfinity, "MoveTowards 0 -90 -INFINITY");
|
||||
|
||||
r = Angle.MoveTowards(0, float.NegativeInfinity, -30);
|
||||
Assert.AreEqual(r, 30, "MoveTowrads 0 -INFINITY -30");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
19
LinearAlgebra/test/LinearAlgebra_Test.csproj
Normal file
19
LinearAlgebra/test/LinearAlgebra_Test.csproj
Normal file
@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
|
||||
<PackageReference Include="NUnit" Version="3.13.2" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\src\LinearAlgebra.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.0.31903.59
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test", "test\test.csproj", "{B65BB41E-5A93-46FC-BA68-B865F50D57BD}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoboidControl_Test", "test\RoboidControl_Test.csproj", "{B65BB41E-5A93-46FC-BA68-B865F50D57BD}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{D6086F71-404B-4D18-BBE9-45947ED33DB2}"
|
||||
EndProject
|
||||
@ -11,14 +11,17 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BB2B", "Examples\BB2B\BB2B.
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoboidControl", "src\RoboidControl.csproj", "{72BFCD03-FA78-4D0B-8B36-32301D60D6DD}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "LinearAlgebra", "LinearAlgebra", "{3B5ED006-C6FC-4776-A6DC-FEE4E6909C03}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinearAlgebra", "LinearAlgebra\src\LinearAlgebra.csproj", "{CDCD9CE5-3224-4DB7-B7D6-5BB0714189B3}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinearAlgebra_Test", "LinearAlgebra\test\LinearAlgebra_Test.csproj", "{8F286220-E70C-4E77-BDA6-6F28B726E320}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{B65BB41E-5A93-46FC-BA68-B865F50D57BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B65BB41E-5A93-46FC-BA68-B865F50D57BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
@ -32,8 +35,21 @@ Global
|
||||
{72BFCD03-FA78-4D0B-8B36-32301D60D6DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{72BFCD03-FA78-4D0B-8B36-32301D60D6DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{72BFCD03-FA78-4D0B-8B36-32301D60D6DD}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{CDCD9CE5-3224-4DB7-B7D6-5BB0714189B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{CDCD9CE5-3224-4DB7-B7D6-5BB0714189B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CDCD9CE5-3224-4DB7-B7D6-5BB0714189B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CDCD9CE5-3224-4DB7-B7D6-5BB0714189B3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{8F286220-E70C-4E77-BDA6-6F28B726E320}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{8F286220-E70C-4E77-BDA6-6F28B726E320}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8F286220-E70C-4E77-BDA6-6F28B726E320}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8F286220-E70C-4E77-BDA6-6F28B726E320}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{488F28B2-A2CC-4E32-8D3B-7DB4EB1485F9} = {D6086F71-404B-4D18-BBE9-45947ED33DB2}
|
||||
{CDCD9CE5-3224-4DB7-B7D6-5BB0714189B3} = {3B5ED006-C6FC-4776-A6DC-FEE4E6909C03}
|
||||
{8F286220-E70C-4E77-BDA6-6F28B726E320} = {3B5ED006-C6FC-4776-A6DC-FEE4E6909C03}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
@ -1,11 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Passer.LinearAlgebra {
|
||||
|
||||
public class Angle {
|
||||
public const float pi = 3.1415927410125732421875F;
|
||||
public static float Rad2Deg = 360.0f / ((float)Math.PI * 2);
|
||||
public static float Deg2Rad = ((float)Math.PI * 2) / 360.0f;
|
||||
}
|
||||
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Passer.LinearAlgebra {
|
||||
|
||||
public class Vector2Of<T> where T : IComparable<T> {
|
||||
public T x;
|
||||
public T y;
|
||||
|
||||
public Vector2Of(T x, T y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
|
||||
public class Vector2Int : Vector2Of<int> {
|
||||
public Vector2Int(int x, int y) : base(x, y) { }
|
||||
|
||||
public static Vector2Int operator -(Vector2Int v1, Vector2Int v2) {
|
||||
return new Vector2Int(v1.x - v2.x, v1.y - v2.y);
|
||||
}
|
||||
|
||||
public float magnitude {
|
||||
get {
|
||||
return (float)Math.Sqrt(this.x * this.x + this.y * this.y);
|
||||
}
|
||||
}
|
||||
|
||||
public static float Distance(Vector2Int v1, Vector2Int v2) {
|
||||
return (v1 - v2).magnitude;
|
||||
}
|
||||
}
|
||||
|
||||
public class Vector2Float : Vector2Of<float> {
|
||||
public Vector2Float(float x, float y) : base(x, y) { }
|
||||
|
||||
public static Vector2Float operator -(Vector2Float v1, Vector2Float v2) {
|
||||
return new Vector2Float(v1.x - v2.x, v1.y - v2.y);
|
||||
}
|
||||
|
||||
public float magnitude {
|
||||
get {
|
||||
return (float)Math.Sqrt(this.x * this.x + this.y * this.y);
|
||||
}
|
||||
}
|
||||
|
||||
public static float Distance(Vector2Float v1, Vector2Float v2) {
|
||||
return (v1 - v2).magnitude;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
#if !UNITY_5_3_OR_NEWER
|
||||
using System;
|
||||
|
||||
namespace Passer.LinearAlgebra {
|
||||
public class Vector3Of<T> {
|
||||
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<int> {
|
||||
public Vector3Int(int x, int y, int z) : base(x, y, z) { }
|
||||
}
|
||||
public class Vector3Float : Vector3Of<float> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -9,6 +9,6 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
|
||||
<PackageReference Include="NUnit" Version="3.13.2" />
|
||||
<ProjectReference Include="..\LinearAlgebra\src\LinearAlgebra.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Loading…
x
Reference in New Issue
Block a user