mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-10 03:45:41 +00:00
(#38) Add Single/Double.LerpFrom/To/With
This commit is contained in:
parent
5b46523c51
commit
1b811cb18d
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user