@ Pure public static double clampCyclic ( double value , double min , double max ) { assert min <= max : AssertMessages . lowerEqualParameters ( 1 , min , 2 , max ) ; if ( Double . isNaN ( max ) || Double . isNaN ( min ) || Double . isNaN ( max ) ) { return Double . NaN ; } if ( value < min ) { final double perimeter = max - min ; final double nvalue = min - value ; double rest = perimeter - ( nvalue % perimeter ) ; if ( rest >= perimeter ) { rest -= perimeter ; } return min + rest ; } else if ( value >= max ) { final double perimeter = max - min ; final double nvalue = value - max ; final double rest = nvalue % perimeter ; return min + rest ; } return value ; }
Clamp the given value to fit between the min and max values according to a cyclic heuristic . If the given value is not between the minimum and maximum values the replied value is modulo the min - max range .