Move Lerp implementation to MathUtils class

float/double/short/int/long methods call MathUtils class
This commit is contained in:
Oliver Booth 2021-07-20 16:56:56 +01:00
parent ac203dda1f
commit 78830a3b9e
No known key found for this signature in database
GPG Key ID: A4AC17007530E9B4
6 changed files with 159 additions and 163 deletions

View File

@ -1,5 +1,4 @@
using System;
using System.Runtime.CompilerServices;
namespace X10D
{
@ -61,12 +60,11 @@ namespace X10D
/// <param name="value">The interpolation source.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static double LerpFrom(this double target, double value, double alpha)
{
return LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
@ -76,12 +74,11 @@ namespace X10D
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static double LerpTo(this double value, double target, double alpha)
{
return LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
@ -91,12 +88,11 @@ namespace X10D
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static double LerpWith(this double alpha, double value, double target)
{
return LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
@ -141,13 +137,5 @@ namespace X10D
{
return value != 0.0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static double LerpInternal(double a, double b, double t)
{
// rookie mistake: a + t * (b - a)
// "precise" method: (1 - t) * a + t * b
return (1.0 - t) * a + t * b;
}
}
}

View File

@ -89,42 +89,11 @@ namespace X10D
/// <param name="value">The interpolation source.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static double LerpFrom(this short target, double value, double alpha)
{
return DoubleExtensions.LerpInternal(value, target, alpha);
}
/// <summary>
/// Linearly interpolates from the current value to a specified target using a specified alpha.
/// </summary>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// </returns>
public static double LerpTo(this short value, double target, double alpha)
{
return DoubleExtensions.LerpInternal(value, target, alpha);
}
/// <summary>
/// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value.
/// </summary>
/// <param name="alpha">The interpolation alpha.</param>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// </returns>
public static double LerpWith(this short alpha, double value, double target)
{
return DoubleExtensions.LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
@ -134,12 +103,11 @@ namespace X10D
/// <param name="value">The interpolation source.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static float LerpFrom(this short target, float value, float alpha)
{
return SingleExtensions.LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
@ -149,12 +117,25 @@ namespace X10D
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static double LerpTo(this short value, double target, double alpha)
{
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
/// Linearly interpolates from the current value to a specified target using a specified alpha.
/// </summary>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static float LerpTo(this short value, float target, float alpha)
{
return SingleExtensions.LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
@ -164,12 +145,25 @@ namespace X10D
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static double LerpWith(this short alpha, double value, double target)
{
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
/// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value.
/// </summary>
/// <param name="alpha">The interpolation alpha.</param>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static float LerpWith(this short alpha, float value, float target)
{
return SingleExtensions.LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>

View File

@ -89,42 +89,11 @@ namespace X10D
/// <param name="value">The interpolation source.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static double LerpFrom(this int target, double value, double alpha)
{
return DoubleExtensions.LerpInternal(value, target, alpha);
}
/// <summary>
/// Linearly interpolates from the current value to a specified target using a specified alpha.
/// </summary>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// </returns>
public static double LerpTo(this int value, double target, double alpha)
{
return DoubleExtensions.LerpInternal(value, target, alpha);
}
/// <summary>
/// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value.
/// </summary>
/// <param name="alpha">The interpolation alpha.</param>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// </returns>
public static double LerpWith(this int alpha, double value, double target)
{
return DoubleExtensions.LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
@ -134,12 +103,11 @@ namespace X10D
/// <param name="value">The interpolation source.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static float LerpFrom(this int target, float value, float alpha)
{
return SingleExtensions.LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
@ -149,12 +117,25 @@ namespace X10D
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static double LerpTo(this int value, double target, double alpha)
{
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
/// Linearly interpolates from the current value to a specified target using a specified alpha.
/// </summary>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static float LerpTo(this int value, float target, float alpha)
{
return SingleExtensions.LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
@ -164,12 +145,25 @@ namespace X10D
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static double LerpWith(this int alpha, double value, double target)
{
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
/// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value.
/// </summary>
/// <param name="alpha">The interpolation alpha.</param>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static float LerpWith(this int alpha, float value, float target)
{
return SingleExtensions.LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>

View File

@ -114,42 +114,11 @@ namespace X10D
/// <param name="value">The interpolation source.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static double LerpFrom(this long target, double value, double alpha)
{
return DoubleExtensions.LerpInternal(value, target, alpha);
}
/// <summary>
/// Linearly interpolates from the current value to a specified target using a specified alpha.
/// </summary>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// </returns>
public static double LerpTo(this long value, double target, double alpha)
{
return DoubleExtensions.LerpInternal(value, target, alpha);
}
/// <summary>
/// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value.
/// </summary>
/// <param name="alpha">The interpolation alpha.</param>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// </returns>
public static double LerpWith(this long alpha, double value, double target)
{
return DoubleExtensions.LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
@ -159,12 +128,11 @@ namespace X10D
/// <param name="value">The interpolation source.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static float LerpFrom(this long target, float value, float alpha)
{
return SingleExtensions.LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
@ -174,12 +142,25 @@ namespace X10D
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static double LerpTo(this long value, double target, double alpha)
{
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
/// Linearly interpolates from the current value to a specified target using a specified alpha.
/// </summary>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static float LerpTo(this long value, float target, float alpha)
{
return SingleExtensions.LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
@ -189,12 +170,25 @@ namespace X10D
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static double LerpWith(this long alpha, double value, double target)
{
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
/// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value.
/// </summary>
/// <param name="alpha">The interpolation alpha.</param>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static float LerpWith(this long alpha, float value, float target)
{
return SingleExtensions.LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>

38
X10D/src/MathUtils.cs Normal file
View File

@ -0,0 +1,38 @@
namespace X10D
{
/// <summary>
/// Provides static helpers methods for mathematical functions not found in the .NET <see cref="System.Math" /> class.
/// </summary>
public static class MathUtils
{
/// <summary>
/// Linearly interpolates from one value to a target using a specified alpha.
/// </summary>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static float Lerp(float value, float target, float alpha)
{
return (float)Lerp(value * 1.0, target, alpha);
}
/// <summary>
/// Linearly interpolates from one value to a target using a specified alpha.
/// </summary>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static double Lerp(double value, double target, double alpha)
{
// rookie mistake: a + t * (b - a)
// "precise" method: (1 - t) * a + t * b
return ((1.0 - alpha) * value) + (alpha * target);
}
}
}

View File

@ -1,5 +1,4 @@
using System;
using System.Runtime.CompilerServices;
using System;
namespace X10D
{
@ -61,12 +60,11 @@ namespace X10D
/// <param name="value">The interpolation source.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static float LerpFrom(this float target, float value, float alpha)
{
return LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
@ -76,12 +74,11 @@ namespace X10D
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static float LerpTo(this float value, float target, float alpha)
{
return LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
@ -91,12 +88,11 @@ namespace X10D
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <returns>
/// The interpolation result as determined by <c>(1 - <paramref name="alpha" />) * <paramref name="value" /> +
/// <paramref name="alpha" /> * <paramref name="target" /></c>.
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
public static float LerpWith(this float alpha, float value, float target)
{
return LerpInternal(value, target, alpha);
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
@ -141,13 +137,5 @@ namespace X10D
{
return value != 0.0f;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal 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;
}
}
}