From 328a88faddc67d5758a7b4132eccf4b28e235e17 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Wed, 30 Oct 2019 20:14:08 -0700 Subject: [PATCH] bounce functions --- Easing/Easing.cs | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ Test/Easing.cs | 56 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/Easing/Easing.cs b/Easing/Easing.cs index 96718a4..4da3304 100644 --- a/Easing/Easing.cs +++ b/Easing/Easing.cs @@ -822,5 +822,70 @@ namespace MoonTools.Core.Easing return InBack((t * 2) - d, b + c / 2, c / 2, d, s); } } + + // OUT BOUNCE + + public static double OutBounce(double t) => NormalizedTime(OutBounce, t); + public static double OutBounce(double time, double start, double end) => TimeRange(OutBounce, time, start, end); + + public static double OutBounce(double t, double b, double c, double d) + { + CheckTime(t, d); + t = t / d; + if (t < 1 / 2.75) + { + return c * (7.5625 * t * t) + b; + } + else if (t < 2 / 2.75) + { + t = t - (1.5 / 2.75); + return c * (7.5625 * t * t + 0.75) + b; + } + else if (t < 2.5 / 2.75) + { + t = t - (2.25 / 2.75); + return c * (7.5625 * t * t + 0.9375) + b; + } + else + { + t = t - (2.625 / 2.75); + return c * (7.5625 * t * t + 0.984375) + b; + } + } + + // IN BOUNCE + + public static double InBounce(double t) => NormalizedTime(InBounce, t); + public static double InBounce(double time, double start, double end) => TimeRange(InBounce, time, start, end); + + public static double InBounce(double t, double b, double c, double d) + { + CheckTime(t, d); + return c - OutBounce(d - t, 0, c, d) + b; + } + + // IN OUT BOUNCE + + public static double InOutBounce(double t) => NormalizedTime(InOutBounce, t); + public static double InOutBounce(double time, double start, double end) => TimeRange(InOutBounce, time, start, end); + + public static double InOutBounce(double t, double b, double c, double d) + { + CheckTime(t, d); + if (t < d / 2) + { + return InBounce(t * 2, 0, c, d) * 0.5 + b; + } + else + { + return OutBounce(t * 2 - d, 0, c, d) * 0.5 + c * 0.5 + b; + } + } + + // OUT IN BOUNCE + + public static double OutInBounce(double t) => NormalizedTime(OutInBounce, t); + public static double OutInBounce(double time, double start, double end) => TimeRange(OutInBounce, time, start, end); + public static double OutInBounce(double t, double b, double c, double d) => OutIn(OutBounce, InBounce, t, b, c, d); } } diff --git a/Test/Easing.cs b/Test/Easing.cs index e3046a1..b5653e2 100644 --- a/Test/Easing.cs +++ b/Test/Easing.cs @@ -1146,6 +1146,62 @@ namespace Test Easing.OutInBackTimeRange(4, 2, 6, 3).Should().BeApproximately(4, 0.001); Easing.OutInBackTimeRange(5, 2, 6, 3).Should().BeApproximately(3.5, 0.001); } + + [Test] + public void OutBounce() + { + Easing.OutBounce(0.25).Should().BeApproximately(0.47265625, 0.001); + Easing.OutBounce(0.5).Should().BeApproximately(0.765625, 0.001); + Easing.OutBounce(0.75).Should().BeApproximately(0.97265625, 0.001); + + Easing.OutBounce(3, 2, 6).Should().BeApproximately(3.890625, 0.001); + Easing.OutBounce(4, 2, 6).Should().BeApproximately(5.0625, 0.001); + Easing.OutBounce(5, 2, 6).Should().BeApproximately(5.890625, 0.001); + + CheckDoubleArguments(Easing.OutBounce, Easing.OutBounce); + } + + [Test] + public void InBounce() + { + Easing.InBounce(0.25).Should().BeApproximately(0.02734375, 0.001); + Easing.InBounce(0.5).Should().BeApproximately(0.234375, 0.001); + Easing.InBounce(0.75).Should().BeApproximately(0.52734375, 0.001); + + Easing.InBounce(3, 2, 6).Should().BeApproximately(2.109375, 0.001); + Easing.InBounce(4, 2, 6).Should().BeApproximately(2.9375, 0.001); + Easing.InBounce(5, 2, 6).Should().BeApproximately(4.109375, 0.001); + + CheckDoubleArguments(Easing.InBounce, Easing.InBounce); + } + + [Test] + public void InOutBounce() + { + Easing.InOutBounce(0.25).Should().BeApproximately(0.1171875, 0.001); + Easing.InOutBounce(0.5).Should().BeApproximately(0.5, 0.001); + Easing.InOutBounce(0.75).Should().BeApproximately(0.8828125, 0.001); + + Easing.InOutBounce(3, 2, 6).Should().BeApproximately(2.46875, 0.001); + Easing.InOutBounce(4, 2, 6).Should().BeApproximately(4, 0.001); + Easing.InOutBounce(5, 2, 6).Should().BeApproximately(5.53125, 0.001); + + CheckDoubleArguments(Easing.InOutBounce, Easing.InOutBounce); + } + + [Test] + public void OutInBounce() + { + Easing.OutInBounce(0.25).Should().BeApproximately(0.3828125, 0.001); + Easing.OutInBounce(0.5).Should().BeApproximately(0.5, 0.001); + Easing.OutInBounce(0.75).Should().BeApproximately(0.6171875, 0.001); + + Easing.OutInBounce(3, 2, 6).Should().BeApproximately(3.53125, 0.001); + Easing.OutInBounce(4, 2, 6).Should().BeApproximately(4, 0.001); + Easing.OutInBounce(5, 2, 6).Should().BeApproximately(4.46875, 0.001); + + CheckDoubleArguments(Easing.OutInBounce, Easing.OutInBounce); + } } } } \ No newline at end of file