Class GenericMath

java.lang.Object
org.spongepowered.math.GenericMath

public final class GenericMath extends Object
Class containing generic mathematical functions.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    A "close to zero" double epsilon value for use
    static final float
    A "close to zero" float epsilon value for use
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    biLerp(double x, double y, double q00, double q01, double q10, double q11, double x1, double x2, double y1, double y2)
    Calculates the value at x,y using bilinear interpolation
    static Color
    blend(Color a, Color b)
    Blends two colors into one.
    static Boolean
    Casts a value to a boolean.
    static Byte
    Casts a value to a byte.
    static Double
    Casts a value to a double.
    static Float
    Casts a value to a float.
    static Integer
    Casts a value to an integer.
    static Long
    Casts a value to a long.
    static Short
    Casts a value to a short.
    static double
    clamp(double value, double low, double high)
    Clamps the value between the low and high boundaries
    static int
    clamp(int value, int low, int high)
    Clamps the value between the low and high boundaries
    static String
    decToHex(int dec, int minDigits)
    Converts an integer to hexadecimal form with at least the minimum of digits specified (by adding leading zeros).
    static float
    degreeDifference(float angle1, float angle2)
    Gets the difference between two angles This value is always positive (0 - 180)
    static int
    floor(double a)
    Rounds 'a' down to the closest integer
    static int
    floor(float a)
    Rounds 'a' down to the closest integer
    static long
    floorl(double a)
    Rounds 'a' down to the closest long
    static long
    floorl(float a)
    Rounds 'a' down to the closest long
    static double
    inverseSqrt(double a)
    Returns a fast estimate of the inverse square root of the value
    static boolean
    isPowerOfTwo(int num)
    Determines if the given number is a power of two.
    static double
    lerp(double a, double b, double percent)
    Calculates the linear interpolation between a and b with the given percent
    static double
    lerp(double x, double x1, double x2, double q0, double q1)
    Calculates the value at x using linear interpolation
    static float
    lerp(float a, float b, float percent)
    Calculates the linear interpolation between a and b with the given percent
    static int
    lerp(int a, int b, int percent)
    Calculates the linear interpolation between a and b with the given percent
    static Color
    lerp(Color a, Color b, float percent)
    Calculates the linear interpolation between a and b with the given percent
    lerp(Quaterniond a, Quaterniond b, double percent)
    Interpolates a quaternion between two others using linear interpolation.
    lerp(Quaternionf a, Quaternionf b, float percent)
    Interpolates a quaternion between two others using linear interpolation.
    static Vector2d
    lerp(Vector2d a, Vector2d b, double percent)
    Calculates the linear interpolation between a and b with the given percent
    static Vector2f
    lerp(Vector2f a, Vector2f b, float percent)
    Calculates the linear interpolation between a and b with the given percent
    static Vector3d
    lerp(Vector3d a, Vector3d b, double percent)
    Calculates the linear interpolation between a and b with the given percent
    static Vector3f
    lerp(Vector3f a, Vector3f b, float percent)
    Calculates the linear interpolation between a and b with the given percent
    static byte
    max(byte value1, byte value2)
    Gets the maximum byte value from two values
    static double
    mean(double... values)
    Calculates the mean of a set of values.
    static int
    mean(int... values)
    Calculates the mean of a set of values
    static double
    mod(double a, double div)
    Returns the modulo of 'a' by 'div' with corrections for negative numbers.
    static float
    mod(float a, float div)
    Returns the modulo of 'a' by 'div' with corrections for negative numbers.
    static int
    mod(int a, int div)
    Returns the modulo of 'a' by 'div' with corrections for negative numbers.
    static int
    Converts a multiplication into a shift.
    static Vector2d
    Attempts to normalize a vector.
    static Vector2f
    Attempts to normalize a vector.
    static Vector3d
    Attempts to normalize a vector.
    static Vector3f
    Attempts to normalize a vector.
    static Vector4d
    Attempts to normalize a vector.
    static Vector4f
    Attempts to normalize a vector.
    static VectorNd
    Attempts to normalize a vector.
    static VectorNf
    Attempts to normalize a vector.
    static double
    radianDifference(double radian1, double radian2)
    Gets the difference between two radians This value is always positive (0 - PI)
    static double
    round(double input, int decimals)
    Rounds a number to the amount of decimals specified
    static int
    roundUpPow2(int a)
    Rounds an integer up to the next power of 2.
    static long
    roundUpPow2(long a)
    Rounds an integer up to the next power of 2.
    slerp(Quaterniond a, Quaterniond b, double percent)
    Interpolates a quaternion between two others using spherical linear interpolation.
    slerp(Quaternionf a, Quaternionf b, float percent)
    Interpolates a quaternion between two others using spherical linear interpolation.
    static double
    sqrt(double a)
    Returns a fast estimate of the square root of the value
    static double
    triLerp(double x, double y, double z, double q000, double q001, double q010, double q011, double q100, double q101, double q110, double q111, double x1, double x2, double y1, double y2, double z1, double z2)
    Calculates the value at x,y,z using trilinear interpolation
    static float
    wrapAngleDeg(float angle)
    Wraps the angle between -180 and 180 degrees
    static float
    wrapAnglePitchDeg(float angle)
    Wraps the pitch angle between -90 and 90 degrees
    static double
    wrapAngleRad(double angle)
    Wraps the radian between -PI and PI
    static byte
    wrapByte(int value)
    Wraps a byte between 0 and 256

    Methods inherited from class java.lang.Object

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

    • DBL_EPSILON

      public static final double DBL_EPSILON
      A "close to zero" double epsilon value for use
    • FLT_EPSILON

      public static final float FLT_EPSILON
      A "close to zero" float epsilon value for use
  • Method Details

    • degreeDifference

      public static float degreeDifference(float angle1, float angle2)
      Gets the difference between two angles This value is always positive (0 - 180)
      Parameters:
      angle1 - The first angle
      angle2 - The second angle
      Returns:
      the positive angle difference
    • radianDifference

      public static double radianDifference(double radian1, double radian2)
      Gets the difference between two radians This value is always positive (0 - PI)
      Parameters:
      radian1 - The first angle
      radian2 - The second angle
      Returns:
      the positive radian difference
    • wrapAngleDeg

      public static float wrapAngleDeg(float angle)
      Wraps the angle between -180 and 180 degrees
      Parameters:
      angle - to wrap
      Returns:
      -180 < angle <= 180
    • wrapAngleRad

      public static double wrapAngleRad(double angle)
      Wraps the radian between -PI and PI
      Parameters:
      angle - to wrap
      Returns:
      -PI < radian <= PI
    • wrapAnglePitchDeg

      public static float wrapAnglePitchDeg(float angle)
      Wraps the pitch angle between -90 and 90 degrees
      Parameters:
      angle - to wrap
      Returns:
      -90 < angle < 90
    • wrapByte

      public static byte wrapByte(int value)
      Wraps a byte between 0 and 256
      Parameters:
      value - to wrap
      Returns:
      0 < byte < 256
    • round

      public static double round(double input, int decimals)
      Rounds a number to the amount of decimals specified
      Parameters:
      input - to round
      decimals - to round to
      Returns:
      the rounded number
    • lerp

      public static double lerp(double a, double b, double percent)
      Calculates the linear interpolation between a and b with the given percent
      Parameters:
      a - The first know value
      b - The second know value
      percent - The percent
      Returns:
      the interpolated value
    • lerp

      public static float lerp(float a, float b, float percent)
      Calculates the linear interpolation between a and b with the given percent
      Parameters:
      a - The first know value
      b - The second know value
      percent - The percent
      Returns:
      the interpolated value
    • lerp

      public static int lerp(int a, int b, int percent)
      Calculates the linear interpolation between a and b with the given percent
      Parameters:
      a - The first know value
      b - The second know value
      percent - The percent
      Returns:
      the interpolated value
    • lerp

      public static Vector3f lerp(Vector3f a, Vector3f b, float percent)
      Calculates the linear interpolation between a and b with the given percent
      Parameters:
      a - The first know value
      b - The second know value
      percent - The percent
      Returns:
      the interpolated vector
    • lerp

      public static Vector3d lerp(Vector3d a, Vector3d b, double percent)
      Calculates the linear interpolation between a and b with the given percent
      Parameters:
      a - The first know value
      b - The second know value
      percent - The percent
      Returns:
      the interpolated vector
    • lerp

      public static Vector2f lerp(Vector2f a, Vector2f b, float percent)
      Calculates the linear interpolation between a and b with the given percent
      Parameters:
      a - The first know value
      b - The second know value
      percent - The percent
      Returns:
      the interpolated vector
    • lerp

      public static Vector2d lerp(Vector2d a, Vector2d b, double percent)
      Calculates the linear interpolation between a and b with the given percent
      Parameters:
      a - The first know value
      b - The second know value
      percent - The percent
      Returns:
      the interpolated vector
    • lerp

      public static double lerp(double x, double x1, double x2, double q0, double q1)
      Calculates the value at x using linear interpolation
      Parameters:
      x - the X coord of the value to interpolate
      x1 - the X coord of q0
      x2 - the X coord of q1
      q0 - the first known value (x1)
      q1 - the second known value (x2)
      Returns:
      the interpolated value
    • lerp

      public static Color lerp(Color a, Color b, float percent)
      Calculates the linear interpolation between a and b with the given percent
      Parameters:
      a - The first know value
      b - The second know value
      percent - The percent
      Returns:
      Color
    • slerp

      public static Quaternionf slerp(Quaternionf a, Quaternionf b, float percent)
      Interpolates a quaternion between two others using spherical linear interpolation.
      Parameters:
      a - The first quaternion
      b - The second quaternion
      percent - The percent for the interpolation, between 0 and 1 inclusively
      Returns:
      The interpolated quaternion
    • slerp

      public static Quaterniond slerp(Quaterniond a, Quaterniond b, double percent)
      Interpolates a quaternion between two others using spherical linear interpolation.
      Parameters:
      a - The first quaternion
      b - The second quaternion
      percent - The percent for the interpolation, between 0 and 1 inclusively
      Returns:
      The interpolated quaternion
    • lerp

      public static Quaternionf lerp(Quaternionf a, Quaternionf b, float percent)
      Interpolates a quaternion between two others using linear interpolation.
      Parameters:
      a - The first quaternion
      b - The second quaternion
      percent - The percent for the interpolation, between 0 and 1 inclusively
      Returns:
      The interpolated quaternion
    • lerp

      public static Quaterniond lerp(Quaterniond a, Quaterniond b, double percent)
      Interpolates a quaternion between two others using linear interpolation.
      Parameters:
      a - The first quaternion
      b - The second quaternion
      percent - The percent for the interpolation, between 0 and 1 inclusively
      Returns:
      The interpolated quaternion
    • biLerp

      public static double biLerp(double x, double y, double q00, double q01, double q10, double q11, double x1, double x2, double y1, double y2)
      Calculates the value at x,y using bilinear interpolation
      Parameters:
      x - the X coord of the value to interpolate
      y - the Y coord of the value to interpolate
      q00 - the first known value (x1, y1)
      q01 - the second known value (x1, y2)
      q10 - the third known value (x2, y1)
      q11 - the fourth known value (x2, y2)
      x1 - the X coord of q00 and q01
      x2 - the X coord of q10 and q11
      y1 - the Y coord of q00 and q10
      y2 - the Y coord of q01 and q11
      Returns:
      the interpolated value
    • triLerp

      public static double triLerp(double x, double y, double z, double q000, double q001, double q010, double q011, double q100, double q101, double q110, double q111, double x1, double x2, double y1, double y2, double z1, double z2)
      Calculates the value at x,y,z using trilinear interpolation
      Parameters:
      x - the X coord of the value to interpolate
      y - the Y coord of the value to interpolate
      z - the Z coord of the value to interpolate
      q000 - the first known value (x1, y1, z1)
      q001 - the second known value (x1, y2, z1)
      q010 - the third known value (x1, y1, z2)
      q011 - the fourth known value (x1, y2, z2)
      q100 - the fifth known value (x2, y1, z1)
      q101 - the sixth known value (x2, y2, z1)
      q110 - the seventh known value (x2, y1, z2)
      q111 - the eighth known value (x2, y2, z2)
      x1 - the X coord of q000, q001, q010 and q011
      x2 - the X coord of q100, q101, q110 and q111
      y1 - the Y coord of q000, q010, q100 and q110
      y2 - the Y coord of q001, q011, q101 and q111
      z1 - the Z coord of q000, q001, q100 and q101
      z2 - the Z coord of q010, q011, q110 and q111
      Returns:
      the interpolated value
    • blend

      public static Color blend(Color a, Color b)
      Blends two colors into one.
      Parameters:
      a - The first color
      b - The second color
      Returns:
      The blended color
    • clamp

      public static double clamp(double value, double low, double high)
      Clamps the value between the low and high boundaries
      Parameters:
      value - The value to clamp
      low - The low bound of the clamp
      high - The high bound of the clamp
      Returns:
      the clamped value
    • clamp

      public static int clamp(int value, int low, int high)
      Clamps the value between the low and high boundaries
      Parameters:
      value - The value to clamp
      low - The low bound of the clamp
      high - The high bound of the clamp
      Returns:
      the clamped value
    • inverseSqrt

      public static double inverseSqrt(double a)
      Returns a fast estimate of the inverse square root of the value
      Parameters:
      a - The value
      Returns:
      The estimate of the inverse square root
    • sqrt

      public static double sqrt(double a)
      Returns a fast estimate of the square root of the value
      Parameters:
      a - The value
      Returns:
      The estimate of the square root
    • floor

      public static int floor(double a)
      Rounds 'a' down to the closest integer
      Parameters:
      a - The value to floor
      Returns:
      The closest integer
    • floor

      public static int floor(float a)
      Rounds 'a' down to the closest integer
      Parameters:
      a - The value to floor
      Returns:
      The closest integer
    • floorl

      public static long floorl(double a)
      Rounds 'a' down to the closest long
      Parameters:
      a - The value to floor
      Returns:
      The closest long
    • floorl

      public static long floorl(float a)
      Rounds 'a' down to the closest long
      Parameters:
      a - The value to floor
      Returns:
      The closest long
    • max

      public static byte max(byte value1, byte value2)
      Gets the maximum byte value from two values
      Parameters:
      value1 - The first value
      value2 - The second value
      Returns:
      the maximum of value1 and value2
    • roundUpPow2

      public static int roundUpPow2(int a)
      Rounds an integer up to the next power of 2.
      Parameters:
      a - The integer to round
      Returns:
      the lowest power of 2 greater or equal to 'a'
    • roundUpPow2

      public static long roundUpPow2(long a)
      Rounds an integer up to the next power of 2.
      Parameters:
      a - The long to round
      Returns:
      the lowest power of 2 greater or equal to 'a'
    • castFloat

      public static Float castFloat(Object o)
      Casts a value to a float. May return null.
      Parameters:
      o - The object to attempt to cast
      Returns:
      The object as a float
    • castByte

      public static Byte castByte(Object o)
      Casts a value to a byte. May return null.
      Parameters:
      o - The object to attempt to cast
      Returns:
      The object as a byte
    • castShort

      public static Short castShort(Object o)
      Casts a value to a short. May return null.
      Parameters:
      o - The object to attempt to cast
      Returns:
      The object as a short
    • castInt

      public static Integer castInt(Object o)
      Casts a value to an integer. May return null.
      Parameters:
      o - The object to attempt to cast
      Returns:
      The object as an int
    • castDouble

      public static Double castDouble(Object o)
      Casts a value to a double. May return null.
      Parameters:
      o - The object to attempt to cast
      Returns:
      The object as a double
    • castLong

      public static Long castLong(Object o)
      Casts a value to a long. May return null.
      Parameters:
      o - The object to attempt to cast
      Returns:
      The object as a long
    • castBoolean

      public static Boolean castBoolean(Object o)
      Casts a value to a boolean. May return null.
      Parameters:
      o - The object to attempt to cast
      Returns:
      The object as a boolean
    • mean

      public static int mean(int... values)
      Calculates the mean of a set of values
      Parameters:
      values - to calculate the mean of
      Returns:
      the mean of the values
    • mean

      public static double mean(double... values)
      Calculates the mean of a set of values.
      Parameters:
      values - to calculate the mean of
      Returns:
      the mean of the values
    • decToHex

      public static String decToHex(int dec, int minDigits)
      Converts an integer to hexadecimal form with at least the minimum of digits specified (by adding leading zeros).
      Parameters:
      dec - The integer to convert
      minDigits - The minimum of digits in the hexadecimal form
      Returns:
      The integer in hexadecimal form
    • mod

      public static int mod(int a, int div)
      Returns the modulo of 'a' by 'div' with corrections for negative numbers.
      Parameters:
      a - The number as an int
      div - The div as an int
      Returns:
      The corrected modulo
    • mod

      public static float mod(float a, float div)
      Returns the modulo of 'a' by 'div' with corrections for negative numbers.
      Parameters:
      a - The dividend
      div - The divider
      Returns:
      The corrected modulo
    • mod

      public static double mod(double a, double div)
      Returns the modulo of 'a' by 'div' with corrections for negative numbers.
      Parameters:
      a - The dividend
      div - The divider
      Returns:
      The corrected modulo
    • isPowerOfTwo

      public static boolean isPowerOfTwo(int num)
      Determines if the given number is a power of two. A number is a power of 2 if it is 1 or greater, and it contains no similar bits of the given number - 1.
      Parameters:
      num - the number to check
      Returns:
      true if num is a power of two
    • multiplyToShift

      public static int multiplyToShift(int a)
      Converts a multiplication into a shift.
      Parameters:
      a - the multiplicand
      Returns:
      the left shift required to multiply by the multiplicand
    • normalizeSafe

      public static Vector2f normalizeSafe(Vector2f v)
      Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.
      Parameters:
      v - The vector to attempt to normalize
      Returns:
      The normalized vector, or the zero vector if it couldn't be normalized.
    • normalizeSafe

      public static Vector2d normalizeSafe(Vector2d v)
      Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.
      Parameters:
      v - The vector to attempt to normalize
      Returns:
      The normalized vector, or the zero vector if it couldn't be normalized.
    • normalizeSafe

      public static Vector3f normalizeSafe(Vector3f v)
      Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.
      Parameters:
      v - The vector to attempt to normalize
      Returns:
      The normalized vector, or the zero vector if it couldn't be normalized.
    • normalizeSafe

      public static Vector3d normalizeSafe(Vector3d v)
      Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.
      Parameters:
      v - The vector to attempt to normalize
      Returns:
      The normalized vector, or the zero vector if it couldn't be normalized.
    • normalizeSafe

      public static Vector4f normalizeSafe(Vector4f v)
      Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.
      Parameters:
      v - The vector to attempt to normalize
      Returns:
      The normalized vector, or the zero vector if it couldn't be normalized.
    • normalizeSafe

      public static Vector4d normalizeSafe(Vector4d v)
      Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.
      Parameters:
      v - The vector to attempt to normalize
      Returns:
      The normalized vector, or the zero vector if it couldn't be normalized.
    • normalizeSafe

      public static VectorNf normalizeSafe(VectorNf v)
      Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.
      Parameters:
      v - The vector to attempt to normalize
      Returns:
      The normalized vector, or the zero vector if it couldn't be normalized.
    • normalizeSafe

      public static VectorNd normalizeSafe(VectorNd v)
      Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.
      Parameters:
      v - The vector to attempt to normalize
      Returns:
      The normalized vector, or the zero vector if it couldn't be normalized.