Package net.risingworld.api.utils
Class Utils.MathUtils
java.lang.Object
net.risingworld.api.utils.Utils.MathUtils
- Enclosing class:
Utils
Contains useful math functions. Unlike the regular Java Math class, this
class uses float values and an improved random number generator,
resulting in better performance. Unless you really need double precision,
it's highly recommendable to use this class.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final float
Multiply with this factor to convert a degree value to radiansstatic final float
Half PI value as a float (90 degrees)static final float
PI value as a float (180 degrees)static final float
Quarter PI value as a float (45 degrees)static final float
Multiply with this factor to convert a radian value to degreesstatic final float
Two PI value as a float (360 degrees) -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic float
abs
(float value) Gets the absolute value of afloat
value.static int
absInt
(int value) Gets the absolute value of anint
value.static float
clamp
(float min, float max, float value) Clamps the value between min and max.static int
clamp
(int min, int max, int value) Clamps the value between min and max.static float
degreeToRadian
(float degree) Converts a degree value to radians.static float
distance
(float x1, float x2) Gets the distance between two values in 1d space (determined by x coordinate).static float
distance
(float x1, float y1, float x2, float y2) Gets the distance between two points in 2d space (determined by x and y coordinate).static float
distance
(float x1, float y1, float z1, float x2, float y2, float z2) Gets the distance between two points in 3d space (determined by x, y and z coordinate).static float
distanceSqr
(float x1, float y1, float z1, float x2, float y2, float z2) static float
dot
(float x1, float y1, float z1, float x2, float y2, float z2) static int
getGCD
(int... input) Gets the greatest common divisor of several int values.static int
getGCD
(int a, int b) Gets the greatest common divisor of two int values.static int
getNextPowerOfTwo
(int value) Gets the next power of two value (rounded up).static float
length
(float x, float y) static float
length
(float x, float y, float z) static float
max
(float x, float y) static float
max
(float x, float y, float z) static int
max
(int x, int y) static int
max
(int x, int y, int z) static float
min
(float x, float y) Returns the smallest value.static float
min
(float x, float y, float z) Returns the smallest value.static int
min
(int x, int y) Returns the smallest (integer) value.static int
min
(int x, int y, int z) Returns the smallest (integer) value.static float
mod
(float x, float y) static int
mod
(int x, int y) static boolean
Gets a random boolean value (true or false).static float
Gets a random float value between 0 and 1static int
nextRandomInt
(int min, int max) Gets a random int value between min (inclusive) and max (inclusive).static float
radianToDegree
(float radian) Converts a radian value to degree.static float
round
(float value, int digit) Rounds a float value to a given decimal place.static float
saturate
(float value) Clamps a value between 0 and 1.static float
smoothstep
(float min, float max, float x) Performs a smooth Hermite interpolation between 0 and 1.static float
sqr
(float value) Gets the value squared (value ^ 2).static float
sqrt
(float value) Gets the square root of a given value.static float
step
(float min, float x) If x is less than min, 0 is returned, else 1.
-
Field Details
-
PI
public static final float PIPI value as a float (180 degrees)- See Also:
-
HALF_PI
public static final float HALF_PIHalf PI value as a float (90 degrees)- See Also:
-
QUARTER_PI
public static final float QUARTER_PIQuarter PI value as a float (45 degrees)- See Also:
-
TWO_PI
public static final float TWO_PITwo PI value as a float (360 degrees)- See Also:
-
DEG_TO_RAD
public static final float DEG_TO_RADMultiply with this factor to convert a degree value to radians- See Also:
-
RAD_TO_DEG
public static final float RAD_TO_DEGMultiply with this factor to convert a radian value to degrees- See Also:
-
-
Constructor Details
-
MathUtils
public MathUtils()
-
-
Method Details
-
abs
public static float abs(float value) Gets the absolute value of afloat
value.- Parameters:
value
- the argument whose absolute value is to be determined.- Returns:
- the absolute value.
-
absInt
public static int absInt(int value) Gets the absolute value of anint
value.- Parameters:
value
- the argument whose absolute value is to be determined.- Returns:
- the absolute value.
-
mod
public static int mod(int x, int y) -
mod
public static float mod(float x, float y) -
min
public static float min(float x, float y) Returns the smallest value.- Parameters:
x
- the 1st value you want to compare with the other values.y
- the 2nd value you want to compare with the other values.- Returns:
- the smaller value (x or y).
-
min
public static float min(float x, float y, float z) Returns the smallest value.- Parameters:
x
- the 1st value you want to compare with the other values.y
- the 2nd value you want to compare with the other values.z
- the 3rd value you want to compare with the other values.- Returns:
- the smaller value (x, y or z).
-
min
public static int min(int x, int y) Returns the smallest (integer) value.- Parameters:
x
- the 1st value you want to compare with the other values.y
- the 2nd value you want to compare with the other values.- Returns:
- the smaller value (x or y).
-
min
public static int min(int x, int y, int z) Returns the smallest (integer) value.- Parameters:
x
- the 1st value you want to compare with the other values.y
- the 2nd value you want to compare with the other values.z
- the 3rd value you want to compare with the other values.- Returns:
- the smaller value (x, y or z).
-
max
public static float max(float x, float y) -
max
public static float max(float x, float y, float z) -
max
public static int max(int x, int y) -
max
public static int max(int x, int y, int z) -
sqr
public static float sqr(float value) Gets the value squared (value ^ 2).- Parameters:
value
- the value to square.- Returns:
- the square value.
-
sqrt
public static float sqrt(float value) Gets the square root of a given value.- Parameters:
value
- the value you want to get the square root of.- Returns:
- the square root value.
- See Also:
-
degreeToRadian
public static float degreeToRadian(float degree) Converts a degree value to radians.- Parameters:
degree
- the degree value.- Returns:
- the degree value in radians.
-
radianToDegree
public static float radianToDegree(float radian) Converts a radian value to degree.- Parameters:
radian
- the radian value.- Returns:
- the radian value in degree.
-
clamp
public static float clamp(float min, float max, float value) Clamps the value between min and max.- Parameters:
min
- the min value.max
- the max value.value
- the value you want to clamp.- Returns:
- the clamped value.
-
clamp
public static int clamp(int min, int max, int value) Clamps the value between min and max.- Parameters:
min
- the min value.max
- the max value.value
- the value you want to clamp.- Returns:
- the clamped value.
-
saturate
public static float saturate(float value) Clamps a value between 0 and 1.- Parameters:
value
- the value you want to clamp.- Returns:
- the value clamped between 0 and 1.
-
step
public static float step(float min, float x) If x is less than min, 0 is returned, else 1.- Parameters:
min
- min threshold.x
- the value.- Returns:
- 0 if x is less than min, 1 if x is greater or equal to min.
-
smoothstep
public static float smoothstep(float min, float max, float x) Performs a smooth Hermite interpolation between 0 and 1.- Parameters:
min
- the lower edge value.max
- the upper edge value.x
- the source value for interpolation.- Returns:
- the interpolated value between 0 and 1. If x is less than min, 0 will be returned. If x is greater than max, 1 will be returned. Otherwise a smooth interpolation between 0 and 1 is performed.
- See Also:
-
distance
public static float distance(float x1, float x2) Gets the distance between two values in 1d space (determined by x coordinate).- Parameters:
x1
- the first x coordinate.x2
- the second x coordinate.- Returns:
- the distance between the two values.
-
distance
public static float distance(float x1, float y1, float x2, float y2) Gets the distance between two points in 2d space (determined by x and y coordinate).- Parameters:
x1
- the x coordinate of the first point.y1
- the y coordinate of the first point.x2
- the x coordinate of the second point.y2
- the y coordinate of the second point.- Returns:
- the distance between the two points.
-
distance
public static float distance(float x1, float y1, float z1, float x2, float y2, float z2) Gets the distance between two points in 3d space (determined by x, y and z coordinate).- Parameters:
x1
- the x coordinate of the first point.y1
- the y coordinate of the first point.z1
- the z coordinate of the first point.x2
- the x coordinate of the second point.y2
- the y coordinate of the second point.z2
- the z coordinate of the second point.- Returns:
- the distance between the two points.
-
distanceSqr
public static float distanceSqr(float x1, float y1, float z1, float x2, float y2, float z2) -
length
public static float length(float x, float y) -
length
public static float length(float x, float y, float z) -
dot
public static float dot(float x1, float y1, float z1, float x2, float y2, float z2) -
getNextPowerOfTwo
public static int getNextPowerOfTwo(int value) Gets the next power of two value (rounded up). For example, 1 returns 1, 3 returns 4, 120 returns 128 etc.- Parameters:
value
- your value.- Returns:
- the next power of two value (rounded up).
-
getGCD
public static int getGCD(int... input) Gets the greatest common divisor of several int values.- Parameters:
input
- multiple int values you want to calculate the GCD of.- Returns:
- the greatest common divisor.
-
getGCD
public static int getGCD(int a, int b) Gets the greatest common divisor of two int values.- Parameters:
a
- first int value.b
- second int value.- Returns:
- the greatest common divisor of the two values.
-
round
public static float round(float value, int digit) Rounds a float value to a given decimal place.- Parameters:
value
- the value you want to round.digit
- the decimal places.- Returns:
- the rounded value.
-
nextRandomFloat
public static float nextRandomFloat()Gets a random float value between 0 and 1- Returns:
- a pseudorandom float value.
-
nextRandomInt
public static int nextRandomInt(int min, int max) Gets a random int value between min (inclusive) and max (inclusive).- Parameters:
min
- the lower bound (inclusive)max
- the upper bound (inclusive)- Returns:
- a pseudorandom int value.
-
nextRandomBoolean
public static boolean nextRandomBoolean()Gets a random boolean value (true or false).- Returns:
- a pseudorandom boolean value.
-