fork download
  1. #include "EaseEquations.h"
  2. #include <cmath>
  3.  
  4. const float FloatPI = 3.14159f;
  5.  
  6. //While I wrote the C++ code here, most of the algorithms used were created by Robert Penner.
  7.  
  8. /*
  9.   TERMS OF USE - EASING EQUATIONS
  10.  
  11.   Open source under the BSD License.
  12.  
  13.   Copyright © 2001 Robert Penner
  14.   All rights reserved.
  15.  
  16.   Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are
  17.   met:
  18.  
  19.   - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  20.  
  21.   - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
  22.   documentation and/or other materials provided with the distribution.
  23.  
  24.   - Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software
  25.   without specific prior written permission.
  26.  
  27.   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28.   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  29.   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30.   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31.   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32.   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33.   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  34.   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  35.   OF SUCH DAMAGE.
  36. */
  37.  
  38. float QuadraticEase(float position)
  39. {
  40. //Optimization for end points, which are the most common spots.
  41. if(position == 0.0f || position == 1.0f)
  42. {
  43. return position;
  44. }
  45.  
  46. return (position * position); //PowerEase(position, 2.0f);
  47. }
  48.  
  49. float CubicEase(float position)
  50. {
  51. //Optimization for end points, which are the most common spots.
  52. if(position == 0.0f || position == 1.0f)
  53. {
  54. return position;
  55. }
  56.  
  57. return (position * position * position); //PowerEase(position, 3.0f);
  58. }
  59.  
  60. float QuarticEase(float position)
  61. {
  62. //Optimization for end points, which are the most common spots.
  63. if(position == 0.0f || position == 1.0f)
  64. {
  65. return position;
  66. }
  67.  
  68. return (position * position * position * position); //PowerEase(position, 4.0f);
  69. }
  70.  
  71. float QuinticEase(float position)
  72. {
  73. //Optimization for end points, which are the most common spots.
  74. if(position == 0.0f || position == 1.0f)
  75. {
  76. return position;
  77. }
  78.  
  79. return (position * position * position * position * position); //PowerEase(position, 5.0f);
  80. }
  81.  
  82. float PowerEase(float position, float power)
  83. {
  84. //Optimization for end points, which are the most common spots.
  85. if(position == 0.0f || position == 1.0f)
  86. {
  87. return position;
  88. }
  89.  
  90. return std::pow(position, power);
  91. }
  92.  
  93. float ExponentialEase(float position, float exponent)
  94. {
  95. //Optimization for end points, which are the most common spots.
  96. if(position == 0.0f || position == 1.0f)
  97. {
  98. return position;
  99. }
  100.  
  101. return ((std::exp(exponent * position) - 1.0f) / (std::exp(exponent) - 1.0f));
  102. }
  103.  
  104. float CircleEase(float position)
  105. {
  106. //Optimization for end points, which are the most common spots.
  107. if(position == 0.0f || position == 1.0f)
  108. {
  109. return position;
  110. }
  111.  
  112. return 1.0f - sqrtf(1.0f - std::pow(position, 2.0f));
  113. }
  114.  
  115. float SineEase(float position)
  116. {
  117. //Optimization for end points, which are the most common spots.
  118. if(position == 0.0f || position == 1.0f)
  119. {
  120. return position;
  121. }
  122.  
  123. return -1.0f * std::cos(position * (FloatPI/2.0f)) + 1.0f;
  124. }
  125.  
  126. float BounceEase(float position)
  127. {
  128. //Optimization for end points, which are the most common spots.
  129. if(position == 0.0f || position == 1.0f)
  130. {
  131. return position;
  132. }
  133.  
  134. //Because this equation is EaseOut, we have to invert the input and the output to make it EaseIn.
  135. position = 1.0f - position;
  136.  
  137. //I don't know the purpose this constant serves in the original ease equation.
  138. const float UnknownVar = 7.5625f;
  139. //1.0f for the half-bounce, 1.0f, 0.5f, and 0.25f, for the other bounces.
  140. const float TotalSize = 2.75f;
  141.  
  142. if(position < (1.0f / TotalSize))
  143. {
  144. return 1.0f - (UnknownVar * position * position);
  145. }
  146. else if(position < (2.0f / TotalSize))
  147. {
  148. position -= (1.5f / TotalSize);
  149. return 1.0f - (UnknownVar * position * position + 0.75f);
  150. }
  151. else if(position < (2.5f / TotalSize))
  152. {
  153. position -= (2.25f / TotalSize);
  154. return 1.0f - (UnknownVar * position * position + 0.9375f); //0.75 + (0.75 * 0.25)
  155. }
  156. else
  157. {
  158. position -= (2.625f / TotalSize);
  159. return 1.0f - (UnknownVar * position * position + 0.984375f); //(0.75 + (0.75 * 0.25)) + (0.75f * 0.0625f)
  160. }
  161. }
  162.  
  163. float ElasticEase(float position, float period)
  164. {
  165. //Optimization for end points, which are the most common spots.
  166. if(position == 0.0f || position == 1.0f)
  167. {
  168. return position;
  169. }
  170.  
  171. return -std::pow(2, 10 * (position - 1.0f)) * std::sin(((position - 1.0f) - period / 4.0f) * 2.0f * FloatPI / period);
  172. }
  173.  
  174. float BackEase(float position, float amplitude)
  175. {
  176. //Optimization for end points, which are the most common spots.
  177. if(position == 0.0f || position == 1.0f)
  178. {
  179. return position;
  180. }
  181.  
  182. return std::pow(position, 3.0f) - (position * amplitude * std::sin(position * FloatPI));
  183. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout

Standard output is empty