Package toxi.math

Class MathUtils

java.lang.Object
toxi.math.MathUtils

public final class MathUtils extends Object
Miscellaneous math utilities.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final float
    Degrees to radians conversion factor
    static final float
    Epsilon value
    static final float
    PI/2
    static final float
    The reciprocal of PI: (1/PI)
    static final float
    Log(2)
    static final float
    PI
    static final float
    PI*PI
    static final float
    PI/4
    static final float
    Radians to degrees conversion factor
    static Random
    Default random number generator used by random methods of this class which don't use a passed in Random instance.
    static final float
    Square root of 2
    static final float
    Square root of 3
    static final float
     
    static final float
    PI/3
    static final float
    PI*1.5
    static final float
    PI*2
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static final double
    abs(double x)
     
    static final float
    abs(float x)
     
    static final int
    abs(int x)
     
    static final int
    ceilPowerOf2(int x)
    Rounds up the value to the nearest higher power^2 value.
    static final double
    clip(double a, double min, double max)
     
    static final float
    clip(float a, float min, float max)
     
    static final int
    clip(int a, int min, int max)
     
    static double
    clipNormalized(double a)
     
    static final float
    clipNormalized(float a)
    Clips the value to the 0.0 ..
    static final double
    cos(double theta)
     
    static final float
    cos(float theta)
    Returns fast cosine approximation of a value.
    static final double
    degrees(double radians)
     
    static final float
    degrees(float radians)
     
    static double
    dualSign(double a, double b)
     
    static final double
    fastCos(double x)
    Fast cosine approximation.
    static final float
    fastInverseSqrt(float x)
    Deprecated. 
    static final float
    fastPow(float a, float b)
    Computes a fast approximation to Math.pow(a, b).
    static final double
    fastSin(double x)
    Fast sine approximation.
    static final boolean
     
    static final boolean
     
    static final long
    floor(double x)
     
    static final int
    floor(float x)
    This method is a *lot* faster than using (int)Math.floor(x).
    static final int
    Rounds down the value to the nearest lower power^2 value.
    static final int
    gcd(int p, int q)
    Computes the Greatest Common Devisor of integers p and q.
    static double
    impulse(double k, double t)
    Creates a single normalized impulse signal with its peak at t=1/k.
    static float
    impulse(float k, float t)
    Creates a single normalized impulse signal with its peak at t=1/k.
    static final int
    lcm(int p, int q)
     
    static double
    lerp(double a, double b, double t)
     
    static float
    lerp(float a, float b, float t)
     
    static double
    mapInterval(double x, double minIn, double maxIn, double minOut, double maxOut)
     
    static float
    mapInterval(float x, float minIn, float maxIn, float minOut, float maxOut)
     
    static final double
    max(double[] values)
     
    static final double
    max(double a, double b)
     
    static final double
    max(double a, double b, double c)
     
    static final float
    max(float[] values)
     
    static final float
    max(float a, float b)
     
    static final float
    max(float a, float b, float c)
    Returns the maximum value of three floats.
    static final int
    max(int[] values)
     
    static final int
    max(int a, int b)
     
    static final int
    max(int a, int b, int c)
    Returns the maximum value of three ints.
    static final double
    min(double a, double b)
     
    static final double
    min(double a, double b, double c)
     
    static final float
    min(float a, float b)
     
    static final float
    min(float a, float b, float c)
    Returns the minimum value of three floats.
    static final int
    min(int a, int b)
     
    static final int
    min(int a, int b, int c)
    Returns the minimum value of three ints.
    static final float
    Returns a random number in the interval -1 ..
    static final float
    Returns a random number in the interval -1 ..
    static double
    radians(double degrees)
     
    static final float
    radians(float degrees)
     
    static final float
    random(float max)
     
    static final float
    random(float min, float max)
     
    static final int
    random(int max)
     
    static final int
    random(int min, int max)
     
    static final double
    random(Random rnd, double max)
     
    static final double
    random(Random rnd, double min, double max)
     
    static final float
    random(Random rnd, float max)
     
    static final float
    random(Random rnd, float min, float max)
     
    static final int
    random(Random rnd, int max)
     
    static final int
    random(Random rnd, int min, int max)
     
    static final boolean
    randomChance(double chance)
     
    static final boolean
    randomChance(float chance)
     
    static final boolean
    randomChance(Random rnd, double chance)
     
    static final boolean
    randomChance(Random rnd, float chance)
     
    static final double
    reduceAngle(double theta)
     
    static final float
    reduceAngle(float theta)
    Reduces the given angle into the -PI/4 ...
    static final double
    roundTo(double val, double prec)
    Rounds a double precision value to the given precision.
    static final float
    roundTo(float val, float prec)
    Rounds a single precision value to the given precision.
    static final int
    roundTo(int val, int prec)
    Rounds an integer value to the given precision.
    static void
    Sets the default Random number generator for this class.
    static int
    sign(double x)
     
    static int
    sign(float x)
     
    static int
    sign(int x)
     
    static final double
    sin(double theta)
     
    static final float
    sin(float theta)
    Returns a fast sine approximation of a value.
    static final float
    sqrt(float x)
    Deprecated. 

    Methods inherited from class java.lang.Object

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

    • THIRD

      public static final float THIRD
      See Also:
    • SQRT2

      public static final float SQRT2
      Square root of 2
    • SQRT3

      public static final float SQRT3
      Square root of 3
    • LOG2

      public static final float LOG2
      Log(2)
    • PI

      public static final float PI
      PI
      See Also:
    • INV_PI

      public static final float INV_PI
      The reciprocal of PI: (1/PI)
      See Also:
    • HALF_PI

      public static final float HALF_PI
      PI/2
      See Also:
    • THIRD_PI

      public static final float THIRD_PI
      PI/3
      See Also:
    • QUARTER_PI

      public static final float QUARTER_PI
      PI/4
      See Also:
    • TWO_PI

      public static final float TWO_PI
      PI*2
      See Also:
    • THREE_HALVES_PI

      public static final float THREE_HALVES_PI
      PI*1.5
      See Also:
    • PI_SQUARED

      public static final float PI_SQUARED
      PI*PI
      See Also:
    • EPS

      public static final float EPS
      Epsilon value
      See Also:
    • DEG2RAD

      public static final float DEG2RAD
      Degrees to radians conversion factor
      See Also:
    • RAD2DEG

      public static final float RAD2DEG
      Radians to degrees conversion factor
      See Also:
    • RND

      public static Random RND
      Default random number generator used by random methods of this class which don't use a passed in Random instance.
  • Constructor Details

    • MathUtils

      public MathUtils()
  • Method Details

    • abs

      public static final double abs(double x)
      Parameters:
      x -
      Returns:
      absolute value of x
    • abs

      public static final float abs(float x)
      Parameters:
      x -
      Returns:
      absolute value of x
    • abs

      public static final int abs(int x)
      Parameters:
      x -
      Returns:
      absolute value of x
    • ceilPowerOf2

      public static final int ceilPowerOf2(int x)
      Rounds up the value to the nearest higher power^2 value.
      Parameters:
      x -
      Returns:
      power^2 value
    • clip

      public static final double clip(double a, double min, double max)
    • clip

      public static final float clip(float a, float min, float max)
    • clip

      public static final int clip(int a, int min, int max)
    • clipNormalized

      public static double clipNormalized(double a)
    • clipNormalized

      public static final float clipNormalized(float a)
      Clips the value to the 0.0 .. 1.0 interval.
      Parameters:
      a -
      Returns:
      clipped value
      Since:
      0012
    • cos

      public static final double cos(double theta)
    • cos

      public static final float cos(float theta)
      Returns fast cosine approximation of a value. Note: code from wiki posting on java.net by jeffpk
      Parameters:
      theta - angle in radians.
      Returns:
      cosine of theta.
    • degrees

      public static final double degrees(double radians)
    • degrees

      public static final float degrees(float radians)
    • dualSign

      public static double dualSign(double a, double b)
    • fastCos

      public static final double fastCos(double x)
      Fast cosine approximation.
      Parameters:
      x - angle in -PI/2 .. +PI/2 interval
      Returns:
      cosine
    • fastInverseSqrt

      @Deprecated public static final float fastInverseSqrt(float x)
      Deprecated.
    • fastPow

      public static final float fastPow(float a, float b)
      Computes a fast approximation to Math.pow(a, b). Adapted from http://www.dctsystems.co.uk/Software/power.html.
      Parameters:
      a - a positive number
      b - a number
      Returns:
      a^b
    • fastSin

      public static final double fastSin(double x)
      Fast sine approximation.
      Parameters:
      x - angle in -PI/2 .. +PI/2 interval
      Returns:
      sine
    • flipCoin

      public static final boolean flipCoin()
    • flipCoin

      public static final boolean flipCoin(Random rnd)
    • floor

      public static final long floor(double x)
    • floor

      public static final int floor(float x)
      This method is a *lot* faster than using (int)Math.floor(x).
      Parameters:
      x - value to be floored
      Returns:
      floored value as integer
      Since:
      0012
    • floorPowerOf2

      public static final int floorPowerOf2(int x)
      Rounds down the value to the nearest lower power^2 value.
      Parameters:
      x -
      Returns:
      power^2 value
    • gcd

      public static final int gcd(int p, int q)
      Computes the Greatest Common Devisor of integers p and q.
      Parameters:
      p -
      q -
      Returns:
      gcd
    • impulse

      public static double impulse(double k, double t)
      Creates a single normalized impulse signal with its peak at t=1/k. The attack and decay period is configurable via the k parameter. Code from: http://www.iquilezles.org/www/articles/functions/functions.htm
      Parameters:
      k - smoothness
      t - time position (should be >= 0)
      Returns:
      impulse value (as double)
    • impulse

      public static float impulse(float k, float t)
      Creates a single normalized impulse signal with its peak at t=1/k. The attack and decay period is configurable via the k parameter. Code from: http://www.iquilezles.org/www/articles/functions/functions.htm
      Parameters:
      k - smoothness
      t - time position (should be >= 0)
      Returns:
      impulse value (as float)
    • lcm

      public static final int lcm(int p, int q)
    • lerp

      public static double lerp(double a, double b, double t)
    • lerp

      public static float lerp(float a, float b, float t)
    • mapInterval

      public static double mapInterval(double x, double minIn, double maxIn, double minOut, double maxOut)
    • mapInterval

      public static float mapInterval(float x, float minIn, float maxIn, float minOut, float maxOut)
    • max

      public static final double max(double a, double b)
    • max

      public static final double max(double a, double b, double c)
    • max

      public static final double max(double[] values)
    • max

      public static final float max(float a, float b)
    • max

      public static final float max(float a, float b, float c)
      Returns the maximum value of three floats.
      Parameters:
      a -
      b -
      c -
      Returns:
      max val
    • max

      public static final float max(float[] values)
    • max

      public static final int max(int a, int b)
    • max

      public static final int max(int a, int b, int c)
      Returns the maximum value of three ints.
      Parameters:
      a -
      b -
      c -
      Returns:
      max val
    • max

      public static final int max(int[] values)
    • min

      public static final double min(double a, double b)
    • min

      public static final double min(double a, double b, double c)
    • min

      public static final float min(float a, float b)
    • min

      public static final float min(float a, float b, float c)
      Returns the minimum value of three floats.
      Parameters:
      a -
      b -
      c -
      Returns:
      min val
    • min

      public static final int min(int a, int b)
    • min

      public static final int min(int a, int b, int c)
      Returns the minimum value of three ints.
      Parameters:
      a -
      b -
      c -
      Returns:
      min val
    • normalizedRandom

      public static final float normalizedRandom()
      Returns a random number in the interval -1 .. +1.
      Returns:
      random float
    • normalizedRandom

      public static final float normalizedRandom(Random rnd)
      Returns a random number in the interval -1 .. +1 using the Random instance provided.
      Returns:
      random float
    • radians

      public static double radians(double degrees)
    • radians

      public static final float radians(float degrees)
    • random

      public static final float random(float max)
    • random

      public static final float random(float min, float max)
    • random

      public static final int random(int max)
    • random

      public static final int random(int min, int max)
    • random

      public static final double random(Random rnd, double max)
    • random

      public static final double random(Random rnd, double min, double max)
    • random

      public static final float random(Random rnd, float max)
    • random

      public static final float random(Random rnd, float min, float max)
    • random

      public static final int random(Random rnd, int max)
    • random

      public static final int random(Random rnd, int min, int max)
    • randomChance

      public static final boolean randomChance(double chance)
    • randomChance

      public static final boolean randomChance(float chance)
    • randomChance

      public static final boolean randomChance(Random rnd, double chance)
    • randomChance

      public static final boolean randomChance(Random rnd, float chance)
    • reduceAngle

      public static final double reduceAngle(double theta)
    • reduceAngle

      public static final float reduceAngle(float theta)
      Reduces the given angle into the -PI/4 ... PI/4 interval for faster computation of sin/cos. This method is used by sin(float) & cos(float).
      Parameters:
      theta - angle in radians
      Returns:
      reduced angle
      See Also:
    • roundTo

      public static final double roundTo(double val, double prec)
      Rounds a double precision value to the given precision.
      Parameters:
      val -
      prec -
      Returns:
      rounded value
    • roundTo

      public static final float roundTo(float val, float prec)
      Rounds a single precision value to the given precision.
      Parameters:
      val -
      prec -
      Returns:
      rounded value
    • roundTo

      public static final int roundTo(int val, int prec)
      Rounds an integer value to the given precision.
      Parameters:
      val -
      prec -
      Returns:
      rounded value
    • setDefaultRandomGenerator

      public static void setDefaultRandomGenerator(Random rnd)
      Sets the default Random number generator for this class. This generator is being reused by all future calls to random() method versions which don't explicitly ask for a Random instance to be used.
      Parameters:
      rnd -
    • sign

      public static int sign(double x)
    • sign

      public static int sign(float x)
    • sign

      public static int sign(int x)
    • sin

      public static final double sin(double theta)
    • sin

      public static final float sin(float theta)
      Returns a fast sine approximation of a value. Note: code from wiki posting on java.net by jeffpk
      Parameters:
      theta - angle in radians.
      Returns:
      sine of theta.
    • sqrt

      @Deprecated public static final float sqrt(float x)
      Deprecated.