From 7ab340380e45d95ab7e2ebd73f1c03051d84e8b9 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Tue, 29 Oct 2019 17:30:34 -0700 Subject: [PATCH] add double implementations --- Easing/Easing.cs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ Test/Easing.cs | 44 +++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) diff --git a/Easing/Easing.cs b/Easing/Easing.cs index 67fb8cd..66c2c50 100644 --- a/Easing/Easing.cs +++ b/Easing/Easing.cs @@ -4,14 +4,29 @@ namespace MoonTools.Core.Easing { public static class Easing { + // argument checker + private static void CheckTime(float time, float 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 easingFunction, float t) => easingFunction(t, 0, 1, 1); private static float TimeRange(Func easingFunction, float time, float start, float end) => start + (end - start) * easingFunction((time - start) / (end - start)); + private static double NormalizedTime(Func easingFunction, double t) => easingFunction(t, 0, 1, 1); + private static double TimeRange(Func 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 time, float start, float end) => TimeRange(Linear, time, start, end); @@ -21,6 +36,17 @@ namespace MoonTools.Core.Easing 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 time, float start, float end) => TimeRange(InQuad, time, start, end); @@ -31,6 +57,18 @@ namespace MoonTools.Core.Easing 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 time, float start, float end) => TimeRange(OutQuad, time, start, end); @@ -41,6 +79,18 @@ namespace MoonTools.Core.Easing 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 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; } } + + 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; + } + } } } diff --git a/Test/Easing.cs b/Test/Easing.cs index 38dd930..c9216fb 100644 --- a/Test/Easing.cs +++ b/Test/Easing.cs @@ -18,12 +18,23 @@ namespace Test Action invalidTime = () => Easing.Linear(1.5f); invalidTime.Should().Throw(); + 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(); + Easing.Linear(3, 2, 6).Should().Be(3f); Easing.Linear(4, 2, 6).Should().Be(4f); Easing.Linear(5, 2, 6).Should().Be(5f); invalidTime = () => Easing.Linear(7, 2, 6); invalidTime.Should().Throw(); + + 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] @@ -36,12 +47,23 @@ namespace Test Action invalidTime = () => Easing.InQuad(1.5f); invalidTime.Should().Throw(); + 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(); + Easing.InQuad(3, 2, 6).Should().Be(2.25f); Easing.InQuad(4, 2, 6).Should().Be(3f); Easing.InQuad(5, 2, 6).Should().Be(4.25f); invalidTime = () => Easing.InQuad(7, 2, 6); invalidTime.Should().Throw(); + + 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] @@ -54,12 +76,23 @@ namespace Test Action invalidTime = () => Easing.OutQuad(1.5f); invalidTime.Should().Throw(); + 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(); + Easing.OutQuad(3, 2, 6).Should().Be(3.75f); Easing.OutQuad(4, 2, 6).Should().Be(5f); Easing.OutQuad(5, 2, 6).Should().Be(5.75f); invalidTime = () => Easing.OutQuad(7, 2, 6); invalidTime.Should().Throw(); + + 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] @@ -72,12 +105,23 @@ namespace Test Action invalidTime = () => Easing.InOutQuad(1.5f); invalidTime.Should().Throw(); + 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(); + Easing.InOutQuad(3, 2, 6).Should().Be(2.5f); Easing.InOutQuad(4, 2, 6).Should().Be(4f); Easing.InOutQuad(5, 2, 6).Should().Be(5.5f); invalidTime = () => Easing.InOutQuad(7, 2, 6); invalidTime.Should().Throw(); + + 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); } } } \ No newline at end of file