Smooth 2D Noise Viewer
Perlin and Value Noise
Functions
Helpers.cpp File Reference

Code for helper functions. More...

#include <algorithm>
#include <sstream>
#include "Helpers.h"

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...
 

Function Documentation

◆ clamp()

const float clamp ( float  a,
float  x,
float  b 
)

Clamp between two bounds.

Parameters
aLower bound, assumed to be less than \(\mathsf{b}\).
xValue to be clamped.
bUpper bound, assumed to be greater than \(\mathsf{a}\).
Returns
Closest value to \(\mathsf{x}\) between the lower and upper, bounds (inclusive), that is, \(\mathsf{\max(a, \min(x, b))}\).

Definition at line 67 of file Helpers.cpp.

◆ isPowerOf2()

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.

Parameters
nA number.
Returns
true if the parameter is a power of 2.

Definition at line 94 of file Helpers.cpp.

◆ lerp()

const float lerp ( float  t,
float  a,
float  b 
)

Linear interpolation between two values.

Parameters
tInterpolation fraction, assumed to be in \([0,1]\).
aLower value.
bUpper value.
Returns
A \(\mathsf{t}\) fraction of the way between \(\mathsf{a}\) and \(\mathsf{b}\), that is, \(\mathsf{(1-t)a + tb}\).

Definition at line 56 of file Helpers.cpp.

◆ spline3()

const float spline3 ( float  t)

Compute the cubic spline of a parameter \(t\), that is, \(3t^2 - 2t^3\).

Parameters
tParameter.
Returns
Cubic spline of parameter.

Definition at line 36 of file Helpers.cpp.

◆ spline5()

const float spline5 ( float  t)

Compute the quintic spline of a parameter \(t\), that is, \(10t^3 - 15t^4 + 6t^5\).

Parameters
tParameter.
Returns
Quintic spline of parameter.

Definition at line 45 of file Helpers.cpp.

◆ to_wstring_f()

std::wstring to_wstring_f ( float  x,
size_t  n 
)

Convert a floating point number into a fixed precision wide string.

Parameters
xA floating point number.
nNumber of digits after the decimal point.
Returns
Fixed precision string.

Definition at line 76 of file Helpers.cpp.