Added the InverseMagnitude property to number group types.

This commit is contained in:
That_One_Nerd 2023-07-26 09:38:11 -04:00
parent 950b67dde1
commit 31609253f6
7 changed files with 7 additions and 1 deletions

View File

@ -16,6 +16,7 @@ public record struct Float2 : IAbsolute<Float2>, IAverage<Float2>, ICeiling<Floa
public static Float2 One => new(1, 1); public static Float2 One => new(1, 1);
public static Float2 Zero => new(0, 0); public static Float2 Zero => new(0, 0);
public float InverseMagnitude => Mathf.InverseSqrt(x * x + y * y);
public float Magnitude => Mathf.Sqrt(x * x + y * y); public float Magnitude => Mathf.Sqrt(x * x + y * y);
public Float2 Normalized => this * Mathf.InverseSqrt(x * x + y * y); public Float2 Normalized => this * Mathf.InverseSqrt(x * x + y * y);

View File

@ -20,6 +20,7 @@ public record struct Float3 : IAbsolute<Float3>, IAverage<Float3>,
public static Float3 One => new(1, 1, 1); public static Float3 One => new(1, 1, 1);
public static Float3 Zero => new(0, 0, 0); public static Float3 Zero => new(0, 0, 0);
public float InverseMagnitude => Mathf.InverseSqrt(x * x + y * y + z * z);
public float Magnitude => Mathf.Sqrt(x * x + y * y + z * z); public float Magnitude => Mathf.Sqrt(x * x + y * y + z * z);
public Float3 Normalized => this * Mathf.InverseSqrt(x * x + y * y + z * z); public Float3 Normalized => this * Mathf.InverseSqrt(x * x + y * y + z * z);

View File

@ -21,6 +21,7 @@ public record struct Float4 : IAbsolute<Float4>,
public static Float4 One => new(1, 1, 1, 1); public static Float4 One => new(1, 1, 1, 1);
public static Float4 Zero => new(0, 0, 0, 0); public static Float4 Zero => new(0, 0, 0, 0);
public float InverseMagnitude => Mathf.InverseSqrt(x * x + y * y + z * z + w * w);
public float Magnitude => Mathf.Sqrt(x * x + y * y + z * z + w * w); public float Magnitude => Mathf.Sqrt(x * x + y * y + z * z + w * w);
public Float4 Normalized => this * Mathf.InverseSqrt(x * x + y * y + z * z + w * w); public Float4 Normalized => this * Mathf.InverseSqrt(x * x + y * y + z * z + w * w);

View File

@ -130,7 +130,7 @@ public record class Line : IAbsolute<Line>, IAverage<Line>, ICeiling<Line>, ICla
public bool Contains(Float3 vert) public bool Contains(Float3 vert)
{ {
Float3 diffA = a - vert, diffB = a - b; Float3 diffA = a - vert, diffB = a - b;
float lerpVal = diffA.Magnitude / diffB.Magnitude; float lerpVal = diffA.Magnitude * diffB.InverseMagnitude;
return Float3.Lerp(a, b, lerpVal) == vert; return Float3.Lerp(a, b, lerpVal) == vert;
} }

View File

@ -14,6 +14,7 @@ public record struct Int2 : IAbsolute<Int2>, IAverage<Int2>, IClamp<Int2>, IClam
public static Int2 One => new(1, 1); public static Int2 One => new(1, 1);
public static Int2 Zero => new(0, 0); public static Int2 Zero => new(0, 0);
public float InverseMagnitude => Mathf.InverseSqrt(x * x + y * y);
public float Magnitude => Mathf.Sqrt(x * x + y * y); public float Magnitude => Mathf.Sqrt(x * x + y * y);
public Int2 Normalized => (Int2)((Float2)this * Mathf.InverseSqrt(x * x + y * y)); public Int2 Normalized => (Int2)((Float2)this * Mathf.InverseSqrt(x * x + y * y));

View File

@ -18,6 +18,7 @@ public record struct Int3 : IAbsolute<Int3>, IAverage<Int3>, IClamp<Int3>, IClam
public static Int3 One => new(1, 1, 1); public static Int3 One => new(1, 1, 1);
public static Int3 Zero => new(0, 0, 0); public static Int3 Zero => new(0, 0, 0);
public float InverseMagnitude => Mathf.InverseSqrt(x * x + y * y + z * z);
public float Magnitude => Mathf.Sqrt(x * x + y * y + z * z); public float Magnitude => Mathf.Sqrt(x * x + y * y + z * z);
public Int3 Normalized => (Int3)((Float3)this * Mathf.InverseSqrt(x * x + y * y + z * z)); public Int3 Normalized => (Int3)((Float3)this * Mathf.InverseSqrt(x * x + y * y + z * z));

View File

@ -18,6 +18,7 @@ public record struct Int4 : IAbsolute<Int4>, IAverage<Int4>, IClamp<Int4>, IClam
public static Int4 One => new(1, 1, 1, 1); public static Int4 One => new(1, 1, 1, 1);
public static Int4 Zero => new(0, 0, 0, 0); public static Int4 Zero => new(0, 0, 0, 0);
public float InverseMagnitude => Mathf.InverseSqrt(x * x + y * y + z * z + w * w);
public float Magnitude => Mathf.Sqrt(x * x + y * y + z * z + w * w); public float Magnitude => Mathf.Sqrt(x * x + y * y + z * z + w * w);
public Int4 Normalized => (Int4)((Float4)this * Mathf.InverseSqrt(x * x + y * y + z * z + w * w)); public Int4 Normalized => (Int4)((Float4)this * Mathf.InverseSqrt(x * x + y * y + z * z + w * w));