Class MathUtils


  • public class MathUtils
    extends Object
    • Constructor Detail

      • MathUtils

        public MathUtils()
    • Method Detail

      • negateDoubleIf

        @Contract(pure=true)
        public static double negateDoubleIf​(boolean shouldInvert,
                                            double toInvert)
        Inverts the provided value if shouldInvert is true.
        Parameters:
        shouldInvert - Whether to invert the number.
        toInvert - The number to invert.
        Returns:
        If shouldInvert is true, -toInvert; otherwise, toInvert
      • constrain

        @Contract(pure=true)
        public static double constrain​(double input,
                                       double cap)
        Constrains an input to a given range in either direction from zero. This does not map the input to the range; it simply hard-caps it when it's outside.
        Parameters:
        input - The input to constrain.
        cap - The distance from zero to constrain input to.
        Returns:
        If input > cap, return cap; if input < -cap, return -cap; otherwise, return input.
      • constrain

        @Contract(pure=true)
        public static double constrain​(double input,
                                       double lowerCap,
                                       double upperCap)
        Constrains an input to a given range. This does not map the input to the range; it simply hard-caps it when it's outside.
        Parameters:
        input - The input to constrain.
        lowerCap - The lower bound of the range.
        upperCap - The upper bound of the range.
        Returns:
        If input > upperCap, return upperCap; if input < lowerCap, return lowerCap; otherwise, return input.
      • preserveSignRaiseToPower

        @Contract(pure=true)
        public static double preserveSignRaiseToPower​(double input,
                                                      double pow)
        Raises the input to the provided power while preserving the sign. Useful for joystick scaling.
        Parameters:
        input - The input to be raised.
        pow - The power.
        Returns:
        The input raised to the provided power, with the sign of the input.