TurtleBrains  0.2.1
High quality, portable, C++ API for native application and game development.
tb_interpolation.h
1 
9 #ifndef _TurtleBrains_Interpolation_h_
10 #define _TurtleBrains_Interpolation_h_
11 
12 namespace TurtleBrains
13 {
14  namespace Math
15  {
16  namespace Interpolation
17  {
18 
26  inline float Linear(float percentage)
27  {
28  return percentage;
29  }
30 
44  template <typename T> T Linear(float percentage, const T& start, const T& final)
45  {
46  //return (p * a) + (b * (1 - p));
47  return start + ((final - start) * percentage);
48  }
49 
58  inline float SmoothStep(float percentage)
59  {
60  return (percentage * percentage * (3.0f - 2.0f * percentage));
61  }
62 
76  template <typename T> T SmoothStep(float percentage, const T& start, const T& end)
77  {
78  const float smoothPercentage = SmoothStep(percentage);
79  return (start * smoothPercentage) + (end * (1.0f - smoothPercentage));
80  }
81 
90  inline float Squared(float percentage)
91  {
92  return percentage * percentage;
93  }
94 
108  template <typename T> T Squared(float percentage, const T& start, const T& end)
109  {
110  const float squaredPercentage = Squared(percentage);
111  return (squaredPercentage * start) + (end * (1.0f - squaredPercentage));
112  }
113 
122  inline float InverseSquared(float percentage)
123  {
124  return 1.0f - ((1.0f - percentage) * (1.0f - percentage));
125  }
126 
140  template <typename T> T InverseSquared(float percentage, const T& start, const T& end)
141  {
142  const float inverseSquared = InverseSquared(percentage);
143  return (inverseSquared * start) + (end * (1.0f - inverseSquared));
144  }
145 
164  template <typename T> T CubicBezier(float percentage, const T& a, const T& b, const T& c, const T& d)
165  {
166  const T tempA = Linear(percentage, a, c);
167  const T tempB = Linear(percentage, c, d);
168  const T tempC = Linear(percentage, d, b);
169 
170  const T tempAA = Linear(percentage, tempA, tempB);
171  const T tempBB = Linear(percentage, tempB, tempC);
172 
173  return Linear(percentage, tempAA, tempBB);
174  }
175 
190  template <typename T> T CubicBezierTangent(float percentage, const T& a, const T& b, const T& c, const T& d)
191  { //Source: http://stackoverflow.com/questions/4089443/find-the-tangent-of-a-point-on-a-cubic-bezier-curve-on-an-iphone
192 
193  const T c1(d - (3.0f * c) + (3.0f * b) - a);
194  const T c2((3.0f * c) - (6.0f * b) + (3.0f * a));
195  const T c3((3.0f * b) - (3.0f * a));
196  //const float c4(a);
197 
198  return ((c1 * (3.0f * percentage * percentage)) + (c2 * (2.0f * percentage)) + c3);
199  }
200 
201  }; /* namespace Interpolation */
202  }; /* namespace Math */
203 }; /* namespace TurtleBrains */
204 
205 namespace tbMath = TurtleBrains::Math;
206 
207 #endif /* _TurtleBrains_Interpolation_h_ */
Contains objects and functions for dealing with Vector and Matrix math.
Contains all functions, classes and helpers related to game/application development written by Tim "B...
Definition: tb_application_dialog.h:21