Vavassor

waves

Aug 31st, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. // ┐ ┌─┐ ┌─┐ ┌─┐ ┌─┐ ┌─┐ ┌─┐ ┌─┐
  2. // └─┘ └─┘ └─┘ └─┘ └─┘ └─┘ └─┘ └
  3. float square_wave(float x)
  4. {
  5.     return 4.0f * floor(x) - 2.0f * floor(2.0f * x) + 1.0f;
  6. }
  7.  
  8. // ┐  ┌┐  ┌┐  ┌┐  ┌┐  ┌┐  ┌┐  ┌┐
  9. // └──┘└──┘└──┘└──┘└──┘└──┘└──┘└
  10. float pulse_wave(float x, float t)
  11. {
  12.     return 2.0f * static_cast<float>(signbit(x - floor(x) - t)) - 1.0f;
  13. }
  14.  
  15. // ╱│ ╱│ ╱│ ╱│ ╱│ ╱│ ╱│ ╱│ ╱│ ╱│
  16. //  │╱ │╱ │╱ │╱ │╱ │╱ │╱ │╱ │╱
  17. float sawtooth_wave(float x)
  18. {
  19.     return 2.0f * (x - floor(0.5f + x));
  20. }
  21.  
  22. // ╲  ╱╲  ╱╲  ╱╲  ╱╲  ╱╲  ╱╲  ╱╲
  23. //  ╲╱  ╲╱  ╲╱  ╲╱  ╲╱  ╲╱  ╲╱
  24. float triangle_wave(float x)
  25. {
  26.     return abs(4.0f * fmod(x, 1.0f) - 2.0f) - 1.0f;
  27. }
  28.  
  29. float rectified_sin(float x)
  30. {
  31.     return 2.0f * abs(sin(x / 2.0f)) - 1.0f;
  32. }
  33.  
  34. float cycloid(float x)
  35. {
  36.     // Use slightly curtate cycloid parameters, so there's no non-differentiable
  37.     // cusps.
  38.     const float a = 1.0f;
  39.     const float b = 0.95f;
  40.     // Approximates parameter t given the value x by applying the Newton-Raphson
  41.     // method to the equation f(t) = at - bsin(t) - x.
  42.     float t = x;
  43.     for(int i = 0; i < 5; ++i)
  44.     {
  45.         float ft = (a * t) - (b * sin(t)) - x;
  46.         float dft = a - (b * cos(t));
  47.         t -= ft / dft;
  48.     }
  49.     // Now that t is known, insert it into the parametric equation to get y,
  50.     // then remap y to the range [-1,1].
  51.     float y = a - (b * cos(t));
  52.     return y / a - 1.0f;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment