Minestom/src/main/java/net/minestom/server/utils/MathUtils.java

106 lines
3.2 KiB
Java
Raw Normal View History

2020-04-24 03:25:58 +02:00
package net.minestom.server.utils;
2019-08-24 20:34:01 +02:00
import net.minestom.server.utils.validate.Check;
2021-07-30 18:33:37 +02:00
import org.jetbrains.annotations.ApiStatus;
@ApiStatus.Internal
public final class MathUtils {
private MathUtils() {
}
2019-08-24 20:34:01 +02:00
2019-08-25 20:03:43 +02:00
public static int square(int num) {
return num * num;
}
2019-08-24 20:34:01 +02:00
public static float square(float num) {
return num * num;
}
public static double square(double num) {
return num * num;
}
2020-04-13 17:17:21 +02:00
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
2020-08-19 16:19:18 +02:00
final long factor = (long) Math.pow(10, places);
2020-04-13 17:17:21 +02:00
value = value * factor;
long tmp = Math.round(value);
return (double) tmp / factor;
}
public static float round(float value, int places) {
if (places < 0) throw new IllegalArgumentException();
2020-08-19 16:19:18 +02:00
final long factor = (long) Math.pow(10, places);
2020-04-13 17:17:21 +02:00
value = value * factor;
long tmp = Math.round(value);
return (float) tmp / factor;
}
public static Direction getHorizontalDirection(float yawInDegrees) {
// +45f gives a 90° angle for the direction (-1° and 1° are towards the same direction)
2020-05-15 18:03:28 +02:00
int directionIndex = (int) Math.floor(((yawInDegrees + 45f) / 90f));
if (directionIndex < 0) {
directionIndex = (-directionIndex) % Direction.HORIZONTAL.length;
2020-05-15 18:03:28 +02:00
directionIndex = Direction.HORIZONTAL.length - directionIndex;
}
directionIndex %= Direction.HORIZONTAL.length;
return Direction.HORIZONTAL[directionIndex];
}
2020-06-01 18:57:16 +02:00
public static boolean isBetween(byte number, byte min, byte max) {
return number >= min && number <= max;
}
2020-05-15 18:03:28 +02:00
public static boolean isBetween(int number, int min, int max) {
return number >= min && number <= max;
}
public static boolean isBetween(double number, double min, double max) {
return number >= min && number <= max;
}
2020-05-15 18:03:28 +02:00
public static boolean isBetween(float number, float min, float max) {
return number >= min && number <= max;
}
public static boolean isBetweenUnordered(double number, double compare1, double compare2) {
if (compare1 > compare2) {
return isBetween(number, compare2, compare1);
} else {
return isBetween(number, compare1, compare2);
}
}
2021-01-08 23:47:31 +01:00
public static boolean isBetweenUnordered(float number, float compare1, float compare2) {
if (compare1 > compare2) {
return isBetween(number, compare2, compare1);
} else {
return isBetween(number, compare1, compare2);
}
}
2020-08-14 00:50:57 +02:00
public static int clamp(int value, int min, int max) {
return Math.min(Math.max(value, min), max);
}
public static float clamp(float value, float min, float max) {
return Math.min(Math.max(value, min), max);
}
public static double clamp(double value, double min, double max) {
return Math.min(Math.max(value, min), max);
2020-08-14 00:50:57 +02:00
}
2020-08-18 03:11:48 +02:00
public static double mod(final double a, final double b) {
return (a % b + b) % b;
}
public static int bitsToRepresent(int n) {
Check.argCondition(n < 1, "n must be greater than 0");
return Integer.SIZE - Integer.numberOfLeadingZeros(n);
}
2019-08-24 20:34:01 +02:00
}