From 17ecdb1cc718f103c58b4abdcb9bfd5c630c77ec Mon Sep 17 00:00:00 2001 From: ikpil Date: Thu, 30 May 2024 22:53:01 +0900 Subject: [PATCH] added RcMath.Lerp() --- src/DotRecast.Core/RcMath.cs | 6 ++++ test/DotRecast.Core.Test/RcMathTest.cs | 38 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 test/DotRecast.Core.Test/RcMathTest.cs diff --git a/src/DotRecast.Core/RcMath.cs b/src/DotRecast.Core/RcMath.cs index 0b407da..b981bd1 100644 --- a/src/DotRecast.Core/RcMath.cs +++ b/src/DotRecast.Core/RcMath.cs @@ -29,5 +29,11 @@ namespace DotRecast.Core { return f * f; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static float Lerp(float value1, float value2, float amount) + { + return (value1 * (1.0f - amount)) + (value2 * amount); + } } } \ No newline at end of file diff --git a/test/DotRecast.Core.Test/RcMathTest.cs b/test/DotRecast.Core.Test/RcMathTest.cs new file mode 100644 index 0000000..9bdda89 --- /dev/null +++ b/test/DotRecast.Core.Test/RcMathTest.cs @@ -0,0 +1,38 @@ +using System; +using NUnit.Framework; + +namespace DotRecast.Core.Test; + +public class RcMathTest +{ + [Test] + public void TestSqr() + { + Assert.That(RcMath.Sqr(0), Is.EqualTo(0)); + Assert.That(RcMath.Sqr(5), Is.EqualTo(25)); + Assert.That(RcMath.Sqr(-5), Is.EqualTo(25)); + Assert.That(RcMath.Sqr(float.PositiveInfinity), Is.EqualTo(float.PositiveInfinity)); + Assert.That(RcMath.Sqr(float.NegativeInfinity), Is.EqualTo(float.PositiveInfinity)); + Assert.That(RcMath.Sqr(float.NaN), Is.EqualTo(float.NaN)); + } + + [Test] + public void TestLerp() + { + // + Assert.That(RcMath.Lerp(-10, 10, 2f), Is.EqualTo(30)); + Assert.That(RcMath.Lerp(-10, 10, 1f), Is.EqualTo(10)); + Assert.That(RcMath.Lerp(-10, 10, 0.5f), Is.EqualTo(0)); + Assert.That(RcMath.Lerp(-10, 10, 0.25f), Is.EqualTo(-5)); + Assert.That(RcMath.Lerp(-10, 10, 0), Is.EqualTo(-10)); + Assert.That(RcMath.Lerp(-10, 10, -0.5f), Is.EqualTo(-20)); + Assert.That(RcMath.Lerp(-10, 10, -1f), Is.EqualTo(-30)); + + // + Assert.That(RcMath.Lerp(10, 10, 0.5f), Is.EqualTo(10)); + Assert.That(RcMath.Lerp(10, 10, 0.8f), Is.EqualTo(10)); + + // + Assert.That(RcMath.Lerp(10, -10, 0.75f), Is.EqualTo(-5)); + } +} \ No newline at end of file