(#39) Explictly implement Lerp

This commit is contained in:
Oliver Booth 2021-03-11 16:45:18 +00:00
parent ae9306cf93
commit 688058a7aa
1 changed files with 14 additions and 5 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using X10D.DoubleExtensions;
namespace X10D.SingleExtensions
@ -15,7 +16,7 @@ namespace X10D.SingleExtensions
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
public static float DegreesToRadians(this float value)
{
return (float)((double)value).DegreesToRadians();
return (float)Math.PI * value / 180.0f;
}
/// <summary>
@ -66,7 +67,7 @@ namespace X10D.SingleExtensions
/// </returns>
public static float LerpFrom(this float target, float value, float alpha)
{
return (float)((double)target).LerpFrom(value, alpha);
return LerpInternal(value, target, alpha);
}
/// <summary>
@ -81,7 +82,7 @@ namespace X10D.SingleExtensions
/// </returns>
public static float LerpTo(this float value, float target, float alpha)
{
return (float)((double)value).LerpTo(target, alpha);
return LerpInternal(value, target, alpha);
}
/// <summary>
@ -96,7 +97,7 @@ namespace X10D.SingleExtensions
/// </returns>
public static float LerpWith(this float alpha, float value, float target)
{
return (float)((double)alpha).LerpWith(value, target);
return LerpInternal(value, target, alpha);
}
/// <summary>
@ -106,7 +107,7 @@ namespace X10D.SingleExtensions
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
public static float RadiansToDegrees(this float value)
{
return (float)((double)value).RadiansToDegrees();
return (float)Math.PI * value / 180.0f;
}
/// <summary>
@ -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;
}
}
}