fork download
  1. #pragma once
  2. #include <iostream>
  3. #include <math.h>
  4. #include <assert.h>
  5.  
  6.  
  7. //enum of interpolator to create the right one
  8. enum interpolatorType{
  9. linear,
  10. inQuad,
  11. outQuad,
  12. inOutQuad,
  13. inSine,
  14. inCube,
  15. inExpo,
  16. };
  17.  
  18.  
  19.  
  20. class IInterpolator{
  21. protected:
  22. IInterpolator(){};
  23. public:
  24.  
  25. virtual ~IInterpolator(){};
  26. };
  27.  
  28.  
  29. template <class T>
  30. class interpolator : public IInterpolator{
  31. protected:
  32. T &interpolant;
  33. public:
  34. interpolator(T &_interpolant) :
  35. interpolant(_interpolant){};
  36.  
  37. virtual ~interpolator(){};
  38.  
  39. virtual void Interpolate() = 0;
  40. virtual bool Done() = 0;
  41. };
  42.  
  43.  
  44. template <class T>
  45. class nullInterpolator : public interpolator<T>{
  46. private:
  47. T value;
  48. public:
  49. nullInterpolator(T &_interpolant, T _value) : interpolator(_interpolant), value(_value){};
  50. void Interpolate(){
  51. interpolant = value;
  52. }
  53.  
  54. bool Done(){ return true; }
  55.  
  56. ~nullInterpolator(){};
  57. };
  58.  
  59.  
  60. //any class that requires this kind of structure can use IeasingInterpolator as the base class
  61. //to simplify life :P most easing interpolators have the same structure
  62. template <class T, class stepType = double>
  63. class IeasingInterpolator : public interpolator<T>{
  64. protected:
  65. stepType currentTime;
  66. const stepType deltaTime;
  67. const stepType totalTime;
  68. T begin, delta;
  69. const double pi;
  70.  
  71. virtual void _Interpolate() = 0;
  72. public:
  73. IeasingInterpolator(T &_interpolant, T begin, T end, stepType _totalTime, stepType _deltaTime = 1)
  74. : interpolator(_interpolant), pi(3.14159265), totalTime(_totalTime), deltaTime(_deltaTime){
  75.  
  76.  
  77. this->currentTime = 0;
  78. this->begin = begin;
  79. this->delta = end - begin;
  80. this->interpolant = begin;
  81. };
  82.  
  83. void Interpolate(){
  84. currentTime += deltaTime;
  85. currentTime = ::std::max(::std::min(currentTime, totalTime), (stepType)0.0);
  86.  
  87. _Interpolate();
  88. };
  89.  
  90. virtual bool Done(){
  91. int x = 0;
  92. return currentTime >= totalTime;
  93. }
  94.  
  95. virtual ~IeasingInterpolator(){};
  96. };
  97.  
  98.  
  99.  
  100. template <class T, class stepType = double>
  101. class linearInterpolator : public IeasingInterpolator<T, stepType>{
  102. protected:
  103. void _Interpolate(){
  104. interpolant = ((delta * currentTime) / totalTime) + begin;
  105. }
  106.  
  107. public:
  108. linearInterpolator(T &_interpolant, T begin, T end, stepType totalTime, stepType deltaTime = 1)
  109. : IeasingInterpolator(_interpolant, begin, end, totalTime, deltaTime){};
  110.  
  111.  
  112. ~linearInterpolator(){};
  113. };
  114.  
  115.  
  116.  
  117. template <class T, class stepType = double>
  118. class inQuadInterpolator : public IeasingInterpolator<T, stepType>{
  119. protected:
  120. void _Interpolate(){
  121. // t: current time, b: beginning value, c: change in value, d: duration
  122. //t /= d; return c*t*t + b;
  123. T newTime = currentTime / totalTime;
  124. interpolant = (delta * newTime * newTime) + begin;
  125.  
  126. }
  127. public:
  128. inQuadInterpolator(T &_interpolant, T begin, T end, stepType totalTime, stepType deltaTime = 1) :
  129. IeasingInterpolator(_interpolant, begin, end, totalTime, deltaTime){};
  130.  
  131. ~inQuadInterpolator(){};
  132.  
  133. };
  134.  
  135.  
  136.  
  137. template <class T, class stepType = double>
  138. class outQuadInterpolator : public IeasingInterpolator<T, stepType>{
  139. protected:
  140. void _Interpolate(){
  141. // t: current time, b: beginning value, c: change in value, d: duration
  142. //-c*t*t/(d*d) + 2*c*t/d + b;
  143. interpolant = (-1 * delta * currentTime * currentTime / (totalTime * totalTime)) +
  144. ((2 * delta * currentTime) / totalTime) + begin ;
  145.  
  146. }
  147.  
  148. public:
  149. outQuadInterpolator (T &_interpolant, T begin, T end, stepType totalTime, stepType deltaTime = 1) :
  150. IeasingInterpolator(_interpolant, begin, end, totalTime, deltaTime){};
  151.  
  152. ~outQuadInterpolator(){};
  153. };
  154.  
  155.  
  156. template <class T, class stepType = double>
  157. class inOutQuadInterpolator : public IeasingInterpolator<T, stepType>{
  158. protected:
  159. void _Interpolate(){
  160. // t: current time, b: beginning value, c: change in value, d: duration
  161.  
  162. /*
  163. if (t < d/2) return 2*c*t*t/(d*d) + b;
  164. var ts = t - d/2;
  165. return -2*c*ts*ts/(d*d) + 2*c*ts/d + c/2 + b;
  166. */
  167.  
  168. if(currentTime < (totalTime / 2.0)){
  169. interpolant = (2 * delta * currentTime * currentTime /(totalTime * totalTime)) + begin;
  170. }else{
  171. stepType ts = currentTime - (totalTime / 2.0);
  172.  
  173. interpolant = (-2 * delta * ts * ts / (totalTime * totalTime)) +
  174. (2 * delta * (ts / totalTime)) + begin;
  175.  
  176. }
  177. }
  178. public:
  179. inOutQuadInterpolator (T &_interpolant, T begin, T end, stepType totalTime, stepType deltaTime = 1) :
  180. IeasingInterpolator(_interpolant, begin, end, totalTime, deltaTime){};
  181.  
  182. ~inOutQuadInterpolator(){};
  183. };
  184.  
  185.  
  186.  
  187. template <class T, class stepType = double>
  188. class inSineInterpolator: public IeasingInterpolator<T, stepType>{
  189. protected:
  190. void _Interpolate(){
  191. // t: current time, b: beginning value, c: change in value, d: duration
  192.  
  193. /*
  194. return -c * Math.cos(t/d * Math.PI/2) + c + b;
  195. */
  196. interpolant = (-delta * cos( (currentTime / totalTime) * (pi / 2.0))) + delta + begin;
  197. }
  198. public:
  199. inSineInterpolator(T &_interpolant, T begin, T end, stepType totalTime, stepType deltaTime = 1) :
  200. IeasingInterpolator(_interpolant, begin, end, totalTime, deltaTime){};
  201.  
  202. ~inSineInterpolator(){};
  203. };
  204.  
  205. template <class T, class stepType = double>
  206. class inCubeInterpolator: public IeasingInterpolator<T, stepType>{
  207. protected:
  208. void _Interpolate(){
  209. // t: current time, b: beginning value, c: change in value, d: duration
  210.  
  211. /*
  212. t /= d;
  213. return c*t*t*t + b;
  214. */
  215. T newT = currentTime / totalTime;
  216.  
  217. interpolant = delta * pow(newT, 3) + begin;
  218. //interpolant = (delta * cos( (currentTime / totalTime) * (pi / 2.0))) + delta + begin;
  219. }
  220. public:
  221. inCubeInterpolator(T &_interpolant, T begin, T end, stepType totalTime, stepType deltaTime = 1) :
  222. IeasingInterpolator(_interpolant, begin, end, totalTime, deltaTime){};
  223.  
  224. ~inCubeInterpolator(){};
  225. };
  226.  
  227.  
  228. template <class T, class stepType = double>
  229. class inExpoInterpolator: public IeasingInterpolator<T, stepType>{
  230. protected:
  231. void _Interpolate(){
  232. // t: current time, b: beginning value, c: change in value, d: duration
  233.  
  234. /*
  235. return c * Math.pow( 2, 10 * (t/d - 1) ) + b;
  236. */
  237. interpolant = delta * pow(2, 10 * (currentTime / totalTime - 1)) + begin;
  238. //interpolant = (delta * cos( (currentTime / totalTime) * (pi / 2.0))) + delta + begin;
  239. }
  240. public:
  241. inExpoInterpolator(T &_interpolant, T begin, T end, stepType totalTime, stepType deltaTime = 1) :
  242. IeasingInterpolator(_interpolant, begin, end, totalTime, deltaTime){};
  243.  
  244. ~inExpoInterpolator(){};
  245. };
  246.  
  247.  
  248. //helper functions--------------------------------------------------------------------------------------
  249.  
  250. template<typename T, typename stepType>
  251. IeasingInterpolator<T, stepType>* createEasingInterpolator(interpolatorType type,
  252. T &_interpolant, T begin, T end, stepType totalTime, stepType deltaTime = (double)1.0){
  253.  
  254. switch(type)
  255. {
  256. case linear:
  257. return new linearInterpolator<T, stepType>(_interpolant, begin, end, totalTime, deltaTime);
  258. break;
  259.  
  260. case inQuad:
  261. return new inQuadInterpolator<T, stepType>(_interpolant, begin, end, totalTime, deltaTime);
  262. break;
  263.  
  264. case outQuad:
  265. return new outQuadInterpolator<T, stepType>(_interpolant, begin, end, totalTime, deltaTime);
  266. break;
  267.  
  268. case inOutQuad:
  269. return new inOutQuadInterpolator<T, stepType>(_interpolant, begin, end, totalTime, deltaTime);
  270. break;
  271. case inSine:
  272. return new inSineInterpolator<T, stepType>(_interpolant, begin, end, totalTime, deltaTime);
  273. break;
  274.  
  275. case inCube:
  276. return new inCubeInterpolator<T, stepType>(_interpolant, begin, end, totalTime, deltaTime);
  277. break;
  278. case inExpo:
  279. return new inExpoInterpolator<T, stepType>(_interpolant, begin, end, totalTime, deltaTime);
  280. break;
  281. default:
  282. assert(false && "wrong interpolant type given to CreateInterpolator");
  283. return new linearInterpolator<T, stepType>(_interpolant, begin, end, totalTime, deltaTime);
  284. }
  285. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout

Standard output is empty