From 688058a7aa7292d1b18f057754bd78faae2c68a2 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 11 Mar 2021 16:45:18 +0000 Subject: [PATCH] (#39) Explictly implement Lerp --- X10D/src/SingleExtensions/SingleExtensions.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/X10D/src/SingleExtensions/SingleExtensions.cs b/X10D/src/SingleExtensions/SingleExtensions.cs index 308c940..9529f2b 100644 --- a/X10D/src/SingleExtensions/SingleExtensions.cs +++ b/X10D/src/SingleExtensions/SingleExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using X10D.DoubleExtensions; namespace X10D.SingleExtensions @@ -15,7 +16,7 @@ namespace X10D.SingleExtensions /// The result of π * / 180. public static float DegreesToRadians(this float value) { - return (float)((double)value).DegreesToRadians(); + return (float)Math.PI * value / 180.0f; } /// @@ -66,7 +67,7 @@ namespace X10D.SingleExtensions /// public static float LerpFrom(this float target, float value, float alpha) { - return (float)((double)target).LerpFrom(value, alpha); + return LerpInternal(value, target, alpha); } /// @@ -81,7 +82,7 @@ namespace X10D.SingleExtensions /// public static float LerpTo(this float value, float target, float alpha) { - return (float)((double)value).LerpTo(target, alpha); + return LerpInternal(value, target, alpha); } /// @@ -96,7 +97,7 @@ namespace X10D.SingleExtensions /// public static float LerpWith(this float alpha, float value, float target) { - return (float)((double)alpha).LerpWith(value, target); + return LerpInternal(value, target, alpha); } /// @@ -106,7 +107,7 @@ namespace X10D.SingleExtensions /// The result of π * / 180. public static float RadiansToDegrees(this float value) { - return (float)((double)value).RadiansToDegrees(); + return (float)Math.PI * value / 180.0f; } /// @@ -129,5 +130,13 @@ namespace X10D.SingleExtensions { return (float)((double)value).Round(nearest); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static float LerpInternal(float a, float b, float t) + { + // rookie mistake: a + t * (b - a) + // "precise" method: (1 - t) * a + t * b + return (1.0f - t) * a + t * b; + } } }