Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ┐ ┌─┐ ┌─┐ ┌─┐ ┌─┐ ┌─┐ ┌─┐ ┌─┐
- // └─┘ └─┘ └─┘ └─┘ └─┘ └─┘ └─┘ └
- float square_wave(float x)
- {
- return 4.0f * floor(x) - 2.0f * floor(2.0f * x) + 1.0f;
- }
- // ┐ ┌┐ ┌┐ ┌┐ ┌┐ ┌┐ ┌┐ ┌┐
- // └──┘└──┘└──┘└──┘└──┘└──┘└──┘└
- float pulse_wave(float x, float t)
- {
- return 2.0f * static_cast<float>(signbit(x - floor(x) - t)) - 1.0f;
- }
- // ╱│ ╱│ ╱│ ╱│ ╱│ ╱│ ╱│ ╱│ ╱│ ╱│
- // │╱ │╱ │╱ │╱ │╱ │╱ │╱ │╱ │╱
- float sawtooth_wave(float x)
- {
- return 2.0f * (x - floor(0.5f + x));
- }
- // ╲ ╱╲ ╱╲ ╱╲ ╱╲ ╱╲ ╱╲ ╱╲
- // ╲╱ ╲╱ ╲╱ ╲╱ ╲╱ ╲╱ ╲╱
- float triangle_wave(float x)
- {
- return abs(4.0f * fmod(x, 1.0f) - 2.0f) - 1.0f;
- }
- float rectified_sin(float x)
- {
- return 2.0f * abs(sin(x / 2.0f)) - 1.0f;
- }
- float cycloid(float x)
- {
- // Use slightly curtate cycloid parameters, so there's no non-differentiable
- // cusps.
- const float a = 1.0f;
- const float b = 0.95f;
- // Approximates parameter t given the value x by applying the Newton-Raphson
- // method to the equation f(t) = at - bsin(t) - x.
- float t = x;
- for(int i = 0; i < 5; ++i)
- {
- float ft = (a * t) - (b * sin(t)) - x;
- float dft = a - (b * cos(t));
- t -= ft / dft;
- }
- // Now that t is known, insert it into the parametric equation to get y,
- // then remap y to the range [-1,1].
- float y = a - (b * cos(t));
- return y / a - 1.0f;
- }
Advertisement
Add Comment
Please, Sign In to add comment