1 /*
2 Copyright (c) 2019-2021 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 /**
30  * Easing functions
31  *
32  * Copyright: Timur Gafarov 2019-2021.
33  * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0).
34  * Authors: Timur Gafarov
35  */
36 module dlib.math.interpolation.easing;
37 
38 /// Quadratic ease in
39 T easeInQuad(T)(T t)
40 {
41     return t * t;
42 }
43 
44 /// Quadratic ease out
45 T easeOutQuad(T)(T t)
46 {
47     return -t * (t - 2);
48 }
49 
50 /// Quadratic ease in-out
51 T easeInOutQuad(T)(T t)
52 {
53     t /= 0.5;
54     if (t < 1.0)
55         return 0.5 * t * t;
56     t--;
57     return -0.5 * (t * (t - 2.0) - 1.0);
58 }
59 
60 /// Back ease in
61 T easeInBack(T)(T t)
62 {
63     enum T s = 1.70158;
64     return t * t * ((s + 1) * t - s);
65 }
66 
67 /// Back ease out
68 T easeOutBack(T)(T t)
69 {
70     enum T s = 1.70158;
71     t = t - 1.0;
72     return (t * t * ((s + 1) * t + s) + 1.0);
73 }
74 
75 /// Back ease in-out
76 T easeInOutBack(T)(T t)
77 {
78     float s = 1.70158;
79     t /= 0.5;
80     if (t < 1.0)
81     {
82         s *= 1.525;
83         return 0.5 * (t * t * ((s + 1.0) * t - s));
84     }
85     t -= 2.0;
86     s *= 1.525;
87     return 0.5 * (t * t * ((s + 1.0) * t + s) + 2.0);
88 }
89 
90 /// Bounce ease out
91 T easeOutBounce(T)(T t)
92 {
93     if (t < (1.0 / 2.75))
94     {
95         return (7.5625 * t * t);
96     }
97     else if (t < (2.0 / 2.75))
98     {
99         t -= (1.5 / 2.75);
100         return (7.5625 * (t) * t + 0.75);
101     }
102     else if (t < (2.5 / 2.75))
103     {
104         t -= (2.25 / 2.75);
105         return (7.5625 * (t) * t + 0.9375);
106     }
107     else
108     {
109         t -= (2.625 / 2.75);
110         return (7.5625 * (t) * t + 0.984375);
111     }
112 }