clamp method

Vector3 clamp(
  1. Vector3 min,
  2. Vector3 max
)

If this vector's x, y or z value is greater than the max vector's x, y or z value, it is replaced by the corresponding value.

If this vector's x, y or z value is less than the min vector's x, y or z value, it is replaced by the corresponding value.

  • min - the minimum x, y and z values.
  • max - the maximum x, y and z values in the desired range

Implementation

Vector3 clamp(Vector3 min, Vector3 max) {
  // assumes min < max, componentwise

  x = math.max(min.x, math.min(max.x, x));
  y = math.max(min.y, math.min(max.y, y));
  z = math.max(min.z, math.min(max.z, z));

  return this;
}