💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
6.9 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * Easing Functions (Equations)
  21. *
  22. * Copyright (C) 2001 Robert Penner
  23. *
  24. * Redistribution and use in source and binary forms, with or without
  25. * modification, are permitted provided that the following conditions are met:
  26. *
  27. * * Redistributions of source code must retain the above copyright notice, this
  28. * list of conditions and the following disclaimer.
  29. *
  30. * * Redistributions in binary form must reproduce the above copyright notice,
  31. * this list of conditions and the following disclaimer in the documentation
  32. * and/or other materials provided with the distribution.
  33. *
  34. * * Neither the name of the author nor the names of contributors may be used to
  35. * endorse or promote products derived from this software without specific
  36. * prior written permission.
  37. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  38. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  40. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  41. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  42. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  43. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  44. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  46. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. */
  48. #ifndef ANTKEEPER_EASINGS_HPP
  49. #define ANTKEEPER_EASINGS_HPP
  50. #include <vmq/vmq.hpp>
  51. #include <cmath>
  52. template <typename T, typename S>
  53. inline T ease_linear(const T& x, const T& y, S a)
  54. {
  55. return (y - x) * a + x;
  56. }
  57. template <typename T, typename S>
  58. T ease_in_sine(const T& x, const T& y, S a)
  59. {
  60. return -(y - x) * std::cos(a * vmq::half_pi<S>) + (y - x) + x;
  61. }
  62. template <typename T, typename S>
  63. T ease_out_sine(const T& x, const T& y, S a)
  64. {
  65. return (y - x) * std::sin(a * vmq::half_pi<S>) + x;
  66. }
  67. template <typename T, typename S>
  68. T ease_in_out_sine(const T& x, const T& y, S a)
  69. {
  70. return -(y - x) * S(0.5) * (std::cos(vmq::pi<S> * a) - S(1.0)) + x;
  71. }
  72. template <typename T, typename S>
  73. T ease_in_quad(const T& x, const T& y, S a)
  74. {
  75. return (y - x) * a * a + x;
  76. }
  77. template <typename T, typename S>
  78. T ease_out_quad(const T& x, const T& y, S a)
  79. {
  80. return -(y - x) * a * (a - S(2.0)) + x;
  81. }
  82. template <typename T, typename S>
  83. T ease_in_out_quad(const T& x, const T& y, S a)
  84. {
  85. a *= S(2.0);
  86. if (a < S(1.0))
  87. {
  88. return (y - x) * S(0.5) * a * a + x;
  89. }
  90. a -= S(1.0);
  91. return -(y - x) * S(0.5) * (a * (a - S(2.0)) - S(1.0)) + x;
  92. }
  93. template <typename T, typename S>
  94. T ease_in_cubic(const T& x, const T& y, S a)
  95. {
  96. return (y - x) * a * a * a + x;
  97. }
  98. template <typename T, typename S>
  99. T ease_out_cubic(const T& x, const T& y, S a)
  100. {
  101. a -= S(1.0);
  102. return (y - x) * (a * a * a + S(1.0)) + x;
  103. }
  104. template <typename T, typename S>
  105. T ease_in_out_cubic(const T& x, const T& y, S a)
  106. {
  107. a *= S(2.0);
  108. if (a < S(1.0))
  109. {
  110. return (y - x) * S(0.5) * a * a * a + x;
  111. }
  112. a -= S(2.0);
  113. return (y - x) * S(0.5) * (a * a * a + S(2.0)) + x;
  114. }
  115. template <typename T, typename S>
  116. T ease_in_quart(const T& x, const T& y, S a)
  117. {
  118. return (y - x) * a * a * a * a + x;
  119. }
  120. template <typename T, typename S>
  121. T ease_out_quart(const T& x, const T& y, S a)
  122. {
  123. a -= S(1.0);
  124. return -(y - x) * (a * a * a * a - S(1.0)) + x;
  125. }
  126. template <typename T, typename S>
  127. T ease_in_out_quart(const T& x, const T& y, S a)
  128. {
  129. a *= S(2.0);
  130. if (a < S(1.0))
  131. {
  132. return (y - x) * S(0.5) * a * a * a * a + x;
  133. }
  134. a -= S(2.0);
  135. return -(y - x) * S(0.5) * (a * a * a * a - S(2.0)) + x;
  136. }
  137. template <typename T, typename S>
  138. T ease_in_quint(const T& x, const T& y, S a)
  139. {
  140. return (y - x) * a * a * a * a * a + x;
  141. }
  142. template <typename T, typename S>
  143. T ease_out_quint(const T& x, const T& y, S a)
  144. {
  145. a -= S(1.0);
  146. return (y - x) * (a * a * a * a * a + S(1.0)) + x;
  147. }
  148. template <typename T, typename S>
  149. T ease_in_out_quint(const T& x, const T& y, S a)
  150. {
  151. a *= S(2.0);
  152. if (a < S(1.0))
  153. {
  154. return (y - x) * S(0.5) * a * a * a * a * a + x;
  155. }
  156. a -= S(2.0);
  157. return (y - x) * S(0.5) * (a * a * a * a * a + S(2.0)) + x;
  158. }
  159. template <typename T, typename S>
  160. T ease_in_expo(const T& x, const T& y, S a)
  161. {
  162. if (a == S(0.0))
  163. {
  164. return x;
  165. }
  166. return (y - x) * std::pow(S(2.0), S(10.0) * (a - S(1.0))) + x;
  167. }
  168. template <typename T, typename S>
  169. T ease_out_expo(const T& x, const T& y, S a)
  170. {
  171. if (a == S(1.0))
  172. {
  173. return y;
  174. }
  175. return (y - x) * (-std::pow(S(2.0), -S(10.0) * a) + S(1.0)) + x;
  176. }
  177. template <typename T, typename S>
  178. T ease_in_out_expo(const T& x, const T& y, S a)
  179. {
  180. if (a == S(0.0))
  181. {
  182. return x;
  183. }
  184. else if (a == S(1.0))
  185. {
  186. return y;
  187. }
  188. a *= S(2.0);
  189. if (a < S(1.0))
  190. {
  191. return (y - x) * S(0.5) * std::pow(S(2.0), S(10.0) * (a - S(1.0))) + x;
  192. }
  193. a -= S(1.0);
  194. return (y - x) * S(0.5) * (-std::pow(S(2.0), -S(10.0) * a) + S(2.0)) + x;
  195. }
  196. template <typename T, typename S>
  197. T ease_in_circ(const T& x, const T& y, S a)
  198. {
  199. return -(y - x) * (std::sqrt(S(1.0) - a * a) - S(1.0)) + x;
  200. }
  201. template <typename T, typename S>
  202. T ease_out_circ(const T& x, const T& y, S a)
  203. {
  204. a -= S(1.0);
  205. return (y - x) * std::sqrt(S(1.0) - a * a) + x;
  206. }
  207. template <typename T, typename S>
  208. T ease_in_out_circ(const T& x, const T& y, S a)
  209. {
  210. a *= S(2.0);
  211. if (a < S(1.0))
  212. {
  213. return -(y - x) * S(0.5) * (std::sqrt(S(1.0) - a * a) - S(1.0)) + x;
  214. }
  215. a -= S(2.0);
  216. return (y - x) * S(0.5) * (std::sqrt(S(1.0) - a * a) + S(1.0)) + x;
  217. }
  218. template <typename T, typename S>
  219. T ease_in_back(const T& x, const T& y, S a)
  220. {
  221. const S s = S(1.70158);
  222. return (y - x) * a * a * ((s + S(1.0)) * a - s) + x;
  223. }
  224. template <typename T, typename S>
  225. T ease_out_back(const T& x, const T& y, S a)
  226. {
  227. const S s = S(1.70158);
  228. a -= S(1.0);
  229. return (y - x) * (a * a * ((s + S(1.0)) * a + s) + S(1.0)) + x;
  230. }
  231. template <typename T, typename S>
  232. T ease_in_out_back(const T& x, const T& y, S a)
  233. {
  234. const S s = S(1.70158) * S(1.525f);
  235. a *= S(2.0);
  236. if (a < S(1.0))
  237. {
  238. return (y - x) * S(0.5) * (a * a * ((s + S(1.0)) * a - s)) + x;
  239. }
  240. a -= S(2.0);
  241. return (y - x) * S(0.5) * (a * a * ((s + S(1.0)) * a + s) + S(2.0)) + x;
  242. }
  243. #endif // ANTKEEPER_EASINGS_HPP