The function, from ogremath.h
Code: Select all
/** Clamp a value within an inclusive range. */
template <typename T>
static T Clamp(T val, T minval, T maxval)
{
assert (minval < maxval && "Invalid clamp range");
return std::max(std::min(val, maxval), minval);
}
Code: Select all
assert (minval <= maxval && "Invalid clamp range");
Code: Select all
int n = 15;
int nMin = 3;
int nMax = 3;
n = Math::Clamp( n, nMin, nMax );
But as currently written, I often have to wrap my Math::Clamp calls with a little extra logic to make sure I won't trigger that assert, in places where nMin and nMax are variable values.