From 1b811cb18d47a463fdcd35216ab1cb3630d5df73 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 10 Mar 2021 12:17:23 +0000 Subject: [PATCH] (#38) Add Single/Double.LerpFrom/To/With --- X10D/src/DoubleExtensions/DoubleExtensions.cs | 53 +++++++++++++++++++ X10D/src/SingleExtensions/SingleExtensions.cs | 45 ++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/X10D/src/DoubleExtensions/DoubleExtensions.cs b/X10D/src/DoubleExtensions/DoubleExtensions.cs index 94bcfa3..c859e58 100644 --- a/X10D/src/DoubleExtensions/DoubleExtensions.cs +++ b/X10D/src/DoubleExtensions/DoubleExtensions.cs @@ -53,6 +53,51 @@ namespace X10D.DoubleExtensions return !number.IsEven(); } + /// + /// Linearly interpolates to the current value from a specified source using a specified alpha. + /// + /// The interpolation target. + /// The interpolation source. + /// The interpolation alpha. + /// + /// The interpolation result as determined by (1 - ) * + + /// * . + /// + public static double LerpFrom(this double target, double value, double alpha) + { + return LerpInternal(value, target, alpha); + } + + /// + /// Linearly interpolates from the current value to a specified target using a specified alpha. + /// + /// The interpolation source. + /// The interpolation target. + /// The interpolation alpha. + /// + /// The interpolation result as determined by (1 - ) * + + /// * . + /// + public static double LerpTo(this double value, double target, double alpha) + { + return LerpInternal(value, target, alpha); + } + + /// + /// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value. + /// + /// The interpolation alpha. + /// The interpolation source. + /// The interpolation target. + /// + /// The interpolation result as determined by (1 - ) * + + /// * . + /// + public static double LerpWith(this double alpha, double value, double target) + { + return LerpInternal(value, target, alpha); + } + /// /// Converts an angle from radians to degrees. /// @@ -73,5 +118,13 @@ namespace X10D.DoubleExtensions { return Math.Round(v / nearest) * nearest; } + + private static double LerpInternal(double a, double b, double t) + { + // rookie mistake: a + t * (b - a) + + // "precise" method: (1 - t) * a + t * b + return (1 - t) * a + t * b; + } } } diff --git a/X10D/src/SingleExtensions/SingleExtensions.cs b/X10D/src/SingleExtensions/SingleExtensions.cs index 89be04f..45d092d 100644 --- a/X10D/src/SingleExtensions/SingleExtensions.cs +++ b/X10D/src/SingleExtensions/SingleExtensions.cs @@ -54,6 +54,51 @@ namespace X10D.SingleExtensions return !number.IsEven(); } + /// + /// Linearly interpolates to the current value from a specified source using a specified alpha. + /// + /// The interpolation target. + /// The interpolation source. + /// The interpolation alpha. + /// + /// The interpolation result as determined by (1 - ) * + + /// * . + /// + public static float LerpFrom(this float target, float value, float alpha) + { + return (float)((double)target).LerpFrom(value, alpha); + } + + /// + /// Linearly interpolates from the current value to a specified target using a specified alpha. + /// + /// The interpolation source. + /// The interpolation target. + /// The interpolation alpha. + /// + /// The interpolation result as determined by (1 - ) * + + /// * . + /// + public static float LerpTo(this float value, float target, float alpha) + { + return (float)((double)value).LerpTo(target, alpha); + } + + /// + /// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value. + /// + /// The interpolation alpha. + /// The interpolation source. + /// The interpolation target. + /// + /// The interpolation result as determined by (1 - ) * + + /// * . + /// + public static float LerpWith(this float alpha, float value, float target) + { + return (float)((double)alpha).LerpWith(value, target); + } + /// /// Converts an angle from radians to degrees. ///