Skip to content

Commit

Permalink
Merge pull request #603 from Nxer/misc
Browse files Browse the repository at this point in the history
add utils
  • Loading branch information
Nxer authored Aug 6, 2024
2 parents 0ad4090 + a2ea878 commit 303fc9a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

import com.Nxer.TwistSpaceTechnology.common.block.BasicBlocks;
import com.Nxer.TwistSpaceTechnology.common.machine.multiMachineClasses.TT_MultiMachineBase_EM;
import com.Nxer.TwistSpaceTechnology.util.MathUtils;
import com.Nxer.TwistSpaceTechnology.util.TextLocalization;
import com.cleanroommc.modularui.utils.MathUtils;
import com.github.technus.tectech.thing.casing.TT_Container_Casings;
import com.github.technus.tectech.thing.metaTileEntity.hatch.GT_MetaTileEntity_Hatch_DynamoMulti;
import com.github.technus.tectech.thing.metaTileEntity.hatch.GT_MetaTileEntity_Hatch_DynamoTunnel;
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/com/Nxer/TwistSpaceTechnology/util/MathUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.Nxer.TwistSpaceTechnology.util;

/**
* Copy from com.cleanroommc.modularui.utils.MathUtils
*/
public class MathUtils {

public static int clamp(int v, int min, int max) {
return Math.max(min, Math.min(v, max));
}

public static float clamp(float v, float min, float max) {
return Math.max(min, Math.min(v, max));
}

public static double clamp(double v, double min, double max) {
return Math.max(min, Math.min(v, max));
}

public static long clamp(long v, long min, long max) {
return Math.max(min, Math.min(v, max));
}

public static int cycler(int x, int min, int max) {
return x < min ? max : (x > max ? min : x);
}

public static float cycler(float x, float min, float max) {
return x < min ? max : (x > max ? min : x);
}

public static double cycler(double x, double min, double max) {
return x < min ? max : (x > max ? min : x);
}

public static int gridIndex(int x, int y, int size, int width) {
x = x / size;
y = y / size;

return x + y * width / size;
}

public static int gridRows(int count, int size, int width) {
double x = count * size / (double) width;

return count <= 0 ? 1 : (int) Math.ceil(x);
}

public static int min(int... values) {
if (values == null || values.length == 0) throw new IllegalArgumentException();
if (values.length == 1) return values[0];
if (values.length == 2) return Math.min(values[0], values[1]);
int min = Integer.MAX_VALUE;
for (int i : values) {
if (i < min) {
min = i;
}
}
return min;
}

public static int max(int... values) {
if (values == null || values.length == 0) throw new IllegalArgumentException();
if (values.length == 1) return values[0];
if (values.length == 2) return Math.max(values[0], values[1]);
int max = Integer.MIN_VALUE;
for (int i : values) {
if (i > max) {
max = i;
}
}
return max;
}

}

0 comments on commit 303fc9a

Please sign in to comment.