|
| 1 | +package com.Nxer.TwistSpaceTechnology.util; |
| 2 | + |
| 3 | +/** |
| 4 | + * Copy from com.cleanroommc.modularui.utils.MathUtils |
| 5 | + */ |
| 6 | +public class MathUtils { |
| 7 | + |
| 8 | + public static int clamp(int v, int min, int max) { |
| 9 | + return Math.max(min, Math.min(v, max)); |
| 10 | + } |
| 11 | + |
| 12 | + public static float clamp(float v, float min, float max) { |
| 13 | + return Math.max(min, Math.min(v, max)); |
| 14 | + } |
| 15 | + |
| 16 | + public static double clamp(double v, double min, double max) { |
| 17 | + return Math.max(min, Math.min(v, max)); |
| 18 | + } |
| 19 | + |
| 20 | + public static long clamp(long v, long min, long max) { |
| 21 | + return Math.max(min, Math.min(v, max)); |
| 22 | + } |
| 23 | + |
| 24 | + public static int cycler(int x, int min, int max) { |
| 25 | + return x < min ? max : (x > max ? min : x); |
| 26 | + } |
| 27 | + |
| 28 | + public static float cycler(float x, float min, float max) { |
| 29 | + return x < min ? max : (x > max ? min : x); |
| 30 | + } |
| 31 | + |
| 32 | + public static double cycler(double x, double min, double max) { |
| 33 | + return x < min ? max : (x > max ? min : x); |
| 34 | + } |
| 35 | + |
| 36 | + public static int gridIndex(int x, int y, int size, int width) { |
| 37 | + x = x / size; |
| 38 | + y = y / size; |
| 39 | + |
| 40 | + return x + y * width / size; |
| 41 | + } |
| 42 | + |
| 43 | + public static int gridRows(int count, int size, int width) { |
| 44 | + double x = count * size / (double) width; |
| 45 | + |
| 46 | + return count <= 0 ? 1 : (int) Math.ceil(x); |
| 47 | + } |
| 48 | + |
| 49 | + public static int min(int... values) { |
| 50 | + if (values == null || values.length == 0) throw new IllegalArgumentException(); |
| 51 | + if (values.length == 1) return values[0]; |
| 52 | + if (values.length == 2) return Math.min(values[0], values[1]); |
| 53 | + int min = Integer.MAX_VALUE; |
| 54 | + for (int i : values) { |
| 55 | + if (i < min) { |
| 56 | + min = i; |
| 57 | + } |
| 58 | + } |
| 59 | + return min; |
| 60 | + } |
| 61 | + |
| 62 | + public static int max(int... values) { |
| 63 | + if (values == null || values.length == 0) throw new IllegalArgumentException(); |
| 64 | + if (values.length == 1) return values[0]; |
| 65 | + if (values.length == 2) return Math.max(values[0], values[1]); |
| 66 | + int max = Integer.MIN_VALUE; |
| 67 | + for (int i : values) { |
| 68 | + if (i > max) { |
| 69 | + max = i; |
| 70 | + } |
| 71 | + } |
| 72 | + return max; |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments