Package de.slikey.effectlib.util
Class MathUtils
java.lang.Object
de.slikey.effectlib.util.MathUtils
Utility and fast math functions.
Thanks to Riven on JavaGaming.org for the basis of sin/cos/atan2/floor/ceil.
- Author:
- Nathan Sweet
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final floatstatic final floatmultiply by this to convert from degrees to radiansstatic final floatstatic final floatstatic final floatstatic final floatstatic final floatstatic final floatstatic final floatmultiply by this to convert from radians to degreesstatic final Randomstatic final float -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic floatatan2(float y, float x) Returns atan2 in radians from a lookup table.static intceil(float x) Returns the smallest integer greater than or equal to the specified float.static intceilPositive(float x) Returns the smallest integer greater than or equal to the specified float.static floatclamp(float value, float min, float max) static intclamp(int value, int min, int max) static shortclamp(short value, short min, short max) static floatcos(float radians) Returns the cosine in radians from a lookup table.static floatcosDeg(float degrees) Returns the cosine in radians from a lookup table.static intfloor(float x) Returns the largest integer less than or equal to the specified float.static intfloorPositive(float x) Returns the largest integer less than or equal to the specified float.static booleanisEqual(float a, float b) Returns true if a is nearly equal to b.static booleanisEqual(float a, float b, float tolerance) Returns true if a is nearly equal to b.static booleanisFinite(double value) Compatiblity method for Double.isFinite from Java 1.7 and upstatic booleanisPowerOfTwo(int value) static booleanisZero(float value) Returns true if the value is zero (using the default tolerance as upper bound)static booleanisZero(float value, float tolerance) Returns true if the value is zero.static intnextPowerOfTwo(int value) Returns the next power of two.static floatrandom()Returns random number between 0.0 (inclusive) and 1.0 (exclusive).static floatrandom(float range) Returns a random number between 0 (inclusive) and the specified value (exclusive).static floatrandom(float start, float end) Returns a random number between start (inclusive) and end (exclusive).static intrandom(int range) Returns a random number between 0 (inclusive) and the specified value (inclusive).static intrandom(int start, int end) Returns a random number between start (inclusive) and end (inclusive).static booleanReturns a random boolean value.static booleanrandomBoolean(float chance) Returns true if a random value between 0 and 1 is less than the specified value.static intround(float x) Returns the closest integer to the specified float.static introundPositive(float x) Returns the closest integer to the specified float.static floatsin(float radians) Returns the sine in radians from a lookup table.static floatsinDeg(float degrees) Returns the sine in radians from a lookup table.
-
Field Details
-
nanoToSec
public static final float nanoToSec- See Also:
-
FLOAT_ROUNDING_ERROR
public static final float FLOAT_ROUNDING_ERROR- See Also:
-
PI
public static final float PI- See Also:
-
PI2
public static final float PI2- See Also:
-
SQRT_3
public static final float SQRT_3- See Also:
-
E
public static final float E- See Also:
-
radiansToDegrees
public static final float radiansToDegreesmultiply by this to convert from radians to degrees- See Also:
-
radDeg
public static final float radDeg- See Also:
-
degreesToRadians
public static final float degreesToRadiansmultiply by this to convert from degrees to radians- See Also:
-
degRad
public static final float degRad- See Also:
-
random
-
-
Constructor Details
-
MathUtils
public MathUtils()
-
-
Method Details
-
sin
public static float sin(float radians) Returns the sine in radians from a lookup table. -
cos
public static float cos(float radians) Returns the cosine in radians from a lookup table. -
sinDeg
public static float sinDeg(float degrees) Returns the sine in radians from a lookup table. -
cosDeg
public static float cosDeg(float degrees) Returns the cosine in radians from a lookup table. -
atan2
public static float atan2(float y, float x) Returns atan2 in radians from a lookup table. -
random
public static int random(int range) Returns a random number between 0 (inclusive) and the specified value (inclusive). -
random
public static int random(int start, int end) Returns a random number between start (inclusive) and end (inclusive). -
randomBoolean
public static boolean randomBoolean()Returns a random boolean value. -
randomBoolean
public static boolean randomBoolean(float chance) Returns true if a random value between 0 and 1 is less than the specified value. -
random
public static float random()Returns random number between 0.0 (inclusive) and 1.0 (exclusive). -
random
public static float random(float range) Returns a random number between 0 (inclusive) and the specified value (exclusive). -
random
public static float random(float start, float end) Returns a random number between start (inclusive) and end (exclusive). -
nextPowerOfTwo
public static int nextPowerOfTwo(int value) Returns the next power of two. Returns the specified value if the value is already a power of two. -
isPowerOfTwo
public static boolean isPowerOfTwo(int value) -
clamp
public static int clamp(int value, int min, int max) -
clamp
public static short clamp(short value, short min, short max) -
clamp
public static float clamp(float value, float min, float max) -
floor
public static int floor(float x) Returns the largest integer less than or equal to the specified float. This method will only properly floor floats from -(2^14) to (Float.MAX_VALUE - 2^14). -
floorPositive
public static int floorPositive(float x) Returns the largest integer less than or equal to the specified float. This method will only properly floor floats that are positive. Note this method simply casts the float to int. -
ceil
public static int ceil(float x) Returns the smallest integer greater than or equal to the specified float. This method will only properly ceil floats from -(2^14) to (Float.MAX_VALUE - 2^14). -
ceilPositive
public static int ceilPositive(float x) Returns the smallest integer greater than or equal to the specified float. This method will only properly ceil floats that are positive. -
round
public static int round(float x) Returns the closest integer to the specified float. This method will only properly round floats from -(2^14) to (Float.MAX_VALUE - 2^14). -
roundPositive
public static int roundPositive(float x) Returns the closest integer to the specified float. This method will only properly round floats that are positive. -
isZero
public static boolean isZero(float value) Returns true if the value is zero (using the default tolerance as upper bound) -
isZero
public static boolean isZero(float value, float tolerance) Returns true if the value is zero.- Parameters:
tolerance- represent an upper bound below which the value is considered zero.
-
isEqual
public static boolean isEqual(float a, float b) Returns true if a is nearly equal to b. The function uses the default floating error tolerance.- Parameters:
a- the first value.b- the second value.
-
isEqual
public static boolean isEqual(float a, float b, float tolerance) Returns true if a is nearly equal to b.- Parameters:
a- the first value.b- the second value.tolerance- represent an upper bound below which the two values are considered equal.
-
isFinite
public static boolean isFinite(double value) Compatiblity method for Double.isFinite from Java 1.7 and up
-