ease.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (c), Recep Aslantas.
  3. *
  4. * MIT License (MIT), http://opensource.org/licenses/MIT
  5. * Full license can be found in the LICENSE file
  6. */
  7. #ifndef cglm_ease_h
  8. #define cglm_ease_h
  9. #include "common.h"
  10. CGLM_INLINE
  11. float
  12. glm_ease_linear(float t) {
  13. return t;
  14. }
  15. CGLM_INLINE
  16. float
  17. glm_ease_sine_in(float t) {
  18. return sinf((t - 1.0f) * GLM_PI_2f) + 1.0f;
  19. }
  20. CGLM_INLINE
  21. float
  22. glm_ease_sine_out(float t) {
  23. return sinf(t * GLM_PI_2f);
  24. }
  25. CGLM_INLINE
  26. float
  27. glm_ease_sine_inout(float t) {
  28. return 0.5f * (1.0f - cosf(t * GLM_PIf));
  29. }
  30. CGLM_INLINE
  31. float
  32. glm_ease_quad_in(float t) {
  33. return t * t;
  34. }
  35. CGLM_INLINE
  36. float
  37. glm_ease_quad_out(float t) {
  38. return -(t * (t - 2.0f));
  39. }
  40. CGLM_INLINE
  41. float
  42. glm_ease_quad_inout(float t) {
  43. float tt;
  44. tt = t * t;
  45. if (t < 0.5f)
  46. return 2.0f * tt;
  47. return (-2.0f * tt) + (4.0f * t) - 1.0f;
  48. }
  49. CGLM_INLINE
  50. float
  51. glm_ease_cubic_in(float t) {
  52. return t * t * t;
  53. }
  54. CGLM_INLINE
  55. float
  56. glm_ease_cubic_out(float t) {
  57. float f;
  58. f = t - 1.0f;
  59. return f * f * f + 1.0f;
  60. }
  61. CGLM_INLINE
  62. float
  63. glm_ease_cubic_inout(float t) {
  64. float f;
  65. if (t < 0.5f)
  66. return 4.0f * t * t * t;
  67. f = 2.0f * t - 2.0f;
  68. return 0.5f * f * f * f + 1.0f;
  69. }
  70. CGLM_INLINE
  71. float
  72. glm_ease_quart_in(float t) {
  73. float f;
  74. f = t * t;
  75. return f * f;
  76. }
  77. CGLM_INLINE
  78. float
  79. glm_ease_quart_out(float t) {
  80. float f;
  81. f = t - 1.0f;
  82. return f * f * f * (1.0f - t) + 1.0f;
  83. }
  84. CGLM_INLINE
  85. float
  86. glm_ease_quart_inout(float t) {
  87. float f, g;
  88. if (t < 0.5f) {
  89. f = t * t;
  90. return 8.0f * f * f;
  91. }
  92. f = t - 1.0f;
  93. g = f * f;
  94. return -8.0f * g * g + 1.0f;
  95. }
  96. CGLM_INLINE
  97. float
  98. glm_ease_quint_in(float t) {
  99. float f;
  100. f = t * t;
  101. return f * f * t;
  102. }
  103. CGLM_INLINE
  104. float
  105. glm_ease_quint_out(float t) {
  106. float f, g;
  107. f = t - 1.0f;
  108. g = f * f;
  109. return g * g * f + 1.0f;
  110. }
  111. CGLM_INLINE
  112. float
  113. glm_ease_quint_inout(float t) {
  114. float f, g;
  115. if (t < 0.5f) {
  116. f = t * t;
  117. return 16.0f * f * f * t;
  118. }
  119. f = 2.0f * t - 2.0f;
  120. g = f * f;
  121. return 0.5f * g * g * f + 1.0f;
  122. }
  123. CGLM_INLINE
  124. float
  125. glm_ease_exp_in(float t) {
  126. if (t == 0.0f)
  127. return t;
  128. return powf(2.0f, 10.0f * (t - 1.0f));
  129. }
  130. CGLM_INLINE
  131. float
  132. glm_ease_exp_out(float t) {
  133. if (t == 1.0f)
  134. return t;
  135. return 1.0f - powf(2.0f, -10.0f * t);
  136. }
  137. CGLM_INLINE
  138. float
  139. glm_ease_exp_inout(float t) {
  140. if (t == 0.0f || t == 1.0f)
  141. return t;
  142. if (t < 0.5f)
  143. return 0.5f * powf(2.0f, (20.0f * t) - 10.0f);
  144. return -0.5f * powf(2.0f, (-20.0f * t) + 10.0f) + 1.0f;
  145. }
  146. CGLM_INLINE
  147. float
  148. glm_ease_circ_in(float t) {
  149. return 1.0f - sqrtf(1.0f - (t * t));
  150. }
  151. CGLM_INLINE
  152. float
  153. glm_ease_circ_out(float t) {
  154. return sqrtf((2.0f - t) * t);
  155. }
  156. CGLM_INLINE
  157. float
  158. glm_ease_circ_inout(float t) {
  159. if (t < 0.5f)
  160. return 0.5f * (1.0f - sqrtf(1.0f - 4.0f * (t * t)));
  161. return 0.5f * (sqrtf(-((2.0f * t) - 3.0f) * ((2.0f * t) - 1.0f)) + 1.0f);
  162. }
  163. CGLM_INLINE
  164. float
  165. glm_ease_back_in(float t) {
  166. float o, z;
  167. o = 1.70158f;
  168. z = ((o + 1.0f) * t) - o;
  169. return t * t * z;
  170. }
  171. CGLM_INLINE
  172. float
  173. glm_ease_back_out(float t) {
  174. float o, z, n;
  175. o = 1.70158f;
  176. n = t - 1.0f;
  177. z = (o + 1.0f) * n + o;
  178. return n * n * z + 1.0f;
  179. }
  180. CGLM_INLINE
  181. float
  182. glm_ease_back_inout(float t) {
  183. float o, z, n, m, s, x;
  184. o = 1.70158f;
  185. s = o * 1.525f;
  186. x = 0.5;
  187. n = t / 0.5f;
  188. if (n < 1.0f) {
  189. z = (s + 1) * n - s;
  190. m = n * n * z;
  191. return x * m;
  192. }
  193. n -= 2.0f;
  194. z = (s + 1.0f) * n + s;
  195. m = (n * n * z) + 2;
  196. return x * m;
  197. }
  198. CGLM_INLINE
  199. float
  200. glm_ease_elast_in(float t) {
  201. return sinf(13.0f * GLM_PI_2f * t) * powf(2.0f, 10.0f * (t - 1.0f));
  202. }
  203. CGLM_INLINE
  204. float
  205. glm_ease_elast_out(float t) {
  206. return sinf(-13.0f * GLM_PI_2f * (t + 1.0f)) * powf(2.0f, -10.0f * t) + 1.0f;
  207. }
  208. CGLM_INLINE
  209. float
  210. glm_ease_elast_inout(float t) {
  211. float a;
  212. a = 2.0f * t;
  213. if (t < 0.5f)
  214. return 0.5f * sinf(13.0f * GLM_PI_2f * a)
  215. * powf(2.0f, 10.0f * (a - 1.0f));
  216. return 0.5f * (sinf(-13.0f * GLM_PI_2f * a)
  217. * powf(2.0f, -10.0f * (a - 1.0f)) + 2.0f);
  218. }
  219. CGLM_INLINE
  220. float
  221. glm_ease_bounce_out(float t) {
  222. float tt;
  223. tt = t * t;
  224. if (t < (4.0f / 11.0f))
  225. return (121.0f * tt) / 16.0f;
  226. if (t < 8.0f / 11.0f)
  227. return ((363.0f / 40.0f) * tt) - ((99.0f / 10.0f) * t) + (17.0f / 5.0f);
  228. if (t < (9.0f / 10.0f))
  229. return (4356.0f / 361.0f) * tt
  230. - (35442.0f / 1805.0f) * t
  231. + (16061.0f / 1805.0f);
  232. return ((54.0f / 5.0f) * tt) - ((513.0f / 25.0f) * t) + (268.0f / 25.0f);
  233. }
  234. CGLM_INLINE
  235. float
  236. glm_ease_bounce_in(float t) {
  237. return 1.0f - glm_ease_bounce_out(1.0f - t);
  238. }
  239. CGLM_INLINE
  240. float
  241. glm_ease_bounce_inout(float t) {
  242. if (t < 0.5f)
  243. return 0.5f * (1.0f - glm_ease_bounce_out(t * 2.0f));
  244. return 0.5f * glm_ease_bounce_out(t * 2.0f - 1.0f) + 0.5f;
  245. }
  246. #endif /* cglm_ease_h */