Class MathUtilsDouble

java.lang.Object
gaiasky.util.math.MathUtilsDouble

public final class MathUtilsDouble extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
     
    static final double
    multiply by this to convert from degrees to radians
    static final double
     
    static final double
     
    static final double
     
    static final double
     
    static final double
    multiply by this to convert from radians to degrees
    static Random
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • 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
    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 boolean
    Checks whether a is divisible by b, using the default precision.
    static boolean
    divisible(String a, String b, long precision)
    Checks whether a is divisible by b, using the given floating-point precision.
    static boolean
    fuzzyEquals(Double a, Double b, double epsilon)
    Compares two doubles, with an epsilon.
    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
    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 double
    logisticSigmoid(double x, double span)
    Implements the logistic sigmoid, defined as expit(x) = 1/(1+exp(-x)).
    static double
    logit(double x)
    Implements the logit function, defined as logit(x) = log(x/(1-x)).
    static double
    lowPass(double newValue, double smoothedValue, double smoothing)
    Implements a low-pass filter to smooth the input values.
    static float
    lowPass(float newValue, float smoothedValue, float smoothing)
    Implements a low-pass filter to smooth the input values.
    static double
    normalizeAngle(double a, double center)
    Normalize an angle in a 2π wide interval around a center value.
    static double
    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 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
    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

    • MathUtilsDouble

      public MathUtilsDouble()
  • 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.
    • 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).
    • 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).
    • 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
    • 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
    • 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
    • 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
    • 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)
    • 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)
    • 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
    • normalizeAngle

      public static double normalizeAngle(double a, double center)
      Normalize an angle in a 2π wide interval around a center value.

      This method has three main uses:

      • normalize an angle between 0 and 2π:
        a = MathUtils.normalizeAngle(a, FastMath.PI);
      • normalize an angle between -π and +π
        a = MathUtils.normalizeAngle(a, 0.0);
      • compute the angle between two defining angular positions:
        angle = MathUtils.normalizeAngle(end, start) - start;

      Note that due to numerical accuracy and since π cannot be represented exactly, the result interval is closed, it cannot be half-closed as would be more satisfactory in a purely mathematical view.

      Parameters:
      a - angle to normalize
      center - center of the desired 2π interval for the result
      Returns:
      a-2kπ with integer k and center-π <= a-2kπ <= center+π
      Since:
      1.2
    • fuzzyEquals

      public static boolean fuzzyEquals(Double a, Double b, double epsilon)
      Compares two doubles, with an epsilon.
      Parameters:
      a - first nullable element
      b - second nullable element
      epsilon - absolute difference tolerance.
    • divisible

      public static boolean divisible(String a, String b)
      Checks whether a is divisible by b, using the default precision.
      Parameters:
      a - First decimal number, represented by a string.
      b - Second decimal number, represented by a string.
      Returns:
      Whether a % b == 0 at the default precision.
    • divisible

      public static boolean divisible(String a, String b, long precision)
      Checks whether a is divisible by b, using the given floating-point precision.
      Parameters:
      a - First decimal number, represented by a string.
      b - Second decimal number, represented by a string.
      precision - The precision.
      Returns:
      Whether a % b == 0 at the given precision.
    • lowPass

      public static double lowPass(double newValue, double smoothedValue, double smoothing)
      Implements a low-pass filter to smooth the input values.
      Parameters:
      newValue - The new value.
      smoothedValue - The previous smoothed value.
      smoothing - The smoothing factor.
      Returns:
      The new value with a low-pass filter applied.
    • lowPass

      public static float lowPass(float newValue, float smoothedValue, float smoothing)
      Implements a low-pass filter to smooth the input values.
      Parameters:
      newValue - The new value.
      smoothedValue - The previous smoothed value.
      smoothing - The smoothing factor.
      Returns:
      The new value with a low-pass filter applied.
    • logit

      public static double logit(double x)
      Implements the logit function, defined as logit(x) = log(x/(1-x)). Note that logit(0) = -Infinity, and logit(1) = Infinity.
      Parameters:
      x - The value to sample.
    • logisticSigmoid

      public static double logisticSigmoid(double x, double span)
      Implements the logistic sigmoid, defined as expit(x) = 1/(1+exp(-x)). It is the inverse of logit.
      Parameters:
      x - The value to sample, in [0, 1].
      span - The span of the function. The value gets re-mapped using this span.
      Returns:
      The logistic sigmoid function.