Class MathUtilsd

java.lang.Object
gaiasky.util.math.MathUtilsd

public final class MathUtilsd
extends java.lang.Object
Utility and fast math functions.

Thanks to Riven on JavaGaming.org for the basis of sin/cos/atan2/floor/ceil.

  • Field Summary

    Fields
    Modifier and Type Field Description
    static double degRad  
    static double degreesToRadians
    multiply by this to convert from degrees to radians
    static double E  
    static double FLOAT_ROUNDING_ERROR  
    static double nanoToSec  
    static double PI  
    static double PI2  
    static double radDeg  
    static double radiansToDegrees
    multiply by this to convert from radians to degrees
    static java.util.Random random  
  • Constructor Summary

    Constructors
    Constructor Description
    MathUtilsd()  
  • Method Summary

    Modifier and Type Method Description
    static double atan2​(double y, double x)
    Returns atan2 in radians, faster but less accurate than Math.atan2.
    static double clamp​(double value, double min, double max)  
    static float clamp​(float value, float min, float max)  
    static int clamp​(int value, int min, int max)  
    static short clamp​(short value, short min, short max)  
    static double cos​(double radians)
    Returns the cosine in radians from a lookup table.
    static double cosDeg​(double degrees)
    Returns the cosine in radians from a lookup table.
    static double distancePointLine​(double x1, double y1, double z1, double x2, double y2, double z2, double x0, double y0, double z0)
    Gets the distance from the point x0 to the line denoted by x1-x2.
    Check this link
    static double distancePointSegment​(double x1, double y1, double z1, double x2, double y2, double z2, double x0, double y0, double z0)
    Gets the distance from the point p0 to the segment denoted by p1-p2.
    Check this link.
    static double eint​(double x, double x0, double x1, double y0, double y1, double exp)
    Does an exponential interpolation: y = y0 + (y1-y0) * 10 ^ (exp * (x-x0)/(x1-x0))
    static Vector3d getClosestPoint​(double x1, double y1, double z1, double x2, double y2, double z2, double x0, double y0, double z0)
    Gets the closest point on the line p1-p2 from p0.
    Check this link.
    static Vector3d getClosestPoint2​(double x1, double y1, double z1, double x2, double y2, double z2, double x0, double y0, double z0, Vector3d result)  
    static boolean isEqual​(double a, double b)
    Returns true if a is nearly equal to b.
    static boolean isEqual​(double a, double b, double tolerance)
    Returns true if a is nearly equal to b.
    static boolean isZero​(double value)
    Returns true if the value is zero (using the default tolerance as upper bound)
    static boolean isZero​(double value, double tolerance)
    Returns true if the value is zero.
    static double lerp​(double x, double y, double a)  
    static float lerp​(float x, float y, float a)  
    static long lerp​(long x, long y, long a)  
    static double lint​(double x, double x0, double x1, double y0, double y1)
    Linear interpolation
    static float lint​(float x, float x0, float x1, float y0, float y1)
    Linear interpolation
    static float lint​(long x, long x0, long x1, float y0, float y1)
    Linear interpolation
    static Vector3d perpendicular​(Vector3d in, Vector3d out)
    Gets perpendicular vector to in vector
    static double random()
    Returns random number between 0.0 (inclusive) and 1.0 (exclusive).
    static double random​(double range)
    Returns a random number between 0 (inclusive) and the specified value (exclusive).
    static double random​(double start, double end)
    Returns a random number between start (inclusive) and end (exclusive).
    static int random​(int range)
    Returns a random number between 0 (inclusive) and the specified value (inclusive).
    static int random​(int start, int end)
    Returns a random number between start (inclusive) and end (inclusive).
    static long random​(long range)
    Returns a random number between 0 (inclusive) and the specified value (inclusive).
    static long random​(long start, long end)
    Returns a random number between start (inclusive) and end (inclusive).
    static boolean randomBoolean()
    Returns a random boolean value.
    static boolean randomBoolean​(double chance)
    Returns true if a random value between 0 and 1 is less than the specified value.
    static int randomSign()
    Returns -1 or 1, randomly.
    static double randomTriangular()
    Returns a triangularly distributed random number between -1.0 (exclusive) and 1.0 (exclusive), where values around zero are more likely.
    static double randomTriangular​(double max)
    Returns a triangularly distributed random number between -max (exclusive) and max (exclusive), where values around zero are more likely.
    static double randomTriangular​(double min, double max)
    Returns a triangularly distributed random number between min (inclusive) and max (exclusive), where the mode argument defaults to the midpoint between the bounds, giving a symmetric distribution.
    static double randomTriangular​(double min, double max, double mode)
    Returns a triangularly distributed random number between min (inclusive) and max (exclusive), where values around mode are more likely.
    static double roundAvoid​(double value, int places)
    Rounds the double value to a number of decimal places
    static double sin​(double radians)
    Returns the sine in radians from a lookup table.
    static double sinDeg​(double degrees)
    Returns the sine in radians from a lookup table.
    static double sqrt​(double value)
    Fast sqrt method.

    Methods inherited from class java.lang.Object

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

  • Constructor Details

    • MathUtilsd

      public MathUtilsd()
  • Method Details

    • sin

      public static double sin​(double radians)
      Returns the sine in radians from a lookup table.
    • cos

      public static double cos​(double radians)
      Returns the cosine in radians from a lookup table.
    • sinDeg

      public static double sinDeg​(double degrees)
      Returns the sine in radians from a lookup table.
    • cosDeg

      public static double cosDeg​(double degrees)
      Returns the cosine in radians from a lookup table.
    • atan2

      public static double atan2​(double y, double x)
      Returns atan2 in radians, faster but less accurate than Math.atan2. Average error of 0.00231 radians (0.1323 degrees), largest error of 0.00488 radians (0.2796 degrees).
    • random

      public static int random​(int range)
      Returns a random number between 0 (inclusive) and the specified value (inclusive).
    • random

      public static int random​(int start, int end)
      Returns a random number between start (inclusive) and end (inclusive).
    • random

      public static long random​(long range)
      Returns a random number between 0 (inclusive) and the specified value (inclusive).
    • random

      public static long random​(long start, long end)
      Returns a random number between start (inclusive) and end (inclusive).
    • randomBoolean

      public static boolean randomBoolean()
      Returns a random boolean value.
    • randomBoolean

      public static boolean randomBoolean​(double chance)
      Returns true if a random value between 0 and 1 is less than the specified value.
    • random

      public static double random()
      Returns random number between 0.0 (inclusive) and 1.0 (exclusive).
    • random

      public static double random​(double range)
      Returns a random number between 0 (inclusive) and the specified value (exclusive).
    • random

      public static double random​(double start, double end)
      Returns a random number between start (inclusive) and end (exclusive).
    • randomSign

      public static int randomSign()
      Returns -1 or 1, randomly.
    • randomTriangular

      public static double randomTriangular()
      Returns a triangularly distributed random number between -1.0 (exclusive) and 1.0 (exclusive), where values around zero are more likely.

      This is an optimized version of randomTriangular(-1, 1, 0)

    • randomTriangular

      public static double randomTriangular​(double max)
      Returns a triangularly distributed random number between -max (exclusive) and max (exclusive), where values around zero are more likely.

      This is an optimized version of randomTriangular(-max, max, 0)

      Parameters:
      max - the upper limit
    • randomTriangular

      public static double randomTriangular​(double min, double max)
      Returns a triangularly distributed random number between min (inclusive) and max (exclusive), where the mode argument defaults to the midpoint between the bounds, giving a symmetric distribution.

      This method is equivalent of randomTriangular(min, max, (min + max) * .5f)

      Parameters:
      min - the lower limit
      max - the upper limit
    • randomTriangular

      public static double randomTriangular​(double min, double max, double mode)
      Returns a triangularly distributed random number between min (inclusive) and max (exclusive), where values around mode are more likely.
      Parameters:
      min - the lower limit
      max - the upper limit
      mode - the point around which the values are more likely
    • clamp

      public static int clamp​(int value, int min, int max)
    • clamp

      public static short clamp​(short value, short min, short max)
    • clamp

      public static float clamp​(float value, float min, float max)
    • clamp

      public static double clamp​(double value, double min, double max)
    • isZero

      public static boolean isZero​(double value)
      Returns true if the value is zero (using the default tolerance as upper bound)
    • isZero

      public static boolean isZero​(double value, double tolerance)
      Returns true if the value is zero.
      Parameters:
      tolerance - represent an upper bound below which the value is considered zero.
    • isEqual

      public static boolean isEqual​(double a, double b)
      Returns true if a is nearly equal to b. The function uses the default doubleing error tolerance.
      Parameters:
      a - the first value.
      b - the second value.
    • isEqual

      public static boolean isEqual​(double a, double b, double tolerance)
      Returns true if a is nearly equal to b.
      Parameters:
      a - the first value.
      b - the second value.
      tolerance - represent an upper bound below which the two values are considered equal.
    • sqrt

      public static double sqrt​(double value)
      Fast sqrt method. Default passes it through one round of Newton's method
      Parameters:
      value - The value
      Returns:
      The square root value
    • eint

      public static double eint​(double x, double x0, double x1, double y0, double y1, double exp)
      Does an exponential interpolation: y = y0 + (y1-y0) * 10 ^ (exp * (x-x0)/(x1-x0))
      Parameters:
      x -
      x0 -
      x1 -
      y0 -
      y1 -
      exp -
      Returns:
    • lint

      public static double lint​(double x, double x0, double x1, double y0, double y1)
      Linear interpolation
      Parameters:
      x - The value to interpolate
      x0 - Inferior limit to the independent value
      x1 - Superior limit to the independent value
      y0 - Inferior limit to the dependent value
      y1 - Superior limit to the dependent value
      Returns:
      The interpolated value
    • lerp

      public static double lerp​(double x, double y, double a)
    • lint

      public static float lint​(float x, float x0, float x1, float y0, float y1)
      Linear interpolation
      Parameters:
      x - The value to interpolate
      x0 - Inferior limit to the independent value
      x1 - Superior limit to the independent value
      y0 - Inferior limit to the dependent value
      y1 - Superior limit to the dependent value
      Returns:
      The interpolated value
    • lerp

      public static float lerp​(float x, float y, float a)
    • lint

      public static float lint​(long x, long x0, long x1, float y0, float y1)
      Linear interpolation
      Parameters:
      x - The value to interpolate
      x0 - Inferior limit to the independent value
      x1 - Superior limit to the independent value
      y0 - Inferior limit to the dependent value
      y1 - Superior limit to the dependent value
      Returns:
      The interpolated value
    • lerp

      public static long lerp​(long x, long y, long a)
    • distancePointLine

      public static double distancePointLine​(double x1, double y1, double z1, double x2, double y2, double z2, double x0, double y0, double z0)
      Gets the distance from the point x0 to the line denoted by x1-x2.
      Check this link
      Parameters:
      x1 - The first point in the line
      x2 - The second point in the line
      x0 - The point
      Returns:
      The Euclidean distance between the line (x1, x2) and x0
    • distancePointSegment

      public static double distancePointSegment​(double x1, double y1, double z1, double x2, double y2, double z2, double x0, double y0, double z0)
      Gets the distance from the point p0 to the segment denoted by p1-p2.
      Check this link.
      Parameters:
      x1 - The first segment delimiter.
      x2 - The second segment delimiter.
      x0 - The point.
      Returns:
      The Euclidean distance between the segment (x1, x2)
    • getClosestPoint

      public static Vector3d getClosestPoint​(double x1, double y1, double z1, double x2, double y2, double z2, double x0, double y0, double z0)
      Gets the closest point on the line p1-p2 from p0.
      Check this link.
      Parameters:
      x1 - The first segment delimiter.
      x2 - The second segment delimiter.
      x0 - The point.
      Returns:
      The vector with the closest point on the line to p0
    • getClosestPoint2

      public static Vector3d getClosestPoint2​(double x1, double y1, double z1, double x2, double y2, double z2, double x0, double y0, double z0, Vector3d result)
    • perpendicular

      public static Vector3d perpendicular​(Vector3d in, Vector3d out)
      Gets perpendicular vector to in vector
      Parameters:
      in -
      Returns:
    • roundAvoid

      public static double roundAvoid​(double value, int places)
      Rounds the double value to a number of decimal places
      Parameters:
      value - The value to round
      places - The number of decimal places
      Returns:
      The rounded value