Class MathUtils

java.lang.Object
net.mtrop.doom.util.MathUtils

public final class MathUtils extends Object
Math utils.
Author:
Matthew Tropiano
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    bitIsSet(long value, long test)
    Checks if bits are set in a value.
    static int
    booleansToInt(boolean... bool)
    Converts a series of boolean values to bits, going from least-significant to most-significant.
    static long
    booleansToLong(boolean... bool)
    Converts a series of boolean values to bits, going from least-significant to most-significant.
    static double
    clampValue(double val, double lo, double hi)
    Coerces a double to the range bounded by lo and hi.
    static float
    clampValue(float val, float lo, float hi)
    Coerces a float to the range bounded by lo and hi.
    static int
    clampValue(int val, int lo, int hi)
    Coerces an integer to the range bounded by lo and hi.
    static short
    clampValue(short val, short lo, short hi)
    Coerces a short to the range bounded by lo and hi.
    static int
    clearBits(int value, int bits)
    Clears the bits of a value.
    static long
    clearBits(long value, long bits)
    Clears the bits of a value.
    static double
    cosineInterpolate(double factor, double x, double y)
    Gives a value that is the result of a cosine interpolation between two values.
    static double
    cubicInterpolate(double factor, double w, double x, double y, double z)
    Gives a value that is the result of a cublic interpolation between two values.
    static double
    getInterpolationFactor(double value, double lo, double hi)
    Gets a scalar factor that equals how "far along" a value is along an interval.
    static double
    linearInterpolate(double factor, double x, double y)
    Gives a value that is the result of a linear interpolation between two values.
    static boolean
    Returns a random boolean.
    static double
     
    static double
    Returns a random double value from -1 to 1 (inclusive).
    static int
    setBits(int value, int bits)
    Sets the bits of a value.
    static long
    setBits(long value, long bits)
    Sets the bits of a value.
    static double
    wrapValue(double val, double lo, double hi)
    Coerces a double to the range bounded by lo and hi, by "wrapping" the value.
    static float
    wrapValue(float val, float lo, float hi)
    Coerces a float to the range bounded by lo and hi, by "wrapping" the value.
    static int
    wrapValue(int val, int lo, int hi)
    Coerces an integer to the range bounded by lo and hi, by "wrapping" the value.
    static short
    wrapValue(short val, short lo, short hi)
    Coerces a short to the range bounded by lo and hi, by "wrapping" the value.

    Methods inherited from class java.lang.Object

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

    • MathUtils

      public MathUtils()
  • Method Details

    • bitIsSet

      public static boolean bitIsSet(long value, long test)
      Checks if bits are set in a value.
      Parameters:
      value - the value.
      test - the testing bits.
      Returns:
      true if all of the bits set in test are set in value, false otherwise.
    • setBits

      public static long setBits(long value, long bits)
      Sets the bits of a value.
      Parameters:
      value - the value.
      bits - the bits to set.
      Returns:
      the resulting number.
    • setBits

      public static int setBits(int value, int bits)
      Sets the bits of a value.
      Parameters:
      value - the value.
      bits - the bits to set.
      Returns:
      the resulting number.
    • clearBits

      public static long clearBits(long value, long bits)
      Clears the bits of a value.
      Parameters:
      value - the value.
      bits - the bits to clear.
      Returns:
      the resulting number.
    • clearBits

      public static int clearBits(int value, int bits)
      Clears the bits of a value.
      Parameters:
      value - the value.
      bits - the bits to clear.
      Returns:
      the resulting number.
    • booleansToInt

      public static int booleansToInt(boolean... bool)
      Converts a series of boolean values to bits, going from least-significant to most-significant. TRUE booleans set the bit, FALSE ones do not.
      Parameters:
      bool - list of booleans. cannot exceed 32.
      Returns:
      the resultant bitstring in an integer.
    • booleansToLong

      public static long booleansToLong(boolean... bool)
      Converts a series of boolean values to bits, going from least-significant to most-significant. TRUE booleans set the bit, FALSE ones do not.
      Parameters:
      bool - list of booleans. cannot exceed 64.
      Returns:
      the resultant bitstring in a long integer.
    • randBoolean

      public static boolean randBoolean(Random rand)
      Returns a random boolean.
      Parameters:
      rand - the random number generator.
      Returns:
      true or false.
    • randDouble

      public static double randDouble(Random rand)
      Parameters:
      rand - the random number generator.
      Returns:
      a random double value from [0 to 1) (inclusive/exclusive).
    • randDoubleN

      public static double randDoubleN(Random rand)
      Returns a random double value from -1 to 1 (inclusive).
      Parameters:
      rand - the random number generator.
      Returns:
      the next double.
    • getInterpolationFactor

      public static double getInterpolationFactor(double value, double lo, double hi)
      Gets a scalar factor that equals how "far along" a value is along an interval.
      Parameters:
      value - the value to test.
      lo - the lower value of the interval.
      hi - the higher value of the interval.
      Returns:
      a value between 0 and 1 describing this distance (0 = beginning or less, 1 = end or greater), or 0 if lo and hi are equal.
    • linearInterpolate

      public static double linearInterpolate(double factor, double x, double y)
      Gives a value that is the result of a linear interpolation between two values.
      Parameters:
      factor - the interpolation factor.
      x - the first value.
      y - the second value.
      Returns:
      the interpolated value.
    • cosineInterpolate

      public static double cosineInterpolate(double factor, double x, double y)
      Gives a value that is the result of a cosine interpolation between two values.
      Parameters:
      factor - the interpolation factor.
      x - the first value.
      y - the second value.
      Returns:
      the interpolated value.
    • cubicInterpolate

      public static double cubicInterpolate(double factor, double w, double x, double y, double z)
      Gives a value that is the result of a cublic interpolation between two values. Requires two outside values to predict a curve more accurately.
      Parameters:
      factor - the interpolation factor between x and y.
      w - the value before the first.
      x - the first value.
      y - the second value.
      z - the value after the second.
      Returns:
      the interpolated value.
    • clampValue

      public static int clampValue(int val, int lo, int hi)
      Coerces an integer to the range bounded by lo and hi.
      Example: clampValue(32,-16,16) returns 16.
      Example: clampValue(4,-16,16) returns 4.
      Example: clampValue(-1000,-16,16) returns -16.
      Parameters:
      val - the integer.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "forced" into the range.
    • clampValue

      public static short clampValue(short val, short lo, short hi)
      Coerces a short to the range bounded by lo and hi.
      Example: clampValue(32,-16,16) returns 16.
      Example: clampValue(4,-16,16) returns 4.
      Example: clampValue(-1000,-16,16) returns -16.
      Parameters:
      val - the short.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "forced" into the range.
    • clampValue

      public static float clampValue(float val, float lo, float hi)
      Coerces a float to the range bounded by lo and hi.
      Example: clampValue(32,-16,16) returns 16.
      Example: clampValue(4,-16,16) returns 4.
      Example: clampValue(-1000,-16,16) returns -16.
      Parameters:
      val - the float.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "forced" into the range.
    • clampValue

      public static double clampValue(double val, double lo, double hi)
      Coerces a double to the range bounded by lo and hi.
      Example: clampValue(32,-16,16) returns 16.
      Example: clampValue(4,-16,16) returns 4.
      Example: clampValue(-1000,-16,16) returns -16.
      Parameters:
      val - the double.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "forced" into the range.
    • wrapValue

      public static int wrapValue(int val, int lo, int hi)
      Coerces an integer to the range bounded by lo and hi, by "wrapping" the value.
      Example: wrapValue(32,-16,16) returns 0.
      Example: wrapValue(4,-16,16) returns 4.
      Example: wrapValue(-1000,-16,16) returns 8.
      Parameters:
      val - the integer.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "wrapped" into the range.
    • wrapValue

      public static short wrapValue(short val, short lo, short hi)
      Coerces a short to the range bounded by lo and hi, by "wrapping" the value.
      Example: wrapValue(32,-16,16) returns 0.
      Example: wrapValue(4,-16,16) returns 4.
      Example: wrapValue(-1000,-16,16) returns 8.
      Parameters:
      val - the short.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "wrapped" into the range.
    • wrapValue

      public static float wrapValue(float val, float lo, float hi)
      Coerces a float to the range bounded by lo and hi, by "wrapping" the value.
      Example: wrapValue(32,-16,16) returns 0.
      Example: wrapValue(4,-16,16) returns 4.
      Example: wrapValue(-1000,-16,16) returns 8.
      Parameters:
      val - the float.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "wrapped" into the range.
    • wrapValue

      public static double wrapValue(double val, double lo, double hi)
      Coerces a double to the range bounded by lo and hi, by "wrapping" the value.
      Example: wrapValue(32,-16,16) returns 0.
      Example: wrapValue(4,-16,16) returns 4.
      Example: wrapValue(-1000,-16,16) returns 8.
      Parameters:
      val - the double.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the value after being "wrapped" into the range.