1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-10 03:05:42 +00:00

(#38) Add Single/Double.LerpFrom/To/With

This commit is contained in:
Oliver Booth 2021-03-10 12:17:23 +00:00
parent 5b46523c51
commit 1b811cb18d
2 changed files with 98 additions and 0 deletions

View File

@ -53,6 +53,51 @@ namespace X10D.DoubleExtensions
return !number.IsEven();
}
/// <summary>
/// Linearly interpolates to the current value from a specified source using a specified alpha.
/// </summary>
/// <param name="target">The interpolation target.</param>
/// <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>.
/// </returns>
public static double LerpFrom(this double target, double value, double alpha)
{
return 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 double value, double target, double alpha)
{
return 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 double alpha, double value, double target)
{
return LerpInternal(value, target, alpha);
}
/// <summary>
/// Converts an angle from radians to degrees.
/// </summary>
@ -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;
}
}
}

View File

@ -54,6 +54,51 @@ namespace X10D.SingleExtensions
return !number.IsEven();
}
/// <summary>
/// Linearly interpolates to the current value from a specified source using a specified alpha.
/// </summary>
/// <param name="target">The interpolation target.</param>
/// <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>.
/// </returns>
public static float LerpFrom(this float target, float value, float alpha)
{
return (float)((double)target).LerpFrom(value, 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 float LerpTo(this float value, float target, float alpha)
{
return (float)((double)value).LerpTo(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 float LerpWith(this float alpha, float value, float target)
{
return (float)((double)alpha).LerpWith(value, target);
}
/// <summary>
/// Converts an angle from radians to degrees.
/// </summary>