Class MathUtil

java.lang.Object
nl.pim16aap2.animatedarchitecture.core.util.MathUtil

public final class MathUtil extends Object
Utility class for mathematical operations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Epsilon value for floating point comparisons.
    static final double
    Half of the mathematical constant PI.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    between(double test, double low, double high)
    Check if a given value is between two other values.
    static boolean
    between(int test, int low, int high)
    Check if a given value is between two other values.
    static int
    ceil(double value)
    Returns the smallest integer value that is greater than or equal to the argument.
    static double
    clamp(double val, double minVal, double maxVal)
    Clamps a value between minimum and maximum values.
    static int
    clamp(int val, int minVal, int maxVal)
    Clamps a value between minimum and maximum values.
    static double
    clampAngleDeg(double angle)
    Clamp an angle to [-360 ; 360].
    static double
    clampAngleRad(double angle)
    Clamp an angle to [-2PI ; 2PI].
    static int
    floor(double value)
    Returns the largest integer value that is less than or equal to the argument.
    static boolean
    isNumerical(@Nullable String str)
    Checks if a string is numerical integer with radix 10.
    parseDouble(@Nullable String str)
    Parses a string to a double.
    Parses an optional string to a double.
    parseInt(@Nullable String str)
    Parses a string to an integer.
    Parses an optional string to an integer.
    parseLong(@Nullable String str)
    Parses a string to a long.
    Parses an optional string to a long.
    static int
    round(double value)
    Returns the closest integer to the argument.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • HALF_PI

      public static final double HALF_PI
      Half of the mathematical constant PI.
      See Also:
    • EPS

      public static final double EPS
      Epsilon 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

      public static OptionalInt parseInt(@Nullable String str)
      Parses a string to an integer.
      Parameters:
      str - The string to parse.
      Returns:
      The parsed integer, or an empty OptionalInt if the string was null or could not be parsed.
    • parseInt

      public static OptionalInt parseInt(Optional<String> str)
      Parses an optional string to an integer.
      Parameters:
      str - The string to parse.
      Returns:
      The parsed integer, or an empty OptionalInt if the optional was empty or the string could not be parsed.
    • parseDouble

      public static OptionalDouble parseDouble(@Nullable String str)
      Parses a string to a double.
      Parameters:
      str - The string to parse.
      Returns:
      The parsed double, or an empty OptionalDouble if the string was null or could not be parsed.
    • parseDouble

      public static OptionalDouble parseDouble(Optional<String> str)
      Parses an optional string to a double.
      Parameters:
      str - The string to parse.
      Returns:
      The parsed double, or an empty OptionalDouble if the optional was empty or the string could not be parsed.
    • parseLong

      public static OptionalLong parseLong(@Nullable String str)
      Parses a string to a long.
      Parameters:
      str - The string to parse.
      Returns:
      The parsed long, or an empty OptionalLong if the string was null or could not be parsed.
    • parseLong

      public static OptionalLong parseLong(Optional<String> str)
      Parses an optional string to a long.
      Parameters:
      str - The string to parse.
      Returns:
      The parsed long, or an empty OptionalLong if the optional was empty or the string could not be parsed.
    • isNumerical

      public static boolean isNumerical(@Nullable String str)
      Checks if a string is numerical integer with radix 10.

      Any string for which this method returns false will always throw a NumberFormatException when parsed by Integer.parseInt(String).

      Conversely, any string for which this method returns true may still throw a NumberFormatException when parsed by Integer.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.