clampRotate method

T clampRotate(
  1. T minValue,
  2. T maxValue
)

Clamps the value between minValue and maxValue and rotates it if necessary.

If the value is greater than maxValue, it will be rotated back to minValue and continue counting up. If the value is less than minValue, it will be rotated forward to maxValue and continue counting down. If the value is within the range, it will be returned as is.

Returns the clamped and rotated value.

Implementation

T clampRotate(T minValue, T maxValue) {
  var correct = minValue.compareAndSwap(maxValue);
  minValue = correct.first;
  maxValue = correct.last;
  var v = this;
  while (v > maxValue) {
    v = minValue + (v - maxValue) - 1 as T;
  }
  while (v < minValue) {
    v = maxValue - (minValue - v) + 1 as T;
  }
  return v;
}