Package me.autobot.lib.math
Class Mathf
java.lang.Object
me.autobot.lib.math.Mathf
A class that contains mathematical functions that are not included in the Math class.
-
Constructor Summary
ConstructorsConstructorDescriptionMathf()
Tries to instantiate a Mathf object, but throws an exception because it is a utility class. -
Method Summary
Modifier and TypeMethodDescriptionstatic int[]
allPos
(byte[] n) Takes in an array of bytes and returns the equivalent positive numbers.static int
allPos
(int n) Takes in a number between -127 and 128 (a byte) and returns the equivalent positive number.static int[]
allPos
(int[] n) Takes in an array of numbers between -127 and 128 (bytes) and returns the equivalent positive numbers.static double
clamp
(double n, double min, double max) Returns the value of the given number clamped between the given minimum and maximum values.static boolean
close
(double a, double b, double tolerance) Returns if two numbers are equal within a certain tolerance.static double
cos
(double r) Returns the cosine of the given angle, but without any floating point errors.static double
distanceBoxParticle2D
(double x, double y, double x_min, double y_min, double x_max, double y_max) Returns the signed distance between a point and a box in 2D space.static float
getFloatFromBytes
(int[] bytes, int offset, int length, boolean reversed) Gets the float value of a byte.static int
getIntFromBytes
(int[] bytes, int offset, int length, boolean reversed) Gets the integer value of a byte.static double
hypot
(double x, double y) Returns the hypotenuse of the given sides.static double
map
(double n, double premin, double premax, double postmin, double postmax) Returns a value that is linearly interpolated between the given minimum and maximum values.static double
max
(double... v) Return the maximum value of the given numbers.static double
min
(double... v) Returns the minimum value of the given numbers.static double
normalizeAngle
(double radians) Normalizes an angle to be between 0 and 2pi.static double
sin
(double r) Returns the sine of the given angle, but without any floating point errors.static byte[]
slice
(byte[] arr, int start, int end) Slices a byte array.static int[]
slice
(int[] arr, int start, int end) Slices an int array.
-
Constructor Details
-
Mathf
public Mathf()Tries to instantiate a Mathf object, but throws an exception because it is a utility class. Please use the static methods instead.
-
-
Method Details
-
clamp
public static double clamp(double n, double min, double max) Returns the value of the given number clamped between the given minimum and maximum values.- Parameters:
n
- The number to clamp. Can be any number. If the number is less than the minimum value, the minimum value is returned. If the number is greater than the maximum value, the maximum value is returned. If the number is between the minimum and maximum values, the number is returned.min
- The minimum value.max
- The maximum value.- Returns:
- The clamped value.
-
map
public static double map(double n, double premin, double premax, double postmin, double postmax) Returns a value that is linearly interpolated between the given minimum and maximum values.- Parameters:
n
- The number to interpolate. Can be any number.premin
- The minimum value of the number. Can be any number.premax
- The maximum value of the number. Can be any number greater than premin.postmin
- The minimum value of the interpolation. Can be any number.postmax
- The maximum value of the interpolation. Can be any number greater than postmin.- Returns:
- The interpolated value.
-
max
public static double max(double... v) Return the maximum value of the given numbers.- Parameters:
v
- The numbers to compare. Can be any number of arguments. If no arguments are given, 0 is returned.- Returns:
- The maximum value of the given numbers.
-
min
public static double min(double... v) Returns the minimum value of the given numbers.- Parameters:
v
- The numbers to compare. Can be any number of arguments. If no arguments are given, 0 is returned.- Returns:
- The minimum value of the given numbers.
-
hypot
public static double hypot(double x, double y) Returns the hypotenuse of the given sides.- Parameters:
x
- The length of the first side. Can be any number.y
- The length of the second side. Can be any number.- Returns:
- The length of the hypotenuse.
-
distanceBoxParticle2D
public static double distanceBoxParticle2D(double x, double y, double x_min, double y_min, double x_max, double y_max) Returns the signed distance between a point and a box in 2D space.- Parameters:
x
- The x-coordinate of the point. Can be any number.y
- The y-coordinate of the point. Can be any number.x_min
- The minimum x-coordinate of the box. Can be any number. Must be less than x_max.y_min
- The minimum y-coordinate of the box. Can be any number. Must be less than y_max.x_max
- The maximum x-coordinate of the box. Can be any number. Must be greater than x_min.y_max
- The maximum y-coordinate of the box. Can be any number. Must be greater than y_min.- Returns:
- The signed distance between the point and the box.
-
cos
public static double cos(double r) Returns the cosine of the given angle, but without any floating point errors.- Parameters:
r
- The angle in radians. Can be any number.- Returns:
- The cosine of the angle.
-
sin
public static double sin(double r) Returns the sine of the given angle, but without any floating point errors.- Parameters:
r
- The angle in radians. Can be any number.- Returns:
- The sine of the angle.
-
allPos
public static int allPos(int n) Takes in a number between -127 and 128 (a byte) and returns the equivalent positive number.- Parameters:
n
- The number to convert.- Returns:
- The equivalent positive number.
-
allPos
public static int[] allPos(int[] n) Takes in an array of numbers between -127 and 128 (bytes) and returns the equivalent positive numbers.- Parameters:
n
- The numbers to convert.- Returns:
- The equivalent positive numbers.
-
allPos
public static int[] allPos(byte[] n) Takes in an array of bytes and returns the equivalent positive numbers.- Parameters:
n
- The numbers to convert.- Returns:
- The equivalent positive numbers.
-
slice
public static byte[] slice(byte[] arr, int start, int end) Slices a byte array.- Parameters:
arr
- The array to slice.start
- The start index of the slice.end
- The end index of the slice.- Returns:
- The sliced array.
-
slice
public static int[] slice(int[] arr, int start, int end) Slices an int array.- Parameters:
arr
- The array to slice.start
- The start index of the slice.end
- The end index of the slice.- Returns:
- The sliced array.
-
getIntFromBytes
public static int getIntFromBytes(int[] bytes, int offset, int length, boolean reversed) Gets the integer value of a byte.- Parameters:
bytes
- The byte to convert.offset
- The offset of the byte.length
- The length of the byte.reversed
- Whether the byte is reversed.- Returns:
- The integer value of the byte.
-
getFloatFromBytes
public static float getFloatFromBytes(int[] bytes, int offset, int length, boolean reversed) Gets the float value of a byte.- Parameters:
bytes
- The byte to convert.offset
- The offset of the byte.length
- The length of the byte.reversed
- Whether the byte is reversed.- Returns:
- The float value of the byte.
-
normalizeAngle
public static double normalizeAngle(double radians) Normalizes an angle to be between 0 and 2pi.- Parameters:
radians
- The angle to normalize.- Returns:
- The normalized angle.
-
close
public static boolean close(double a, double b, double tolerance) Returns if two numbers are equal within a certain tolerance.- Parameters:
a
- The first number.b
- The second number.tolerance
- The tolerance.- Returns:
- If the two numbers are equal within the tolerance.
-