add double implementations

master
Evan Hemsley 2019-10-29 17:30:34 -07:00
parent fbdeea55fb
commit 7ab340380e
2 changed files with 111 additions and 0 deletions

View File

@ -4,14 +4,29 @@ namespace MoonTools.Core.Easing
{ {
public static class Easing public static class Easing
{ {
// argument checker
private static void CheckTime(float time, float duration) private static void CheckTime(float time, float duration)
{ {
if (time > duration) { throw new ArgumentException($"{time} is invalid. Must be less than {duration}."); } if (time > duration) { throw new ArgumentException($"{time} is invalid. Must be less than {duration}."); }
} }
private static void CheckTime(double time, double duration)
{
if (time > duration) { throw new ArgumentException($"{time} is invalid. Must be less than {duration}."); }
}
// function transformations
private static float NormalizedTime(Func<float, float, float, float, float> easingFunction, float t) => easingFunction(t, 0, 1, 1); private static float NormalizedTime(Func<float, float, float, float, float> easingFunction, float t) => easingFunction(t, 0, 1, 1);
private static float TimeRange(Func<float, float> easingFunction, float time, float start, float end) => start + (end - start) * easingFunction((time - start) / (end - start)); private static float TimeRange(Func<float, float> easingFunction, float time, float start, float end) => start + (end - start) * easingFunction((time - start) / (end - start));
private static double NormalizedTime(Func<double, double, double, double, double> easingFunction, double t) => easingFunction(t, 0, 1, 1);
private static double TimeRange(Func<double, double> easingFunction, double time, double start, double end) => start + (end - start) * easingFunction((time - start) / (end - start));
/********* EASING FUNCTIONS ********/
// LINEAR
public static float Linear(float t) => NormalizedTime(Linear, t); public static float Linear(float t) => NormalizedTime(Linear, t);
public static float Linear(float time, float start, float end) => TimeRange(Linear, time, start, end); public static float Linear(float time, float start, float end) => TimeRange(Linear, time, start, end);
@ -21,6 +36,17 @@ namespace MoonTools.Core.Easing
return c * t / d + b; return c * t / d + b;
} }
public static double Linear(double t) => NormalizedTime(Linear, t);
public static double Linear(double time, double start, double end) => TimeRange(Linear, time, start, end);
public static double Linear(double t, double b, double c, double d)
{
CheckTime(t, d);
return c * t / d + b;
}
// IN QUAD
public static float InQuad(float t) => NormalizedTime(InQuad, t); public static float InQuad(float t) => NormalizedTime(InQuad, t);
public static float InQuad(float time, float start, float end) => TimeRange(InQuad, time, start, end); public static float InQuad(float time, float start, float end) => TimeRange(InQuad, time, start, end);
@ -31,6 +57,18 @@ namespace MoonTools.Core.Easing
return c * (t * t) + b; return c * (t * t) + b;
} }
public static double InQuad(double t) => NormalizedTime(InQuad, t);
public static double InQuad(double time, double start, double end) => TimeRange(InQuad, time, start, end);
public static double InQuad(double t, double b, double c, double d)
{
CheckTime(t, d);
t = t / d;
return c * (t * t) + b;
}
// OUT QUAD
public static float OutQuad(float t) => NormalizedTime(OutQuad, t); public static float OutQuad(float t) => NormalizedTime(OutQuad, t);
public static float OutQuad(float time, float start, float end) => TimeRange(OutQuad, time, start, end); public static float OutQuad(float time, float start, float end) => TimeRange(OutQuad, time, start, end);
@ -41,6 +79,18 @@ namespace MoonTools.Core.Easing
return -c * t * (t - 2) + b; return -c * t * (t - 2) + b;
} }
public static double OutQuad(double t) => NormalizedTime(OutQuad, t);
public static double OutQuad(double time, double start, double end) => TimeRange(OutQuad, time, start, end);
public static double OutQuad(double t, double b, double c, double d)
{
CheckTime(t, d);
t = t / d;
return -c * t * (t - 2) + b;
}
// IN OUT QUAD
public static float InOutQuad(float t) => NormalizedTime(InOutQuad, t); public static float InOutQuad(float t) => NormalizedTime(InOutQuad, t);
public static float InOutQuad(float time, float start, float end) => TimeRange(InOutQuad, time, start, end); public static float InOutQuad(float time, float start, float end) => TimeRange(InOutQuad, time, start, end);
@ -57,5 +107,22 @@ namespace MoonTools.Core.Easing
return -c / 2 * ((t - 1) * (t - 3) - 1) + b; return -c / 2 * ((t - 1) * (t - 3) - 1) + b;
} }
} }
public static double InOutQuad(double t) => NormalizedTime(InOutQuad, t);
public static double InOutQuad(double time, double start, double end) => TimeRange(InOutQuad, time, start, end);
public static double InOutQuad(double t, double b, double c, double d)
{
CheckTime(t, d);
t = t / d * 2;
if (t < 1)
{
return c / 2 * (t * t) + b;
}
else
{
return -c / 2 * ((t - 1) * (t - 3) - 1) + b;
}
}
} }
} }

View File

