Class MathUtil
java.lang.Object
nl.pim16aap2.animatedarchitecture.core.util.MathUtil
Utility class for mathematical operations.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanbetween(double test, double low, double high) Check if a given value is between two other values.static booleanbetween(int test, int low, int high) Check if a given value is between two other values.static intceil(double value) Returns the smallest integer value that is greater than or equal to the argument.static doubleclamp(double val, double minVal, double maxVal) Clamps a value between minimum and maximum values.static intclamp(int val, int minVal, int maxVal) Clamps a value between minimum and maximum values.static doubleclampAngleDeg(double angle) Clamp an angle to [-360 ; 360].static doubleclampAngleRad(double angle) Clamp an angle to [-2PI ; 2PI].static intfloor(double value) Returns the largest integer value that is less than or equal to the argument.static booleanisNumerical(@Nullable String str) Checks if a string is numerical integer with radix 10.static OptionalDoubleparseDouble(@Nullable String str) Parses a string to a double.static OptionalDoubleparseDouble(Optional<String> str) Parses an optional string to a double.static OptionalIntParses a string to an integer.static OptionalIntParses an optional string to an integer.static OptionalLongParses a string to a long.static OptionalLongParses an optional string to a long.static intround(double value) Returns the closest integer to the argument.
-
Field Details
-
HALF_PI
public static final double HALF_PIHalf of the mathematical constant PI.- See Also:
-
EPS
public static final double EPSEpsilon value for floating point comparisons.- See Also:
-
-
Method Details
-
floor
public static int floor(double value) Returns the largest integer value that is less than or equal to the argument.See
Math.floor(double).- Parameters:
value- the value to round down to the nearest integer.- Returns:
- the largest integer value that is less than or equal to the argument.
-
round
public static int round(double value) Returns the closest integer to the argument. The result is rounded to an integer if the argument is not already an integer.See
Math.round(double).- Parameters:
value- the value to round to the nearest integer.- Returns:
- the closest integer to the argument.
-
ceil
public static int ceil(double value) Returns the smallest integer value that is greater than or equal to the argument.See
Math.ceil(double).- Parameters:
value- The value to round up to the nearest integer.- Returns:
- The smallest integer value that is greater than or equal to the argument.
-
clamp
public static double clamp(double val, double minVal, double maxVal) Clamps a value between minimum and maximum values.- Parameters:
val- The value to clamp.minVal- The lower bound value of the range to which the value should be clamped.maxVal- The upper bound value of the range to which the value should be clamped.- Returns:
- The input value if it is less than the maximum value and more than the minimum value, or the minimum or maximum values if it exceeds either of those.
-
clamp
public static int clamp(int val, int minVal, int maxVal) Clamps a value between minimum and maximum values.- Parameters:
val- The value to clamp.minVal- The lower bound value of the range to which the value should be clamped.maxVal- The upper bound value of the range to which the value should be clamped.- Returns:
- The input value if it is less than the maximum value and more than the minimum value, or the minimum or maximum values if it exceeds either of those.
-
clampAngleRad
public static double clampAngleRad(double angle) Clamp an angle to [-2PI ; 2PI].- Parameters:
angle- The current angle in radians.- Returns:
- The angle (in radians) clamped to [-2PI ; 2PI].
-
clampAngleDeg
public static double clampAngleDeg(double angle) Clamp an angle to [-360 ; 360].- Parameters:
angle- The current angle in degrees.- Returns:
- The angle (in degrees) clamped to [-360 ; 360].
-
parseInt
Parses a string to an integer.- Parameters:
str- The string to parse.- Returns:
- The parsed integer, or an empty
OptionalIntif the string was null or could not be parsed.
-
parseInt
Parses an optional string to an integer.- Parameters:
str- The string to parse.- Returns:
- The parsed integer, or an empty
OptionalIntif the optional was empty or the string could not be parsed.
-
parseDouble
Parses a string to a double.- Parameters:
str- The string to parse.- Returns:
- The parsed double, or an empty
OptionalDoubleif the string was null or could not be parsed.
-
parseDouble
Parses an optional string to a double.- Parameters:
str- The string to parse.- Returns:
- The parsed double, or an empty
OptionalDoubleif the optional was empty or the string could not be parsed.
-
parseLong
Parses a string to a long.- Parameters:
str- The string to parse.- Returns:
- The parsed long, or an empty
OptionalLongif the string was null or could not be parsed.
-
parseLong
Parses an optional string to a long.- Parameters:
str- The string to parse.- Returns:
- The parsed long, or an empty
OptionalLongif the optional was empty or the string could not be parsed.
-
isNumerical
Checks if a string is numerical integer with radix 10.Any string for which this method returns false will always throw a
NumberFormatExceptionwhen parsed byInteger.parseInt(String).Conversely, any string for which this method returns true may still throw a
NumberFormatExceptionwhen parsed byInteger.parseInt(String)if the number is too large.- Parameters:
str- The string to check.- Returns:
- True if the string is numerical.
-
between
public static boolean between(int test, int low, int high) Check if a given value is between two other values. Matches inclusively.- Parameters:
test- Value to be compared.low- Minimum value.high- Maximum value.- Returns:
- True if the value is in the provided range or if it equals the low and/or the high value.
-
between
public static boolean between(double test, double low, double high) Check if a given value is between two other values. Matches inclusively.- Parameters:
test- Value to be compared.low- Minimum value.high- Maximum value.- Returns:
- True if the value is in the provided range or if it equals the low and/or the high value.
-