-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #603 from Nxer/misc
add utils
- Loading branch information
Showing
2 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/com/Nxer/TwistSpaceTechnology/util/MathUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |