Package toxi.math
Class MathUtils
java.lang.Object
toxi.math.MathUtils
Miscellaneous math utilities.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final float
Degrees to radians conversion factorstatic final float
Epsilon valuestatic final float
PI/2static final float
The reciprocal of PI: (1/PI)static final float
Log(2)static final float
PIstatic final float
PI*PIstatic final float
PI/4static final float
Radians to degrees conversion factorstatic Random
Default random number generator used by random methods of this class which don't use a passed inRandom
instance.static final float
Square root of 2static final float
Square root of 3static final float
static final float
PI/3static final float
PI*1.5static final float
PI*2 -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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 toMath.pow(a, b)
.static final double
fastSin
(double x) Fast sine approximation.static final boolean
flipCoin()
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
floorPowerOf2
(int x) 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
normalizedRandom
(Random rnd) 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
static final double
static final float
static final float
static final int
static final int
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.
-
Field Details
-
THIRD
public static final float THIRD- See Also:
-
SQRT2
public static final float SQRT2Square root of 2 -
SQRT3
public static final float SQRT3Square root of 3 -
LOG2
public static final float LOG2Log(2) -
PI
public static final float PIPI- See Also:
-
INV_PI
public static final float INV_PIThe reciprocal of PI: (1/PI)- See Also:
-
HALF_PI
public static final float HALF_PIPI/2- See Also:
-
THIRD_PI
public static final float THIRD_PIPI/3- See Also:
-
QUARTER_PI
public static final float QUARTER_PIPI/4- See Also:
-
TWO_PI
public static final float TWO_PIPI*2- See Also:
-
THREE_HALVES_PI
public static final float THREE_HALVES_PIPI*1.5- See Also:
-
PI_SQUARED
public static final float PI_SQUAREDPI*PI- See Also:
-
EPS
public static final float EPSEpsilon value- See Also:
-
DEG2RAD
public static final float DEG2RADDegrees to radians conversion factor- See Also:
-
RAD2DEG
public static final float RAD2DEGRadians to degrees conversion factor- See Also:
-
RND
Default random number generator used by random methods of this class which don't use a passed inRandom
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. -
fastPow
public static final float fastPow(float a, float b) Computes a fast approximation toMath.pow(a, b)
. Adapted from http://www.dctsystems.co.uk/Software/power.html.- Parameters:
a
- a positive numberb
- 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
-
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
- smoothnesst
- 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
- smoothnesst
- 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
Returns a random number in the interval -1 .. +1 using theRandom
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
-
random
-
random
-
random
-
random
-
random
-
randomChance
public static final boolean randomChance(double chance) -
randomChance
public static final boolean randomChance(float chance) -
randomChance
-
randomChance
-
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 bysin(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
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 aRandom
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.
-