From 2d8efa0154844710378f8fcecbb73b88586be85e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Thu, 2 Sep 2021 23:34:42 +0200 Subject: [PATCH] Added MathHelper - common Values of Pi - Degree <-> Radians conversions --- GlitchyEngine.Test/src/Math/MathHelperTest.bf | 64 ++++++++++++++++++ GlitchyEngine/src/Math/MathHelper.bf | 65 +++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 GlitchyEngine.Test/src/Math/MathHelperTest.bf create mode 100644 GlitchyEngine/src/Math/MathHelper.bf diff --git a/GlitchyEngine.Test/src/Math/MathHelperTest.bf b/GlitchyEngine.Test/src/Math/MathHelperTest.bf new file mode 100644 index 0000000..b7208a7 --- /dev/null +++ b/GlitchyEngine.Test/src/Math/MathHelperTest.bf @@ -0,0 +1,64 @@ +using System; +using GlitchyEngine.Math; + +namespace GlitchyEngine.Test.Math +{ + static class MathHelperTest + { + [Test] + public static void TestDegToRad() + { + { + float deg = 0; + float rad = MathHelper.ToRadians(deg); + Test.Assert(rad == 0.0f); + } + + { + float deg = 45; + float rad = MathHelper.ToRadians(deg); + Test.Assert(rad == MathHelper.PiOverFour); + } + + { + float deg = 180; + float rad = MathHelper.ToRadians(deg); + Test.Assert(rad == MathHelper.Pi); + } + + { + float deg = -360; + float rad = MathHelper.ToRadians(deg); + Test.Assert(rad == -MathHelper.TwoPi); + } + } + + [Test] + public static void TestRadToDeg() + { + { + float rad = 0; + float deg = MathHelper.ToDegrees(rad); + Test.Assert(deg == 0.0f); + } + + { + float rad = MathHelper.PiOverFour; + float deg = MathHelper.ToDegrees(rad); + Test.Assert(deg == 45); + } + + { + float rad = MathHelper.Pi; + float deg = MathHelper.ToDegrees(rad); + Test.Assert(deg == 180); + } + + { + float rad = -MathHelper.TwoPi; + float deg = MathHelper.ToDegrees(rad); + Test.Assert(deg == -360); + } + } + } +} diff --git a/GlitchyEngine/src/Math/MathHelper.bf b/GlitchyEngine/src/Math/MathHelper.bf new file mode 100644 index 0000000..05bcb64 --- /dev/null +++ b/GlitchyEngine/src/Math/MathHelper.bf @@ -0,0 +1,65 @@ +namespace GlitchyEngine.Math +{ + public static class MathHelper + { + public const float Pi = 3.14159265359f; + public const float TwoPi = 6.28318530718f; + public const float PiOverTwo = 1.57079632679f; + public const float PiOverThree = 1.0471975511965f; + public const float PiOverFour = 0.7853981633974f; + + /// Converts radians to degrees + const float RadToDeg = 180.0f / Pi; + + /// Converts radians to degrees + const float DegToRad = Pi / 180.0f; + + // Converts the given radians to degrees + public static float ToDegrees(float radians) + { + return radians * RadToDeg; + } + + // Converts the given radians to degrees + public static Vector2 ToDegrees(Vector2 radians) + { + return radians * RadToDeg; + } + + // Converts the given radians to degrees + public static Vector3 ToDegrees(Vector3 radians) + { + return radians * RadToDeg; + } + + // Converts the given radians to degrees + public static Vector4 ToDegrees(Vector4 radians) + { + return radians * RadToDeg; + } + + // Converts the given degrees to radians + public static float ToRadians(float degrees) + { + return degrees * DegToRad; + } + + // Converts the given degrees to radians + public static Vector2 ToRadians(Vector2 degrees) + { + return degrees * DegToRad; + } + + // Converts the given degrees to radians + public static Vector3 ToRadians(Vector3 degrees) + { + return degrees * DegToRad; + } + + // Converts the given degrees to radians + public static Vector4 ToRadians(Vector4 degrees) + { + return degrees * DegToRad; + } + } +}