diff --git a/X10D.Tests/src/Math/MathUtilityTests.cs b/X10D.Tests/src/Math/MathUtilityTests.cs index f58835c..a40f18c 100644 --- a/X10D.Tests/src/Math/MathUtilityTests.cs +++ b/X10D.Tests/src/Math/MathUtilityTests.cs @@ -163,6 +163,78 @@ public class MathUtilityTests Assert.AreEqual(1.0f, floatResult); } + [TestMethod] + public void Pulse_ShouldReturn1_GivenDoubleValueWithinBounds() + { + const double value = 0.5; + const double lower = 0.0; + const double upper = 1.0; + + double result = MathUtility.Pulse(value, lower, upper); + + Assert.AreEqual(1.0, result, 1e-6); + } + + [TestMethod] + public void Pulse_ShouldReturn0_GivenDoubleValueLessThanLowerBound() + { + const double value = -1.0; + const double lower = 0.0; + const double upper = 1.0; + + double result = MathUtility.Pulse(value, lower, upper); + + Assert.AreEqual(0.0, result, 1e-6); + } + + [TestMethod] + public void Pulse_ShouldReturn0_GivenDoubleValueGreaterThanUpperBound() + { + const double value = 2.0; + const double lower = 0.0; + const double upper = 1.0; + + double result = MathUtility.Pulse(value, lower, upper); + + Assert.AreEqual(0.0, result, 1e-6); + } + + [TestMethod] + public void Pulse_ShouldReturn1_GivenSingleValueWithinBounds() + { + const float value = 0.5f; + const float lower = 0.0f; + const float upper = 1.0f; + + float result = MathUtility.Pulse(value, lower, upper); + + Assert.AreEqual(1.0f, result, 1e-6f); + } + + [TestMethod] + public void Pulse_ShouldReturn0_GivenSingleValueLessThanLowerBound() + { + const float value = -1.0f; + const float lower = 0.0f; + const float upper = 1.0f; + + float result = MathUtility.Pulse(value, lower, upper); + + Assert.AreEqual(0.0f, result, 1e-6f); + } + + [TestMethod] + public void Pulse_ShouldReturn0_GivenSingleValueGreaterThanUpperBound() + { + const float value = 2.0f; + const float lower = 0.0f; + const float upper = 1.0f; + + float result = MathUtility.Pulse(value, lower, upper); + + Assert.AreEqual(0.0f, result, 1e-6f); + } + [TestMethod] public void Sawtooth_ShouldReturn0Point5_Given0Point5AsDouble() { diff --git a/X10D/src/Math/MathUtility.cs b/X10D/src/Math/MathUtility.cs index 18d0009..890acac 100644 --- a/X10D/src/Math/MathUtility.cs +++ b/X10D/src/Math/MathUtility.cs @@ -287,6 +287,54 @@ public static class MathUtility return System.Math.Pow(value, 1.0 / gamma); } + /// + /// Returns the pulse wave for a given value. + /// + /// The value to calculate. + /// The inclusive lower bound of the pulse. + /// The inclusive upper bound of the pulse. + /// + /// 1 if lies between and ; + /// otherwise, 0. + /// + public static float Pulse(float value, float lowerBound, float upperBound) + { + bool result = lowerBound <= value && value <= upperBound; +#if NET6_0_OR_GREATER + return Unsafe.As(ref result); +#else + unsafe + { + var pResult = (int*)&result; + return *pResult; + } +#endif + } + + /// + /// Returns the pulse wave for a given value. + /// + /// The value to calculate. + /// The inclusive lower bound of the pulse. + /// The inclusive upper bound of the pulse. + /// + /// 1 if lies between and ; + /// otherwise, 0. + /// + public static double Pulse(double value, double lowerBound, double upperBound) + { + bool result = lowerBound <= value && value <= upperBound; +#if NET6_0_OR_GREATER + return Unsafe.As(ref result); +#else + unsafe + { + var pResult = (int*)&result; + return *pResult; + } +#endif + } + /// /// Returns the incremental sawtooth wave of a given value. ///