Skip to content

Commit

Permalink
Replace HyperMathF to HyperMath
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Oct 26, 2024
1 parent ab73fd3 commit 139933b
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private void AddCircleBatch(int startIndex, Circle circle, Color color, int segm

private void AddCircleBatch(uint startIndex, Circle circle, Color color, int segments)
{
var total = HyperMathF.TwoPI / segments;
var total = HyperMath.TwoPIf / segments;
for (var i = 0; i < segments; i++)
{
var theta = total * i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void OnFrameUpdate(ref UpdateFrameEvent args)
if (_cameraManager.MainCamera is not null)
{
cameraTitle =
$"| cPos: {_cameraManager.MainCamera.Position}| cRot: {_cameraManager.MainCamera.Rotation * HyperMathF.RadiansToDegrees} | cScale: {_cameraManager.MainCamera.Scale}";
$"| cPos: {_cameraManager.MainCamera.Position}| cRot: {_cameraManager.MainCamera.Rotation * HyperMath.RadiansToDegreesF} | cScale: {_cameraManager.MainCamera.Scale}";
}

_windowing.WindowSetTitle(MainWindow,
Expand Down
101 changes: 96 additions & 5 deletions Hypercube.Mathematics/HyperMath.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,114 @@
namespace Hypercube.Mathematics;
using JetBrains.Annotations;

namespace Hypercube.Mathematics;

[PublicAPI]
public static class HyperMath
{
/// <summary>
/// Represents the ratio of the circumference of a circle to its diameter,
/// specified by the constant, π.
/// </summary>
public const double PI = Math.PI;

/// <summary>
/// Represents the ratio of the circumference of a circle to its diameter,
/// specified by the constant, π.
/// </summary>
public const float PIf = MathF.PI;

public const double PIOver2 = PI / 2;
public const double PIOver4 = PI / 4;
public const double PIOver6 = PI / 6;

public const double TwoPI = 2 * PI;
public const double ThreePiOver2 = 3 * PI / 2;

public const double TwoPI = PI * 2;
public const double ThreePiOver2 = PI / 2 * 3;

public const double RadiansToDegrees = 180 / PI;
public const double DegreesToRadians = PI / 180;

public const float PIOver2F = PIf / 2;
public const float PIOver4F = PIf / 4;
public const float PIOver6F = PIf / 6;
public const float TwoPIf = PIf * 2;
public const float ThreePiOver2F = PIf / 2 * 3;

public const float RadiansToDegreesF = 180 / PIf;
public const float DegreesToRadiansF = PIf / 180;

public static byte MoveTowards(byte current, byte target, byte distance)
{
return current < target ?
(byte)Math.Min(current + distance, target) :
(byte)Math.Max(current - distance, target);
}

public static sbyte MoveTowards(sbyte current,sbyte target, sbyte distance)
{
return current < target ?
(sbyte)Math.Min(current + distance, target) :
(sbyte)Math.Max(current - distance, target);
}

public static short MoveTowards(short current, short target, short distance)
{
return current < target ?
(short)Math.Min(current + distance, target) :
(short)Math.Max(current - distance, target);
}

public static ushort MoveTowards(ushort current, ushort target, ushort distance)
{
return current < target ?
(ushort)Math.Min(current + distance, target) :
(ushort)Math.Max(current - distance, target);
}

public static int MoveTowards(int current, int target, int distance)
{
return current < target ?
Math.Min(current + distance, target) :
Math.Max(current - distance, target);
}

public static nint MoveTowards(nint current, nint target, nint distance)
{
return current < target ?
Math.Min(current + distance, target) :
Math.Max(current - distance, target);
}

public static long MoveTowards(long current, long target, long distance)
{
return current < target ?
Math.Min(current + distance, target) :
Math.Max(current - distance, target);
}

public static ulong MoveTowards(ulong current, ulong target, ulong distance)
{
return current < target ?
Math.Min(current + distance, target) :
Math.Max(current - distance, target);
}

public static uint MoveTowards(uint current, uint target, uint distance)
{
return current < target ?
Math.Min(current + distance, target) :
Math.Max(current - distance, target);
}

public static float MoveTowards(float current, float target, float distance)
{
return current < target ?
MathF.Min(current + distance, target) :
MathF.Max(current - distance, target);
}

public static double MoveTowards(double current, double target, double distance)
{
return current < target ?
Math.Min(current + distance, target) :
Math.Max(current - distance, target);
}
}
23 changes: 0 additions & 23 deletions Hypercube.Mathematics/HyperMathF.cs

This file was deleted.

4 changes: 2 additions & 2 deletions Hypercube.Mathematics/Quaternion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,15 @@ public static Vector3 ToEuler(Quaternion quaternion)
// Singularity at north pole
return new Vector3(
0,
HyperMathF.PIOver2,
HyperMath.PIOver2F,
2f * MathF.Atan2(quaternion.X, quaternion.W)
);

if (singularityTest < -SingularityThreshold * unit)
// Singularity at south pole
return new Vector3(
0,
-HyperMathF.PIOver2,
-HyperMath.PIOver2F,
-2f * MathF.Atan2(quaternion.X, quaternion.W)
);

Expand Down
2 changes: 1 addition & 1 deletion Hypercube.Mathematics/Shapes/Circle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public readonly struct Circle(Vector2 position, float radius)
public readonly Vector2 Position = position;
public readonly float Radius = radius;

public float Area => Radius * Radius * HyperMathF.PI;
public float Area => Radius * Radius * HyperMath.PIf;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Circle operator +(Circle a, Vector2 b)
Expand Down
4 changes: 2 additions & 2 deletions Hypercube.Mathematics/Vectors/Vector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,8 @@ public static Vector2 Reflect(Vector2 value, Vector2 normal)
public static Vector2 MoveTowards(Vector2 current, Vector2 target, float distance)
{
return new Vector2(
HyperMathF.MoveTowards(current.X, target.X, distance),
HyperMathF.MoveTowards(current.Y, target.Y, distance));
HyperMath.MoveTowards(current.X, target.X, distance),
HyperMath.MoveTowards(current.Y, target.Y, distance));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
4 changes: 2 additions & 2 deletions Hypercube.Mathematics/Vectors/Vector2i.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ public static float Dot(Vector2i valueA, Vector2i valueB)
public static Vector2i MoveTowards(Vector2i current, Vector2i target, int distance)
{
return new Vector2i(
HyperMathF.MoveTowards(current.X, target.X, distance),
HyperMathF.MoveTowards(current.Y, target.Y, distance));
HyperMath.MoveTowards(current.X, target.X, distance),
HyperMath.MoveTowards(current.Y, target.Y, distance));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
6 changes: 3 additions & 3 deletions Hypercube.Mathematics/Vectors/Vector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,9 @@ public static Vector3 Reflect(Vector3 value, Vector3 normal)
public static Vector3 MoveTowards(Vector3 current, Vector3 target, float distance)
{
return new Vector3(
HyperMathF.MoveTowards(current.X, target.X, distance),
HyperMathF.MoveTowards(current.Y, target.Y, distance),
HyperMathF.MoveTowards(current.Z, target.Z, distance));
HyperMath.MoveTowards(current.X, target.X, distance),
HyperMath.MoveTowards(current.Y, target.Y, distance),
HyperMath.MoveTowards(current.Z, target.Z, distance));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
8 changes: 4 additions & 4 deletions Hypercube.Mathematics/Vectors/Vector4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -553,10 +553,10 @@ public static Vector4 Reflect(Vector4 value, Vector4 normal)
public static Vector4 MoveTowards(Vector4 current, Vector4 target, float distance)
{
return new Vector4(
HyperMathF.MoveTowards(current.X, target.X, distance),
HyperMathF.MoveTowards(current.Y, target.Y, distance),
HyperMathF.MoveTowards(current.Z, target.Z, distance),
HyperMathF.MoveTowards(current.W, target.W, distance));
HyperMath.MoveTowards(current.X, target.X, distance),
HyperMath.MoveTowards(current.Y, target.Y, distance),
HyperMath.MoveTowards(current.Z, target.Z, distance),
HyperMath.MoveTowards(current.W, target.W, distance));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down

0 comments on commit 139933b

Please sign in to comment.