DMath.clamp(x,a,b);
x - { Scalar or vector to be clamped } a - { Scalar or vector for bottom of clamp range } b - { Scalar or vector for top of clamp range }
Returns x clamped to the range [a,b] as follows:
-
Returns a if x is less than a; else
-
Returns b if x is greater than b; else
-
Returns x otherwise.
For vectors, the returned vector contains the clamped result of each element of the vector x clamped using the respective element of vectors a and b.
clamp implementation for a float scalar.
float clamp(float x, float a, float b)
{
return max(a, min(b, x));
}
Clamp is extremely inexpensive in coding.