par_easings.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // EASINGS :: https://github.com/prideout/par
  2. // Robert Penner's easing functions.
  3. //
  4. // Distributed under the MIT License, see bottom of file.
  5. // -----------------------------------------------------------------------------
  6. // BEGIN PUBLIC API
  7. // -----------------------------------------------------------------------------
  8. #ifndef PAR_EASINGS_H
  9. #define PAR_EASINGS_H
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. #ifndef PAR_EASINGS_FLOAT
  14. #define PAR_EASINGS_FLOAT float
  15. #endif
  16. PAR_EASINGS_FLOAT par_easings_linear(PAR_EASINGS_FLOAT t);
  17. PAR_EASINGS_FLOAT par_easings_in_cubic(PAR_EASINGS_FLOAT t);
  18. PAR_EASINGS_FLOAT par_easings_out_cubic(PAR_EASINGS_FLOAT t);
  19. PAR_EASINGS_FLOAT par_easings_in_out_cubic(PAR_EASINGS_FLOAT t);
  20. PAR_EASINGS_FLOAT par_easings_in_quad(PAR_EASINGS_FLOAT t);
  21. PAR_EASINGS_FLOAT par_easings_out_quad(PAR_EASINGS_FLOAT t);
  22. PAR_EASINGS_FLOAT par_easings_in_out_quad(PAR_EASINGS_FLOAT t);
  23. PAR_EASINGS_FLOAT par_easings_in_elastic(PAR_EASINGS_FLOAT t);
  24. PAR_EASINGS_FLOAT par_easings_out_elastic(PAR_EASINGS_FLOAT t);
  25. PAR_EASINGS_FLOAT par_easings_in_out_elastic(PAR_EASINGS_FLOAT t);
  26. PAR_EASINGS_FLOAT par_easings_in_bounce(PAR_EASINGS_FLOAT t);
  27. PAR_EASINGS_FLOAT par_easings_out_bounce(PAR_EASINGS_FLOAT t);
  28. PAR_EASINGS_FLOAT par_easings_in_out_bounce(PAR_EASINGS_FLOAT t);
  29. PAR_EASINGS_FLOAT par_easings_in_back(PAR_EASINGS_FLOAT t);
  30. PAR_EASINGS_FLOAT par_easings_out_back(PAR_EASINGS_FLOAT t);
  31. PAR_EASINGS_FLOAT par_easings_in_out_back(PAR_EASINGS_FLOAT t);
  32. #ifndef PAR_PI
  33. #define PAR_PI (3.14159265359)
  34. #define PAR_MIN(a, b) (a > b ? b : a)
  35. #define PAR_MAX(a, b) (a > b ? a : b)
  36. #define PAR_CLAMP(v, lo, hi) PAR_MAX(lo, PAR_MIN(hi, v))
  37. #define PAR_SWAP(T, A, B) { T tmp = B; B = A; A = tmp; }
  38. #define PAR_SQR(a) ((a) * (a))
  39. #endif
  40. #ifdef __cplusplus
  41. }
  42. #endif
  43. // -----------------------------------------------------------------------------
  44. // END PUBLIC API
  45. // -----------------------------------------------------------------------------
  46. #ifdef PAR_EASINGS_IMPLEMENTATION
  47. #define PARFLT PAR_EASINGS_FLOAT
  48. PARFLT par_easings__linear(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  49. {
  50. return c * t / d + b;
  51. }
  52. PARFLT par_easings__in_cubic(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  53. {
  54. t /= d;
  55. return c * t * t * t + b;
  56. }
  57. PARFLT par_easings__out_cubic(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  58. {
  59. t = t / d - 1;
  60. return c * (t * t * t + 1) + b;
  61. }
  62. PARFLT par_easings__in_out_cubic(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  63. {
  64. t /= d / 2;
  65. if (t < 1) {
  66. return c / 2 * t * t * t + b;
  67. }
  68. t -= 2;
  69. return c / 2 * (t * t * t + 2) + b;
  70. }
  71. PARFLT par_easings__in_quad(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  72. {
  73. t /= d;
  74. return c * t * t + b;
  75. }
  76. PARFLT par_easings__out_quad(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  77. {
  78. t /= d;
  79. return -c * t * (t - 2) + b;
  80. }
  81. PARFLT par_easings__in_out_quad(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  82. {
  83. t /= d / 2;
  84. if (t < 1) {
  85. return c / 2 * t * t + b;
  86. }
  87. --t;
  88. return -c / 2 * (t * (t - 2) - 1) + b;
  89. }
  90. PARFLT par_easings__in_quart(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  91. {
  92. t /= d;
  93. return c * t * t * t * t + b;
  94. }
  95. PARFLT par_easings__out_quart(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  96. {
  97. t = t / d - 1;
  98. return -c * (t * t * t * t - 1) + b;
  99. }
  100. PARFLT par_easings__in_out_quart(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  101. {
  102. t /= d / 2;
  103. if (t < 1) {
  104. return c / 2 * t * t * t * t + b;
  105. }
  106. t -= 2;
  107. return -c / 2 * (t * t * t * t - 2) + b;
  108. }
  109. PARFLT par_easings__in_quint(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  110. {
  111. t /= d;
  112. return c * t * t * t * t * t + b;
  113. }
  114. PARFLT par_easings__out_quint(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  115. {
  116. t = t / d - 1;
  117. return c * (t * t * t * t * t + 1) + b;
  118. }
  119. PARFLT par_easings__in_out_quint(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  120. {
  121. t /= d / 2;
  122. if (t < 1) {
  123. return c / 2 * t * t * t * t * t + b;
  124. }
  125. t -= 2;
  126. return c / 2 * (t * t * t * t * t + 2) + b;
  127. }
  128. PARFLT par_easings__in_sine(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  129. {
  130. return -c * cos(t / d * (PAR_PI / 2)) + c + b;
  131. }
  132. PARFLT par_easings__out_sine(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  133. {
  134. return c * sin(t / d * (PAR_PI / 2)) + b;
  135. }
  136. PARFLT par_easings__in_out_sine(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  137. {
  138. return -c / 2 * (cos(PAR_PI * t / d) - 1) + b;
  139. }
  140. PARFLT par_easings__in_out_expo(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  141. {
  142. t /= d / 2;
  143. if (t < 1) {
  144. return c / 2 * pow(2, 10 * (t - 1)) + b;
  145. }
  146. return c / 2 * (-pow(2, -10 * --t) + 2) + b;
  147. }
  148. PARFLT par_easings__in_circ(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  149. {
  150. t /= d;
  151. return -c * (sqrt(1 - t * t) - 1) + b;
  152. }
  153. PARFLT par_easings__out_circ(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  154. {
  155. t = t / d - 1;
  156. return c * sqrt(1 - t * t) + b;
  157. }
  158. PARFLT par_easings__in_out_circ(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  159. {
  160. t /= d / 2;
  161. if (t < 1) {
  162. return -c / 2 * (sqrt(1 - t * t) - 1) + b;
  163. }
  164. t -= 2;
  165. return c / 2 * (sqrt(1 - t * t) + 1) + b;
  166. }
  167. PARFLT par_easings__in_elastic(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  168. {
  169. PARFLT a, p, s;
  170. s = 1.70158;
  171. p = 0;
  172. a = c;
  173. if (!p) {
  174. p = d * 0.3;
  175. }
  176. if (a < fabsf(c)) {
  177. a = c;
  178. s = p / 4;
  179. } else {
  180. s = p / (2 * PAR_PI) * asin(c / a);
  181. }
  182. t -= 1;
  183. return -(a * pow(2, 10 * t) *
  184. sin((t * d - s) * (2 * PAR_PI) / p)) + b;
  185. }
  186. PARFLT par_easings__out_elastic(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  187. {
  188. PARFLT a, p, s;
  189. s = 1.70158;
  190. p = 0;
  191. a = c;
  192. if (!p) {
  193. p = d * 0.3;
  194. }
  195. if (a < fabsf(c)) {
  196. a = c;
  197. s = p / 4;
  198. } else {
  199. s = p / (2 * PAR_PI) * asin(c / a);
  200. }
  201. return a * pow(2, -10 * t) *
  202. sin((t * d - s) * (2 * PAR_PI) / p) + c + b;
  203. }
  204. PARFLT par_easings__in_out_elastic(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  205. {
  206. PARFLT a, p, s;
  207. s = 1.70158;
  208. p = 0;
  209. a = c;
  210. if (!p) {
  211. p = d * (0.3 * 1.5);
  212. }
  213. if (a < fabsf(c)) {
  214. a = c;
  215. s = p / 4;
  216. } else {
  217. s = p / (2 * PAR_PI) * asin(c / a);
  218. }
  219. if (t < 1) {
  220. t -= 1;
  221. return -0.5 * (a * pow(2, 10 * t) *
  222. sin((t * d - s) * (2 * PAR_PI) / p)) + b;
  223. }
  224. t -= 1;
  225. return a * pow(2, -10 * t) *
  226. sin((t * d - s) * (2 * PAR_PI) / p) * 0.5 + c + b;
  227. }
  228. PARFLT par_easings__in_back(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  229. {
  230. PARFLT s = 1.70158;
  231. t/=d;
  232. return c*t*t*((s+1)*t - s) + b;
  233. }
  234. PARFLT par_easings__out_back(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  235. {
  236. PARFLT s = 1.70158;
  237. t=t/d-1;
  238. return c*(t*t*((s+1)*t + s) + 1) + b;
  239. }
  240. PARFLT par_easings__in_out_back(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  241. {
  242. PARFLT s = 1.70158;
  243. t/=d/2;
  244. s*=1.525;
  245. if (t < 1) { return c/2*(t*t*((s+1)*t - s)) + b; }
  246. s*=1.525;
  247. t-=2;
  248. return (c/2*(t*t*(s+1)*t + s) + 2) + b;
  249. }
  250. PARFLT par_easings__out_bounce(PARFLT t, PARFLT b, PARFLT c, PARFLT d);
  251. PARFLT par_easings__in_bounce(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  252. {
  253. PARFLT v = par_easings__out_bounce(d - t, 0, c, d);
  254. return c - v + b;
  255. }
  256. PARFLT par_easings__out_bounce(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  257. {
  258. t /= d;
  259. if (t < 1.0 / 2.75) {
  260. return c * (7.5625 * t * t) + b;
  261. }
  262. if (t < 2.0 / 2.75) {
  263. t -= 1.5 / 2.75;
  264. return c * (7.5625 * t * t + 0.75) + b;
  265. }
  266. if (t < 2.5 / 2.75) {
  267. t -= 2.25 / 2.75;
  268. return c * (7.5625 * t * t + 0.9375) + b;
  269. }
  270. t -= 2.625 / 2.75;
  271. return c * (7.5625 * t * t + 0.984375) + b;
  272. }
  273. PARFLT par_easings__in_out_bounce(PARFLT t, PARFLT b, PARFLT c, PARFLT d)
  274. {
  275. PARFLT v;
  276. if (t < d / 2) {
  277. v = par_easings__in_bounce(t * 2, 0, c, d);
  278. return v * 0.5 + b;
  279. }
  280. v = par_easings__out_bounce(t * 2 - d, 0, c, d);
  281. return v * 0.5 + c * 0.5 + b;
  282. }
  283. PARFLT par_easings_linear(PARFLT t)
  284. {
  285. return par_easings__linear(t, 0, 1, 1);
  286. }
  287. PARFLT par_easings_in_cubic(PARFLT t)
  288. {
  289. return par_easings__in_cubic(t, 0, 1, 1);
  290. }
  291. PARFLT par_easings_out_cubic(PARFLT t)
  292. {
  293. return par_easings__out_cubic(t, 0, 1, 1);
  294. }
  295. PARFLT par_easings_in_out_cubic(PARFLT t)
  296. {
  297. return par_easings__in_out_cubic(t, 0, 1, 1);
  298. }
  299. PARFLT par_easings_in_quad(PARFLT t)
  300. {
  301. return par_easings__in_quad(t, 0, 1, 1);
  302. }
  303. PARFLT par_easings_out_quad(PARFLT t)
  304. {
  305. return par_easings__out_quad(t, 0, 1, 1);
  306. }
  307. PARFLT par_easings_in_out_quad(PARFLT t)
  308. {
  309. return par_easings__in_out_quad(t, 0, 1, 1);
  310. }
  311. PARFLT par_easings_in_elastic(PARFLT t)
  312. {
  313. return par_easings__in_elastic(t, 0, 1, 1);
  314. }
  315. PARFLT par_easings_out_elastic(PARFLT t)
  316. {
  317. return par_easings__out_elastic(t, 0, 1, 1);
  318. }
  319. PARFLT par_easings_in_out_elastic(PARFLT t)
  320. {
  321. return par_easings__in_out_elastic(t, 0, 1, 1);
  322. }
  323. PARFLT par_easings_in_bounce(PARFLT t)
  324. {
  325. return par_easings__in_bounce(t, 0, 1, 1);
  326. }
  327. PARFLT par_easings_out_bounce(PARFLT t)
  328. {
  329. return par_easings__out_bounce(t, 0, 1, 1);
  330. }
  331. PARFLT par_easings_in_out_bounce(PARFLT t)
  332. {
  333. return par_easings__in_out_bounce(t, 0, 1, 1);
  334. }
  335. PARFLT par_easings_in_back(PARFLT t)
  336. {
  337. return par_easings__in_back(t, 0, 1, 1);
  338. }
  339. PARFLT par_easings_out_back(PARFLT t)
  340. {
  341. return par_easings__out_back(t, 0, 1, 1);
  342. }
  343. PARFLT par_easings_in_out_back(PARFLT t)
  344. {
  345. return par_easings__in_out_back(t, 0, 1, 1);
  346. }
  347. #undef PARFLT
  348. #endif // PAR_EASINGS_IMPLEMENTATION
  349. #endif // PAR_EASINGS_H
  350. // par_easings is distributed under the MIT license:
  351. //
  352. // Copyright (c) 2019 Philip Rideout
  353. //
  354. // Permission is hereby granted, free of charge, to any person obtaining a copy
  355. // of this software and associated documentation files (the "Software"), to deal
  356. // in the Software without restriction, including without limitation the rights
  357. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  358. // copies of the Software, and to permit persons to whom the Software is
  359. // furnished to do so, subject to the following conditions:
  360. //
  361. // The above copyright notice and this permission notice shall be included in
  362. // all copies or substantial portions of the Software.
  363. //
  364. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  365. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  366. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  367. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  368. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  369. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  370. // SOFTWARE.