Package toxi.math
Class MathUtils
java.lang.Object
toxi.math.MathUtils
Miscellaneous math utilities.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final floatDegrees to radians conversion factorstatic final floatEpsilon valuestatic final floatPI/2static final floatThe reciprocal of PI: (1/PI)static final floatLog(2)static final floatPIstatic final floatPI*PIstatic final floatPI/4static final floatRadians to degrees conversion factorstatic RandomDefault random number generator used by random methods of this class which don't use a passed inRandominstance.static final floatSquare root of 2static final floatSquare root of 3static final floatstatic final floatPI/3static final floatPI*1.5static final floatPI*2 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final doubleabs(double x) static final floatabs(float x) static final intabs(int x) static final intceilPowerOf2(int x) Rounds up the value to the nearest higher power^2 value.static final doubleclip(double a, double min, double max) static final floatclip(float a, float min, float max) static final intclip(int a, int min, int max) static doubleclipNormalized(double a) static final floatclipNormalized(float a) Clips the value to the 0.0 ..static final doublecos(double theta) static final floatcos(float theta) Returns fast cosine approximation of a value.static final doubledegrees(double radians) static final floatdegrees(float radians) static doubledualSign(double a, double b) static final doublefastCos(double x) Fast cosine approximation.static final floatfastInverseSqrt(float x) Deprecated.static final floatfastPow(float a, float b) Computes a fast approximation toMath.pow(a, b).static final doublefastSin(double x) Fast sine approximation.static final booleanflipCoin()static final booleanstatic final longfloor(double x) static final intfloor(float x) This method is a *lot* faster than using (int)Math.floor(x).static final intfloorPowerOf2(int x) Rounds down the value to the nearest lower power^2 value.static final intgcd(int p, int q) Computes the Greatest Common Devisor of integers p and q.static doubleimpulse(double k, double t) Creates a single normalized impulse signal with its peak at t=1/k.static floatimpulse(float k, float t) Creates a single normalized impulse signal with its peak at t=1/k.static final intlcm(int p, int q) static doublelerp(double a, double b, double t) static floatlerp(float a, float b, float t) static doublemapInterval(double x, double minIn, double maxIn, double minOut, double maxOut) static floatmapInterval(float x, float minIn, float maxIn, float minOut, float maxOut) static final doublemax(double[] values) static final doublemax(double a, double b) static final doublemax(double a, double b, double c) static final floatmax(float[] values) static final floatmax(float a, float b) static final floatmax(float a, float b, float c) Returns the maximum value of three floats.static final intmax(int[] values) static final intmax(int a, int b) static final intmax(int a, int b, int c) Returns the maximum value of three ints.static final doublemin(double a, double b) static final doublemin(double a, double b, double c) static final floatmin(float a, float b) static final floatmin(float a, float b, float c) Returns the minimum value of three floats.static final intmin(int a, int b) static final intmin(int a, int b, int c) Returns the minimum value of three ints.static final floatReturns a random number in the interval -1 ..static final floatnormalizedRandom(Random rnd) Returns a random number in the interval -1 ..static doubleradians(double degrees) static final floatradians(float degrees) static final floatrandom(float max) static final floatrandom(float min, float max) static final intrandom(int max) static final intrandom(int min, int max) static final doublestatic final doublestatic final floatstatic final floatstatic final intstatic final intstatic final booleanrandomChance(double chance) static final booleanrandomChance(float chance) static final booleanrandomChance(Random rnd, double chance) static final booleanrandomChance(Random rnd, float chance) static final doublereduceAngle(double theta) static final floatreduceAngle(float theta) Reduces the given angle into the -PI/4 ...static final doubleroundTo(double val, double prec) Rounds a double precision value to the given precision.static final floatroundTo(float val, float prec) Rounds a single precision value to the given precision.static final introundTo(int val, int prec) Rounds an integer value to the given precision.static voidSets the default Random number generator for this class.static intsign(double x) static intsign(float x) static intsign(int x) static final doublesin(double theta) static final floatsin(float theta) Returns a fast sine approximation of a value.static final floatsqrt(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 inRandominstance.
-
-
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 theRandominstance 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 aRandominstance 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.
-