A class that contains utility methods related to numbers. supplement utilities for #java.lang.Math
| 13 | * supplement utilities for #java.lang.Math |
| 14 | */ |
| 15 | public final class MathUtils |
| 16 | { |
| 17 | private static final Random random = new Random(); |
| 18 | |
| 19 | private static final float DEG_TO_RAD = (float)(Math.PI / 180); |
| 20 | private static final float RAD_TO_DEG = (float)(180 / Math.PI); |
| 21 | |
| 22 | private MathUtils() |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | public static int clamp(int amount, int low, int high) |
| 27 | { |
| 28 | if(BuildConfig.DEBUG && low > high) |
| 29 | throw new InvalidParameterException("low > high"); |
| 30 | return amount < low ? low : (amount > high ? high : amount); |
| 31 | } |
| 32 | |
| 33 | public static long clamp(long amount, long low, long high) |
| 34 | { |
| 35 | if(BuildConfig.DEBUG && low > high) |
| 36 | throw new InvalidParameterException("low > high"); |
| 37 | return amount < low ? low : (amount > high ? high : amount); |
| 38 | } |
| 39 | |
| 40 | public static float clamp(float amount, float low, float high) |
| 41 | { |
| 42 | if(BuildConfig.DEBUG && low > high) |
| 43 | throw new InvalidParameterException("low > high"); |
| 44 | return amount < low ? low : (amount > high ? high : amount); |
| 45 | } |
| 46 | |
| 47 | public static float clamp(float amount) |
| 48 | { |
| 49 | if(amount < 0) |
| 50 | return 0; |
| 51 | else if(amount > 1) |
| 52 | return 1; |
| 53 | else return amount; |
| 54 | } |
| 55 | |
| 56 | // with modulation fall in range [low, high] |
| 57 | public static int wrap(int amount, int low, int high) |
| 58 | { |
| 59 | if(BuildConfig.DEBUG && low >= high) |
| 60 | throw new InvalidParameterException("low >= high"); |
| 61 | |
| 62 | int range = high - low + 1; |
| 63 | if(amount < low) |
| 64 | amount += range *((low - amount)/range + 1); |
| 65 | |
| 66 | return low + (amount - low)%range; |
| 67 | } |
| 68 | |
| 69 | public static float max(float a, float b, float c) |
| 70 | { |
| 71 | // return Math.max(Math.max(a, b), c); // take consider NAN condition |
| 72 | return a > b ? (a > c ? a : c) : (b > c ? b : c); |
nothing calls this directly
no outgoing calls
no test coverage detected