-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51b54c6
commit 761496b
Showing
11 changed files
with
215 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,120 @@ | ||
using System.Runtime.CompilerServices; | ||
using Hypercube.Shared.Math.Extensions; | ||
using Hypercube.Shared.Math.Vector; | ||
|
||
namespace Hypercube.Shared.Math; | ||
|
||
public readonly struct Angle(double theta) | ||
public readonly struct Angle : IEquatable<Angle>, IEquatable<double> | ||
{ | ||
public static readonly Angle Zero = new(0); | ||
|
||
public readonly double Theta; | ||
|
||
public double Degrees | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => Theta * HyperMath.RadiansToDegrees; | ||
} | ||
|
||
public Vector2 Vector | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => new((float)System.Math.Cos(Theta), (float)System.Math.Sin(Theta)); | ||
} | ||
|
||
public Angle(double theta) | ||
{ | ||
Theta = theta; | ||
} | ||
|
||
public Angle(Vector2 vector2) | ||
{ | ||
vector2 = vector2.Normalized; | ||
Theta = System.Math.Atan2(vector2.X, vector2.Y); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool Equals(Angle other) | ||
{ | ||
return Theta.AboutEquals(other.Theta); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool Equals(double other) | ||
{ | ||
return Theta.AboutEquals(other); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public override bool Equals(object? obj) | ||
{ | ||
return (obj is double theta && Equals(theta)) || | ||
(obj is Angle angle && Equals(angle)); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public override int GetHashCode() | ||
{ | ||
return Theta.GetHashCode(); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public override string ToString() | ||
{ | ||
return $"{Degrees} deg"; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool operator ==(Angle a, double b) | ||
{ | ||
return a.Equals(b); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool operator !=(Angle a, double b) | ||
{ | ||
return !a.Equals(b); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool operator ==(Angle a, Angle b) | ||
{ | ||
return a.Equals(b); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool operator !=(Angle a, Angle b) | ||
{ | ||
return !a.Equals(b); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator double(Angle angle) | ||
{ | ||
return angle.Theta; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator Angle(double radians) | ||
{ | ||
return new Angle(radians); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator Angle(float radians) | ||
{ | ||
return new Angle(radians); | ||
} | ||
|
||
public readonly double Theta = theta; | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static Angle FromDegrees(double degrees) | ||
{ | ||
return new Angle(degrees * HyperMath.DegreesToRadians); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator float(Angle angle) | ||
public static Angle FromDegrees(float degrees) | ||
{ | ||
return (float)angle.Theta; | ||
return new Angle(degrees * HyperMath.DegreesToRadians); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace Hypercube.Shared.Math; | ||
|
||
public static class HyperMathF | ||
{ | ||
public const float PI = MathF.PI; | ||
|
||
public const float PIOver2 = PI / 2; | ||
public const float PIOver4 = PI / 4; | ||
public const float PIOver6 = PI / 6; | ||
|
||
public const float TwoPI = 2 * PI; | ||
public const float ThreePiOver2 = 3 * PI / 2; | ||
|
||
public const float RadiansToDegrees = 180 / PI; | ||
public const float DegreesToRadians = PI / 180; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Hypercube.Shared.Math; | ||
using Hypercube.Shared.Math.Vector; | ||
|
||
namespace Hypercube.UnitTests.Math; | ||
|
||
public static class AngleTest | ||
{ | ||
[Test] | ||
public static void Degrees() | ||
{ | ||
Assert.Multiple(() => | ||
{ | ||
Assert.That(new Angle(HyperMath.PI).Degrees, Is.EqualTo(180d).Within(0.01d)); | ||
Assert.That(new Angle(HyperMath.PIOver2).Degrees, Is.EqualTo(90d).Within(0.01d)); | ||
Assert.That(new Angle(HyperMath.PIOver4).Degrees, Is.EqualTo(45d).Within(0.01d)); | ||
Assert.That(new Angle(HyperMath.PIOver6).Degrees, Is.EqualTo(30d).Within(0.01d)); | ||
Assert.That(Angle.FromDegrees(180d), Is.EqualTo(new Angle(HyperMath.PI))); | ||
Assert.That(Angle.FromDegrees(90d), Is.EqualTo(new Angle(HyperMath.PIOver2))); | ||
Assert.That(Angle.FromDegrees(45d), Is.EqualTo(new Angle(HyperMath.PIOver4))); | ||
Assert.That(Angle.FromDegrees(30d), Is.EqualTo(new Angle(HyperMath.PIOver6))); | ||
}); | ||
Assert.Pass($"{nameof(Angle)} degrees passed"); | ||
} | ||
|
||
[Test] | ||
public static void Vector() | ||
{ | ||
Assert.Multiple(() => | ||
{ | ||
Assert.That(Angle.Zero.GetRoundVector(), Is.EqualTo(Vector2.UnitX)); | ||
Assert.That(new Angle(HyperMath.PI).GetRoundVector(), Is.EqualTo(-Vector2.UnitX)); | ||
Assert.That(new Angle(-HyperMath.PI).GetRoundVector(), Is.EqualTo(-Vector2.UnitX)); | ||
Assert.That(new Angle(HyperMath.PIOver2).GetRoundVector(), Is.EqualTo(Vector2.UnitY)); | ||
Assert.That(new Angle(HyperMath.ThreePiOver2).GetRoundVector(), Is.EqualTo(-Vector2.UnitY)); | ||
Assert.That(new Angle(-HyperMath.PIOver2).GetRoundVector(), Is.EqualTo(-Vector2.UnitY)); | ||
Assert.That(new Angle(HyperMath.PIOver4).GetRoundVector(), Is.EqualTo(Vector2.One.Normalized)); | ||
Assert.That(new Angle(HyperMath.ThreePiOver2 - HyperMath.PIOver4).GetRoundVector(), Is.EqualTo(-Vector2.One.Normalized)); | ||
}); | ||
|
||
Assert.Pass($"{nameof(Angle)} vector passed"); | ||
} | ||
|
||
private static Vector2 GetRoundVector(this Angle angle) | ||
{ | ||
var vector = angle.Vector; | ||
var x = MathF.Abs(vector.X) < 1e-15f ? 0 : vector.X; | ||
var y = MathF.Abs(vector.Y) < 1e-15f ? 0 : vector.Y; | ||
return new Vector2(x, y); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters