1
0

easing.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "pocketpy/pocketpy.h"
  2. #include "pocketpy/interpreter/vm.h"
  3. #include <math.h>
  4. // https://easings.net/
  5. const double kPi = 3.1415926545;
  6. static double easeLinear(double x) { return x; }
  7. static double easeInSine(double x) { return 1.0 - cos(x * kPi / 2); }
  8. static double easeOutSine(double x) { return sin(x * kPi / 2); }
  9. static double easeInOutSine(double x) { return -(cos(kPi * x) - 1) / 2; }
  10. static double easeInQuad(double x) { return x * x; }
  11. static double easeOutQuad(double x) { return 1 - pow(1 - x, 2); }
  12. static double easeInOutQuad(double x) {
  13. if(x < 0.5) {
  14. return 2 * x * x;
  15. } else {
  16. return 1 - pow(-2 * x + 2, 2) / 2;
  17. }
  18. }
  19. static double easeInCubic(double x) { return x * x * x; }
  20. static double easeOutCubic(double x) { return 1 - pow(1 - x, 3); }
  21. static double easeInOutCubic(double x) {
  22. if(x < 0.5) {
  23. return 4 * x * x * x;
  24. } else {
  25. return 1 - pow(-2 * x + 2, 3) / 2;
  26. }
  27. }
  28. static double easeInQuart(double x) { return pow(x, 4); }
  29. static double easeOutQuart(double x) { return 1 - pow(1 - x, 4); }
  30. static double easeInOutQuart(double x) {
  31. if(x < 0.5) {
  32. return 8 * pow(x, 4);
  33. } else {
  34. return 1 - pow(-2 * x + 2, 4) / 2;
  35. }
  36. }
  37. static double easeInQuint(double x) { return pow(x, 5); }
  38. static double easeOutQuint(double x) { return 1 - pow(1 - x, 5); }
  39. static double easeInOutQuint(double x) {
  40. if(x < 0.5) {
  41. return 16 * pow(x, 5);
  42. } else {
  43. return 1 - pow(-2 * x + 2, 5) / 2;
  44. }
  45. }
  46. static double easeInExpo(double x) { return x == 0 ? 0 : pow(2, 10 * x - 10); }
  47. static double easeOutExpo(double x) { return x == 1 ? 1 : 1 - pow(2, -10 * x); }
  48. static double easeInOutExpo(double x) {
  49. if(x == 0) {
  50. return 0;
  51. } else if(x == 1) {
  52. return 1;
  53. } else if(x < 0.5) {
  54. return pow(2, 20 * x - 10) / 2;
  55. } else {
  56. return (2 - pow(2, -20 * x + 10)) / 2;
  57. }
  58. }
  59. static double easeInCirc(double x) { return 1 - sqrt(1 - pow(x, 2)); }
  60. static double easeOutCirc(double x) { return sqrt(1 - pow(x - 1, 2)); }
  61. static double easeInOutCirc(double x) {
  62. if(x < 0.5) {
  63. return (1 - sqrt(1 - pow(2 * x, 2))) / 2;
  64. } else {
  65. return (sqrt(1 - pow(-2 * x + 2, 2)) + 1) / 2;
  66. }
  67. }
  68. static double easeInBack(double x) {
  69. const double c1 = 1.70158;
  70. const double c3 = c1 + 1;
  71. return c3 * x * x * x - c1 * x * x;
  72. }
  73. static double easeOutBack(double x) {
  74. const double c1 = 1.70158;
  75. const double c3 = c1 + 1;
  76. return 1 + c3 * pow(x - 1, 3) + c1 * pow(x - 1, 2);
  77. }
  78. static double easeInOutBack(double x) {
  79. const double c1 = 1.70158;
  80. const double c2 = c1 * 1.525;
  81. if(x < 0.5) {
  82. return (pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2;
  83. } else {
  84. return (pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2;
  85. }
  86. }
  87. static double easeInElastic(double x) {
  88. const double c4 = (2 * kPi) / 3;
  89. if(x == 0) {
  90. return 0;
  91. } else if(x == 1) {
  92. return 1;
  93. } else {
  94. return -pow(2, 10 * x - 10) * sin((x * 10 - 10.75) * c4);
  95. }
  96. }
  97. static double easeOutElastic(double x) {
  98. const double c4 = (2 * kPi) / 3;
  99. if(x == 0) {
  100. return 0;
  101. } else if(x == 1) {
  102. return 1;
  103. } else {
  104. return pow(2, -10 * x) * sin((x * 10 - 0.75) * c4) + 1;
  105. }
  106. }
  107. static double easeInOutElastic(double x) {
  108. const double c5 = (2 * kPi) / 4.5;
  109. if(x == 0) {
  110. return 0;
  111. } else if(x == 1) {
  112. return 1;
  113. } else if(x < 0.5) {
  114. return -(pow(2, 20 * x - 10) * sin((20 * x - 11.125) * c5)) / 2;
  115. } else {
  116. return (pow(2, -20 * x + 10) * sin((20 * x - 11.125) * c5)) / 2 + 1;
  117. }
  118. }
  119. static double easeOutBounce(double x) {
  120. const double n1 = 7.5625;
  121. const double d1 = 2.75;
  122. if(x < 1 / d1) {
  123. return n1 * x * x;
  124. } else if(x < 2 / d1) {
  125. x -= 1.5 / d1;
  126. return n1 * x * x + 0.75;
  127. } else if(x < 2.5 / d1) {
  128. x -= 2.25 / d1;
  129. return n1 * x * x + 0.9375;
  130. } else {
  131. x -= 2.625 / d1;
  132. return n1 * x * x + 0.984375;
  133. }
  134. }
  135. static double easeInBounce(double x) { return 1 - easeOutBounce(1 - x); }
  136. static double easeInOutBounce(double x) {
  137. return x < 0.5 ? (1 - easeOutBounce(1 - 2 * x)) / 2 : (1 + easeOutBounce(2 * x - 1)) / 2;
  138. }
  139. #define DEF_EASE(name) \
  140. static bool easing_##name(int argc, py_Ref argv) { \
  141. PY_CHECK_ARGC(1); \
  142. py_f64 t; \
  143. if(!py_castfloat(argv, &t)) return false; \
  144. py_newfloat(py_retval(), ease##name(t)); \
  145. return true; \
  146. }
  147. DEF_EASE(Linear)
  148. DEF_EASE(InSine)
  149. DEF_EASE(OutSine)
  150. DEF_EASE(InOutSine)
  151. DEF_EASE(InQuad)
  152. DEF_EASE(OutQuad)
  153. DEF_EASE(InOutQuad)
  154. DEF_EASE(InCubic)
  155. DEF_EASE(OutCubic)
  156. DEF_EASE(InOutCubic)
  157. DEF_EASE(InQuart)
  158. DEF_EASE(OutQuart)
  159. DEF_EASE(InOutQuart)
  160. DEF_EASE(InQuint)
  161. DEF_EASE(OutQuint)
  162. DEF_EASE(InOutQuint)
  163. DEF_EASE(InExpo)
  164. DEF_EASE(OutExpo)
  165. DEF_EASE(InOutExpo)
  166. DEF_EASE(InCirc)
  167. DEF_EASE(OutCirc)
  168. DEF_EASE(InOutCirc)
  169. DEF_EASE(InBack)
  170. DEF_EASE(OutBack)
  171. DEF_EASE(InOutBack)
  172. DEF_EASE(InElastic)
  173. DEF_EASE(OutElastic)
  174. DEF_EASE(InOutElastic)
  175. DEF_EASE(InBounce)
  176. DEF_EASE(OutBounce)
  177. DEF_EASE(InOutBounce)
  178. #undef DEF_EASE
  179. void pk__add_module_easing() {
  180. py_GlobalRef mod = py_newmodule("easing");
  181. py_bindfunc(mod, "Linear", easing_Linear);
  182. py_bindfunc(mod, "InSine", easing_InSine);
  183. py_bindfunc(mod, "OutSine", easing_OutSine);
  184. py_bindfunc(mod, "InOutSine", easing_InOutSine);
  185. py_bindfunc(mod, "InQuad", easing_InQuad);
  186. py_bindfunc(mod, "OutQuad", easing_OutQuad);
  187. py_bindfunc(mod, "InOutQuad", easing_InOutQuad);
  188. py_bindfunc(mod, "InCubic", easing_InCubic);
  189. py_bindfunc(mod, "OutCubic", easing_OutCubic);
  190. py_bindfunc(mod, "InOutCubic", easing_InOutCubic);
  191. py_bindfunc(mod, "InQuart", easing_InQuart);
  192. py_bindfunc(mod, "OutQuart", easing_OutQuart);
  193. py_bindfunc(mod, "InOutQuart", easing_InOutQuart);
  194. py_bindfunc(mod, "InQuint", easing_InQuint);
  195. py_bindfunc(mod, "OutQuint", easing_OutQuint);
  196. py_bindfunc(mod, "InOutQuint", easing_InOutQuint);
  197. py_bindfunc(mod, "InExpo", easing_InExpo);
  198. py_bindfunc(mod, "OutExpo", easing_OutExpo);
  199. py_bindfunc(mod, "InOutExpo", easing_InOutExpo);
  200. py_bindfunc(mod, "InCirc", easing_InCirc);
  201. py_bindfunc(mod, "OutCirc", easing_OutCirc);
  202. py_bindfunc(mod, "InOutCirc", easing_InOutCirc);
  203. py_bindfunc(mod, "InBack", easing_InBack);
  204. py_bindfunc(mod, "OutBack", easing_OutBack);
  205. py_bindfunc(mod, "InOutBack", easing_InOutBack);
  206. py_bindfunc(mod, "InElastic", easing_InElastic);
  207. py_bindfunc(mod, "OutElastic", easing_OutElastic);
  208. py_bindfunc(mod, "InOutElastic", easing_InOutElastic);
  209. py_bindfunc(mod, "InBounce", easing_InBounce);
  210. py_bindfunc(mod, "OutBounce", easing_OutBounce);
  211. py_bindfunc(mod, "InOutBounce", easing_InOutBounce);
  212. }