@ -18,12 +18,23 @@ namespace Test
Action invalidTime = () => Easing.Linear(1.5f); Action invalidTime = () => Easing.Linear(1.5f);
invalidTime.Should().Throw<ArgumentException>(); invalidTime.Should().Throw<ArgumentException>();
Easing.Linear(0.25).Should().Be(0.25);
Easing.Linear(0.5).Should().Be(0.5);
Easing.Linear(0.75).Should().Be(0.75);
invalidTime = () => Easing.Linear(1.5);
invalidTime.Should().Throw<ArgumentException>();
Easing.Linear(3, 2, 6).Should().Be(3f); Easing.Linear(3, 2, 6).Should().Be(3f);
Easing.Linear(4, 2, 6).Should().Be(4f); Easing.Linear(4, 2, 6).Should().Be(4f);
Easing.Linear(5, 2, 6).Should().Be(5f); Easing.Linear(5, 2, 6).Should().Be(5f);
invalidTime = () => Easing.Linear(7, 2, 6); invalidTime = () => Easing.Linear(7, 2, 6);
invalidTime.Should().Throw<ArgumentException>(); invalidTime.Should().Throw<ArgumentException>();
Easing.Linear(3, 2, 6).Should().Be(3);
Easing.Linear(4, 2, 6).Should().Be(4);
Easing.Linear(5, 2, 6).Should().Be(5);
} }
[Test] [Test]
@ -36,12 +47,23 @@ namespace Test
Action invalidTime = () => Easing.InQuad(1.5f); Action invalidTime = () => Easing.InQuad(1.5f);
invalidTime.Should().Throw<ArgumentException>(); invalidTime.Should().Throw<ArgumentException>();
Easing.InQuad(0.25).Should().Be(0.0625);
Easing.InQuad(0.5).Should().Be(0.25);
Easing.InQuad(0.75).Should().Be(0.5625);
invalidTime = () => Easing.InQuad(1.5);
invalidTime.Should().Throw<ArgumentException>();
Easing.InQuad(3, 2, 6).Should().Be(2.25f); Easing.InQuad(3, 2, 6).Should().Be(2.25f);
Easing.InQuad(4, 2, 6).Should().Be(3f); Easing.InQuad(4, 2, 6).Should().Be(3f);
Easing.InQuad(5, 2, 6).Should().Be(4.25f); Easing.InQuad(5, 2, 6).Should().Be(4.25f);
invalidTime = () => Easing.InQuad(7, 2, 6); invalidTime = () => Easing.InQuad(7, 2, 6);
invalidTime.Should().Throw<ArgumentException>(); invalidTime.Should().Throw<ArgumentException>();
Easing.InQuad(3.0, 2, 6).Should().Be(2.25);
Easing.InQuad(4.0, 2, 6).Should().Be(3);
Easing.InQuad(5.0, 2, 6).Should().Be(4.25);
} }
[Test] [Test]
@ -54,12 +76,23 @@ namespace Test
Action invalidTime = () => Easing.OutQuad(1.5f); Action invalidTime = () => Easing.OutQuad(1.5f);
invalidTime.Should().Throw<ArgumentException>(); invalidTime.Should().Throw<ArgumentException>();
Easing.OutQuad(0.25).Should().Be(0.4375);
Easing.OutQuad(0.5).Should().Be(0.75);
Easing.OutQuad(0.75).Should().Be(0.9375);
invalidTime = () => Easing.OutQuad(1.5);
invalidTime.Should().Throw<ArgumentException>();
Easing.OutQuad(3, 2, 6).Should().Be(3.75f); Easing.OutQuad(3, 2, 6).Should().Be(3.75f);
Easing.OutQuad(4, 2, 6).Should().Be(5f); Easing.OutQuad(4, 2, 6).Should().Be(5f);
Easing.OutQuad(5, 2, 6).Should().Be(5.75f); Easing.OutQuad(5, 2, 6).Should().Be(5.75f);
invalidTime = () => Easing.OutQuad(7, 2, 6); invalidTime = () => Easing.OutQuad(7, 2, 6);
invalidTime.Should().Throw<ArgumentException>(); invalidTime.Should().Throw<ArgumentException>();
Easing.OutQuad(3.0, 2, 6).Should().Be(3.75);
Easing.OutQuad(4.0, 2, 6).Should().Be(5);
Easing.OutQuad(5.0, 2, 6).Should().Be(5.75);
} }
[Test] [Test]
@ -72,12 +105,23 @@ namespace Test
Action invalidTime = () => Easing.InOutQuad(1.5f); Action invalidTime = () => Easing.InOutQuad(1.5f);
invalidTime.Should().Throw<ArgumentException>(); invalidTime.Should().Throw<ArgumentException>();
Easing.InOutQuad(0.25).Should().Be(0.125);
Easing.InOutQuad(0.5).Should().Be(0.5);
Easing.InOutQuad(0.75).Should().Be(0.875);
invalidTime = () => Easing.InOutQuad(1.5);
invalidTime.Should().Throw<ArgumentException>();
Easing.InOutQuad(3, 2, 6).Should().Be(2.5f); Easing.InOutQuad(3, 2, 6).Should().Be(2.5f);
Easing.InOutQuad(4, 2, 6).Should().Be(4f); Easing.InOutQuad(4, 2, 6).Should().Be(4f);
Easing.InOutQuad(5, 2, 6).Should().Be(5.5f); Easing.InOutQuad(5, 2, 6).Should().Be(5.5f);
invalidTime = () => Easing.InOutQuad(7, 2, 6); invalidTime = () => Easing.InOutQuad(7, 2, 6);
invalidTime.Should().Throw<ArgumentException>(); invalidTime.Should().Throw<ArgumentException>();
Easing.InOutQuad(3.0, 2, 6).Should().Be(2.5);
Easing.InOutQuad(4.0, 2, 6).Should().Be(4);
Easing.InOutQuad(5.0, 2, 6).Should().Be(5.5);
} }
} }
} }