Smooth 2D Noise Viewer
Perlin and Value Noise
|
Code for helper functions. More...
Go to the source code of this file.
Functions | |
const float | spline3 (float t) |
Cubic spline. More... | |
const float | spline5 (float t) |
Quintic spline. More... | |
const float | lerp (float t, float a, float b) |
Linear interpolation. More... | |
const float | clamp (float a, float x, float b) |
Clamp between two values. More... | |
std::wstring | to_wstring_f (float x, size_t n) |
Float to fixed precision wstring. More... | |
const bool | isPowerOf2 (size_t n) |
Power of 2 test. More... | |
const float clamp | ( | float | a, |
float | x, | ||
float | b | ||
) |
Clamp between two bounds.
a | Lower bound, assumed to be less than \(\mathsf{b}\). |
x | Value to be clamped. |
b | Upper bound, assumed to be greater than \(\mathsf{a}\). |
Definition at line 67 of file Helpers.cpp.
const bool isPowerOf2 | ( | size_t | n | ) |
Test whether an unsigned integer is a power of 2. This is a sneaky one. It relies pm the fact that n
is a power of 2 iff n
is nonzero and n & (n - 1)
is zero. To see this, note that if \(n = 2^k\), then the binary representation of \(n\) is a one followed by \(k\) zeros and that of \(n-1\) is a zero followed by \(k\) ones. Clearly the bit-wise conjunction of the two is zero. Conversely, if \(n\) is not a power of two, then the binary representations of \(n\) and \(n-1\) will share one-bits in the same position so their conjunction will be non-zero.
n | A number. |
Definition at line 94 of file Helpers.cpp.
const float lerp | ( | float | t, |
float | a, | ||
float | b | ||
) |
Linear interpolation between two values.
t | Interpolation fraction, assumed to be in \([0,1]\). |
a | Lower value. |
b | Upper value. |
Definition at line 56 of file Helpers.cpp.
const float spline3 | ( | float | t | ) |
Compute the cubic spline of a parameter \(t\), that is, \(3t^2 - 2t^3\).
t | Parameter. |
Definition at line 36 of file Helpers.cpp.
const float spline5 | ( | float | t | ) |
Compute the quintic spline of a parameter \(t\), that is, \(10t^3 - 15t^4 + 6t^5\).
t | Parameter. |
Definition at line 45 of file Helpers.cpp.
std::wstring to_wstring_f | ( | float | x, |
size_t | n | ||
) |
Convert a floating point number into a fixed precision wide string.
x | A floating point number. |
n | Number of digits after the decimal point. |
Definition at line 76 of file Helpers.cpp.