easing.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "pocketpy/modules/easing.hpp"
  2. #include "pocketpy/interpreter/bindings.hpp"
  3. #include <cmath>
  4. namespace pkpy {
  5. // https://easings.net/
  6. const double kPi = 3.1415926545;
  7. static double easeLinear(double x) { return x; }
  8. static double easeInSine(double x) { return 1.0 - std::cos(x * kPi / 2); }
  9. static double easeOutSine(double x) { return std::sin(x * kPi / 2); }
  10. static double easeInOutSine(double x) { return -(std::cos(kPi * x) - 1) / 2; }
  11. static double easeInQuad(double x) { return x * x; }
  12. static double easeOutQuad(double x) { return 1 - std::pow(1 - x, 2); }
  13. static double easeInOutQuad(double x) {
  14. if(x < 0.5) {
  15. return 2 * x * x;
  16. } else {
  17. return 1 - std::pow(-2 * x + 2, 2) / 2;
  18. }
  19. }
  20. static double easeInCubic(double x) { return x * x * x; }
  21. static double easeOutCubic(double x) { return 1 - std::pow(1 - x, 3); }
  22. static double easeInOutCubic(double x) {
  23. if(x < 0.5) {
  24. return 4 * x * x * x;
  25. } else {
  26. return 1 - std::pow(-2 * x + 2, 3) / 2;
  27. }
  28. }
  29. static double easeInQuart(double x) { return std::pow(x, 4); }
  30. static double easeOutQuart(double x) { return 1 - std::pow(1 - x, 4); }
  31. static double easeInOutQuart(double x) {
  32. if(x < 0.5) {
  33. return 8 * std::pow(x, 4);
  34. } else {
  35. return 1 - std::pow(-2 * x + 2, 4) / 2;
  36. }
  37. }
  38. static double easeInQuint(double x) { return std::pow(x, 5); }
  39. static double easeOutQuint(double x) { return 1 - std::pow(1 - x, 5); }
  40. static double easeInOutQuint(double x) {
  41. if(x < 0.5) {
  42. return 16 * std::pow(x, 5);
  43. } else {
  44. return 1 - std::pow(-2 * x + 2, 5) / 2;
  45. }
  46. }
  47. static double easeInExpo(double x) { return x == 0 ? 0 : std::pow(2, 10 * x - 10); }
  48. static double easeOutExpo(double x) { return x == 1 ? 1 : 1 - std::pow(2, -10 * x); }
  49. static double easeInOutExpo(double x) {
  50. if(x == 0) {
  51. return 0;
  52. } else if(x == 1) {
  53. return 1;
  54. } else if(x < 0.5) {
  55. return std::pow(2, 20 * x - 10) / 2;
  56. } else {
  57. return (2 - std::pow(2, -20 * x + 10)) / 2;
  58. }
  59. }
  60. static double easeInCirc(double x) { return 1 - std::sqrt(1 - std::pow(x, 2)); }
  61. static double easeOutCirc(double x) { return std::sqrt(1 - std::pow(x - 1, 2)); }
  62. static double easeInOutCirc(double x) {
  63. if(x < 0.5) {
  64. return (1 - std::sqrt(1 - std::pow(2 * x, 2))) / 2;
  65. } else {
  66. return (std::sqrt(1 - std::pow(-2 * x + 2, 2)) + 1) / 2;
  67. }
  68. }
  69. static double easeInBack(double x) {
  70. const double c1 = 1.70158;
  71. const double c3 = c1 + 1;
  72. return c3 * x * x * x - c1 * x * x;
  73. }
  74. static double easeOutBack(double x) {
  75. const double c1 = 1.70158;
  76. const double c3 = c1 + 1;
  77. return 1 + c3 * std::pow(x - 1, 3) + c1 * std::pow(x - 1, 2);
  78. }
  79. static double easeInOutBack(double x) {
  80. const double c1 = 1.70158;
  81. const double c2 = c1 * 1.525;
  82. if(x < 0.5) {
  83. return (std::pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2;
  84. } else {
  85. return (std::pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2;
  86. }
  87. }
  88. static double easeInElastic(double x) {
  89. const double c4 = (2 * kPi) / 3;
  90. if(x == 0) {
  91. return 0;
  92. } else if(x == 1) {
  93. return 1;
  94. } else {
  95. return -std::pow(2, 10 * x - 10) * std::sin((x * 10 - 10.75) * c4);
  96. }
  97. }
  98. static double easeOutElastic(double x) {
  99. const double c4 = (2 * kPi) / 3;
  100. if(x == 0) {
  101. return 0;
  102. } else if(x == 1) {
  103. return 1;
  104. } else {
  105. return std::pow(2, -10 * x) * std::sin((x * 10 - 0.75) * c4) + 1;
  106. }
  107. }
  108. static double easeInOutElastic(double x) {
  109. const double c5 = (2 * kPi) / 4.5;
  110. if(x == 0) {
  111. return 0;
  112. } else if(x == 1) {
  113. return 1;
  114. } else if(x < 0.5) {
  115. return -(std::pow(2, 20 * x - 10) * std::sin((20 * x - 11.125) * c5)) / 2;
  116. } else {
  117. return (std::pow(2, -20 * x + 10) * std::sin((20 * x - 11.125) * c5)) / 2 + 1;
  118. }
  119. }
  120. static double easeOutBounce(double x) {
  121. const double n1 = 7.5625;
  122. const double d1 = 2.75;
  123. if(x < 1 / d1) {
  124. return n1 * x * x;
  125. } else if(x < 2 / d1) {
  126. x -= 1.5 / d1;
  127. return n1 * x * x + 0.75;
  128. } else if(x < 2.5 / d1) {
  129. x -= 2.25 / d1;
  130. return n1 * x * x + 0.9375;
  131. } else {
  132. x -= 2.625 / d1;
  133. return n1 * x * x + 0.984375;
  134. }
  135. }
  136. static double easeInBounce(double x) { return 1 - easeOutBounce(1 - x); }
  137. static double easeInOutBounce(double x) {
  138. return x < 0.5 ? (1 - easeOutBounce(1 - 2 * x)) / 2 : (1 + easeOutBounce(2 * x - 1)) / 2;
  139. }
  140. void add_module_easing(VM* vm) {
  141. PyObject* mod = vm->new_module("easing");
  142. #define EASE(name) \
  143. vm->bind_func(mod, #name, 1, [](VM* vm, ArgsView args) { \
  144. f64 t = CAST(f64, args[0]); \
  145. return VAR(ease##name(t)); \
  146. });
  147. EASE(Linear)
  148. EASE(InSine)
  149. EASE(OutSine)
  150. EASE(InOutSine)
  151. EASE(InQuad)
  152. EASE(OutQuad)
  153. EASE(InOutQuad)
  154. EASE(InCubic)
  155. EASE(OutCubic)
  156. EASE(InOutCubic)
  157. EASE(InQuart)
  158. EASE(OutQuart)
  159. EASE(InOutQuart)
  160. EASE(InQuint)
  161. EASE(OutQuint)
  162. EASE(InOutQuint)
  163. EASE(InExpo)
  164. EASE(OutExpo)
  165. EASE(InOutExpo)
  166. EASE(InCirc)
  167. EASE(OutCirc)
  168. EASE(InOutCirc)
  169. EASE(InBack)
  170. EASE(OutBack)
  171. EASE(InOutBack)
  172. EASE(InElastic)
  173. EASE(OutElastic)
  174. EASE(InOutElastic)
  175. EASE(InBounce)
  176. EASE(OutBounce)
  177. EASE(InOutBounce)
  178. #undef EASE
  179. }
  180. } // namespace pkpy