Class MathUtil

java.lang.Object
org.locationtech.jts.math.MathUtil

public class MathUtil extends Object
Various utility functions for mathematical and numerical operations.
Author:
mbdavis
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    The inverse of the Golden Ratio phi.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    average(double x1, double x2)
    Computes the average of two numbers.
    static int
    ceil(int num, int denom)
    Computes the ceiling function of the dividend of two integers.
    static double
    clamp(double x, double min, double max)
    Clamps a double value to a given range.
    static int
    clamp(int x, int min, int max)
    Clamps an int value to a given range.
    static int
    clampMax(int x, int max)
    Clamps an integer to a given maximum limit.
    static double
    log10(double x)
    Computes the base-10 logarithm of a double value.
    static double
    max(double v1, double v2, double v3)
     
    static double
    max(double v1, double v2, double v3, double v4)
     
    static double
    min(double v1, double v2, double v3, double v4)
     
    static double
    quasirandom(double curr)
    Generates a quasi-random sequence of numbers in the range [0,1].
    static double
    quasirandom(double curr, double alpha)
    Generates a quasi-random sequence of numbers in the range [0,1].
    static int
    wrap(int index, int max)
    Computes an index which wraps around a given maximum value.

    Methods inherited from class java.lang.Object

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

    • PHI_INV

      public static final double PHI_INV
      The inverse of the Golden Ratio phi.
  • Constructor Details

    • MathUtil

      public MathUtil()
  • Method Details

    • clamp

      public static double clamp(double x, double min, double max)
      Clamps a double value to a given range.
      Parameters:
      x - the value to clamp
      min - the minimum value of the range
      max - the maximum value of the range
      Returns:
      the clamped value
    • clamp

      public static int clamp(int x, int min, int max)
      Clamps an int value to a given range.
      Parameters:
      x - the value to clamp
      min - the minimum value of the range
      max - the maximum value of the range
      Returns:
      the clamped value
    • clampMax

      public static int clampMax(int x, int max)
      Clamps an integer to a given maximum limit.
      Parameters:
      x - the value to clamp
      max - the maximum value
      Returns:
      the clamped value
    • ceil

      public static int ceil(int num, int denom)
      Computes the ceiling function of the dividend of two integers.
      Parameters:
      num - the numerator
      denom - the denominator
      Returns:
      the ceiling of num / denom
    • log10

      public static double log10(double x)
      Computes the base-10 logarithm of a double value.
      • If the argument is NaN or less than zero, then the result is NaN.
      • If the argument is positive infinity, then the result is positive infinity.
      • If the argument is positive zero or negative zero, then the result is negative infinity.
      Parameters:
      x - a positive number
      Returns:
      the value log a, the base-10 logarithm of the input value
    • wrap

      public static int wrap(int index, int max)
      Computes an index which wraps around a given maximum value. For values >= 0, this is equals to val % max. For values < 0, this is equal to max - (-val) % max
      Parameters:
      index - the value to wrap
      max - the maximum value (or modulus)
      Returns:
      the wrapped index
    • average

      public static double average(double x1, double x2)
      Computes the average of two numbers.
      Parameters:
      x1 - a number
      x2 - a number
      Returns:
      the average of the inputs
    • max

      public static double max(double v1, double v2, double v3)
    • max

      public static double max(double v1, double v2, double v3, double v4)
    • min

      public static double min(double v1, double v2, double v3, double v4)
    • quasirandom

      public static double quasirandom(double curr)
      Generates a quasi-random sequence of numbers in the range [0,1]. They are produced by an additive recurrence with 1/φ as the constant. This produces a low-discrepancy sequence which is more evenly distribute than random numbers.

      See Wikipedia: Low-discrepancy Sequences - Additive Recurrence.

      The sequence is initialized by calling it with any positive fractional number; 0 works well for most uses.

      Parameters:
      curr - the current number in the sequence
      Returns:
      the next value in the sequence
    • quasirandom

      public static double quasirandom(double curr, double alpha)
      Generates a quasi-random sequence of numbers in the range [0,1]. They are produced by an additive recurrence with constant α.
           R(α) :  tn = { t0 + nα },  n = 1,2,3,...   
       
      When α is irrational this produces a Low discrepancy sequence which is more evenly distributed than random numbers.

      The sequence is initialized by calling it with any positive fractional number. 0 works well for most uses.

      Parameters:
      curr - the current number in the sequence
      alpha - the sequence additive constant
      Returns:
      the next value in the sequence