net.nexttext
Class FastMath

java.lang.Object
  extended by net.nexttext.FastMath

public final class FastMath
extends java.lang.Object

FastMath provides 'fast' math approximations and float equivalents of Math functions. These are all used as static values and functions.

Version:
$Id: FastMath.java 4131 2009-03-19 20:15:28Z blaine.dev $
Author:
Various

Field Summary
static double DBL_EPSILON
          A "close to zero" double epsilon value for use
static float DEG_TO_RAD
          A value to multiply a degree value by, to convert it to radians.
static float FLT_EPSILON
          A "close to zero" float epsilon value for use
static float HALF_PI
          The value PI/2 as a float.
static float INV_PI
          The value 1/PI as a float.
static float INV_TWO_PI
          The value 1/(2PI) as a float.
static float ONE_THIRD
           
static float PI
          The value PI as a float.
static float QUARTER_PI
          The value PI/4 as a float.
static float RAD_TO_DEG
          A value to multiply a radian value by, to convert it to degrees.
static java.util.Random rand
          A precreated random object for random numbers.
static float TWO_PI
          The value 2PI as a float.
static float ZERO_TOLERANCE
          A "close to zero" float epsilon value for use
 
Method Summary
static double abs(double dValue)
          Returns Absolute value of a double.
static float abs(float fValue)
          Returns Absolute value of a float.
static float acos(float fValue)
          Returns the arc cosine of an angle given in radians.
Special cases: If fValue is smaller than -1, then the result is PI.
static double asin(double dValue)
          Returns the arc sine of an angle given in radians.
Special cases: If dValue is smaller than -1, then the result is -HALF_PI.
static float asin(float fValue)
          Returns the arc sine of an angle given in radians.
Special cases: If fValue is smaller than -1, then the result is -HALF_PI.
static double atan(double dValue)
          Returns the arc tangent of an angle given in radians.
static float atan(float fValue)
          Returns the arc tangent of an angle given in radians.
static double atan2(double dY, double dX)
          A direct call to Math.atan2.
static float atan2(float fY, float fX)
          A direct call to Math.atan2.
static processing.core.PVector cartesianToSpherical(processing.core.PVector cartCoords, processing.core.PVector store)
          Converts a point from Cartesian coordinates (using positive Y as up) to Spherical and stores the results in the store var.
static processing.core.PVector cartesianZToSpherical(processing.core.PVector cartCoords, processing.core.PVector store)
          Converts a point from Cartesian coordinates (using positive Z as up) to Spherical and stores the results in the store var.
static float ceil(float fValue)
          Rounds a fValue up.
static float clamp(float input, float min, float max)
          Take a float input and clamp it between min and max.
static float copysign(float x, float y)
           
static double cos(double dValue)
          Returns cos of a value.
static float cos(float fValue)
          Returns cos of a value.
static float determinant(double m00, double m01, double m02, double m03, double m10, double m11, double m12, double m13, double m20, double m21, double m22, double m23, double m30, double m31, double m32, double m33)
          Returns the determinant of a 4x4 matrix.
static float exp(float fValue)
          Returns E^fValue
static float floor(float fValue)
          Returns a number rounded down.
static float invSqrt(float fValue)
          Returns 1/sqrt(fValue)
static boolean isPowerOfTwo(int number)
          Returns true if the number is a power of 2 (2,4,8,16...) A good implementation found on the Java boards.
static float LERP(float percent, float startValue, float endValue)
          Linear interpolation from startValue to endValue by the given percent.
static float log(float fValue)
          Returns the log base E of a value.
static float log(float value, float base)
          Returns the logarithm of value with given base, calculated as log(value)/log(base), so that pow(base, return)==value (contributed by vear)
static int nearestPowerOfTwo(int number)
           
static float nextRandomFloat()
          Returns a random float between 0 and 1.
static int nextRandomInt()
           
static int nextRandomInt(int min, int max)
          Returns a random float between min and max.
static float normalize(float val, float min, float max)
          Takes an value and expresses it in terms of min to max.
static float pow(float fBase, float fExponent)
          Returns a number raised to an exponent power.
static double reduceSinAngle(double radians)
          Fast Trig functions for x86.
static float reduceSinAngle(float radians)
          Fast Trig functions for x86.
static float sign(float fValue)
          Returns 1 if the number is positive, -1 if the number is negative, and 0 otherwise
static int sign(int iValue)
          Returns 1 if the number is positive, -1 if the number is negative, and 0 otherwise
static double sin(double dValue)
          Returns sine of a value.
static float sin(float fValue)
          Returns sine of a value.
static processing.core.PVector sphericalToCartesian(processing.core.PVector sphereCoords, processing.core.PVector store)
          Converts a point from Spherical coordinates to Cartesian (using positive Y as up) and stores the results in the store var.
static processing.core.PVector sphericalToCartesianZ(processing.core.PVector sphereCoords, processing.core.PVector store)
          Converts a point from Spherical coordinates to Cartesian (using positive Z as up) and stores the results in the store var.
static float sqr(float fValue)
          Returns the value squared.
static double sqrt(double dValue)
          Returns the square root of a given value.
static float sqrt(float fValue)
          Returns the square root of a given value.
static float tan(float fValue)
          Returns the tangent of a value.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DBL_EPSILON

public static final double DBL_EPSILON
A "close to zero" double epsilon value for use

See Also:
Constant Field Values

FLT_EPSILON

public static final float FLT_EPSILON
A "close to zero" float epsilon value for use

See Also:
Constant Field Values

ZERO_TOLERANCE

public static final float ZERO_TOLERANCE
A "close to zero" float epsilon value for use

See Also:
Constant Field Values

ONE_THIRD

public static final float ONE_THIRD
See Also:
Constant Field Values

PI

public static final float PI
The value PI as a float. (180 degrees)

See Also:
Constant Field Values

TWO_PI

public static final float TWO_PI
The value 2PI as a float. (360 degrees)

See Also:
Constant Field Values

HALF_PI

public static final float HALF_PI
The value PI/2 as a float. (90 degrees)

See Also:
Constant Field Values

QUARTER_PI

public static final float QUARTER_PI
The value PI/4 as a float. (45 degrees)

See Also:
Constant Field Values

INV_PI

public static final float INV_PI
The value 1/PI as a float.

See Also:
Constant Field Values

INV_TWO_PI

public static final float INV_TWO_PI
The value 1/(2PI) as a float.

See Also:
Constant Field Values

DEG_TO_RAD

public static final float DEG_TO_RAD
A value to multiply a degree value by, to convert it to radians.

See Also:
Constant Field Values

RAD_TO_DEG

public static final float RAD_TO_DEG
A value to multiply a radian value by, to convert it to degrees.

See Also:
Constant Field Values

rand

public static final java.util.Random rand
A precreated random object for random numbers.

Method Detail

isPowerOfTwo

public static boolean isPowerOfTwo(int number)
Returns true if the number is a power of 2 (2,4,8,16...) A good implementation found on the Java boards. note: a number is a power of two if and only if it is the smallest number with that number of significant bits. Therefore, if you subtract 1, you know that the new number will have fewer bits, so ANDing the original number with anything less than it will give 0.

Parameters:
number - The number to test.
Returns:
True if it is a power of two.

nearestPowerOfTwo

public static int nearestPowerOfTwo(int number)

LERP

public static float LERP(float percent,
                         float startValue,
                         float endValue)
Linear interpolation from startValue to endValue by the given percent. Basically: ((1 - percent) * startValue) + (percent * endValue)

Parameters:
percent - Percent value to use.
startValue - Begining value. 0% of f
endValue - ending value. 100% of f
Returns:
The interpolated value between startValue and endValue.

acos

public static float acos(float fValue)
Returns the arc cosine of an angle given in radians.
Special cases:

Parameters:
fValue - The angle, in radians.
Returns:
fValue's acos
See Also:
Math.acos(double)

asin

public static float asin(float fValue)
Returns the arc sine of an angle given in radians.
Special cases:

Parameters:
fValue - The angle, in radians.
Returns:
fValue's asin
See Also:
Math.asin(double)

asin

public static double asin(double dValue)
Returns the arc sine of an angle given in radians.
Special cases:

Parameters:
dValue - The angle, in radians.
Returns:
dValue's asin
See Also:
Math.asin(double)

atan

public static float atan(float fValue)
Returns the arc tangent of an angle given in radians.

Parameters:
fValue - The angle, in radians.
Returns:
fValue's asin
See Also:
Math.atan(double)

atan

public static double atan(double dValue)
Returns the arc tangent of an angle given in radians.

Parameters:
dValue - The angle, in radians.
Returns:
dValue's asin
See Also:
Math.atan(double)

atan2

public static float atan2(float fY,
                          float fX)
A direct call to Math.atan2.

Parameters:
fY -
fX -
Returns:
Math.atan2(fY,fX)
See Also:
Math.atan2(double, double)

atan2

public static double atan2(double dY,
                           double dX)
A direct call to Math.atan2.

Parameters:
dY -
dX -
Returns:
Math.atan2(dY,dX)
See Also:
Math.atan2(double, double)

ceil

public static float ceil(float fValue)
Rounds a fValue up. A call to Math.ceil

Parameters:
fValue - The value.
Returns:
The fValue rounded up
See Also:
Math.ceil(double)

reduceSinAngle

public static float reduceSinAngle(float radians)
Fast Trig functions for x86. This forces the trig functiosn to stay within the safe area on the x86 processor (-45 degrees to +45 degrees) The results may be very slightly off from what the Math and StrictMath trig functions give due to rounding in the angle reduction but it will be very very close. note: code from wiki posting on java.net by jeffpk


reduceSinAngle

public static double reduceSinAngle(double radians)
Fast Trig functions for x86. This forces the trig functiosn to stay within the safe area on the x86 processor (-45 degrees to +45 degrees) The results may be very slightly off from what the Math and StrictMath trig functions give due to rounding in the angle reduction but it will be very very close. note: code from wiki posting on java.net by jeffpk


sin

public static float sin(float fValue)
Returns sine of a value. note: code from wiki posting on java.net by jeffpk

Parameters:
fValue - The value to sine, in radians.
Returns:
The sine of fValue.
See Also:
Math.sin(double)

sin

public static double sin(double dValue)
Returns sine of a value. note: code from wiki posting on java.net by jeffpk

Parameters:
dValue - The value to sine, in radians.
Returns:
The sine of dValue.
See Also:
Math.sin(double)

cos

public static float cos(float fValue)
Returns cos of a value.

Parameters:
fValue - The value to cosine, in radians.
Returns:
The cosine of fValue.
See Also:
Math.cos(double)

cos

public static double cos(double dValue)
Returns cos of a value.

Parameters:
dValue - The value to cosine, in radians.
Returns:
The cosine of dValue.
See Also:
Math.cos(double)

exp

public static float exp(float fValue)
Returns E^fValue

Parameters:
fValue - Value to raise to a power.
Returns:
The value E^fValue
See Also:
Math.exp(double)

abs

public static float abs(float fValue)
Returns Absolute value of a float.

Parameters:
fValue - The value to abs.
Returns:
The abs of the value.
See Also:
Math.abs(float)

abs

public static double abs(double dValue)
Returns Absolute value of a double.

Parameters:
dValue - The value to abs.
Returns:
The abs of the value.
See Also:
Math.abs(double)

floor

public static float floor(float fValue)
Returns a number rounded down.

Parameters:
fValue - The value to round
Returns:
The given number rounded down
See Also:
Math.floor(double)

invSqrt

public static float invSqrt(float fValue)
Returns 1/sqrt(fValue)

Parameters:
fValue - The value to process.
Returns:
1/sqrt(fValue)
See Also:
Math.sqrt(double)

log

public static float log(float fValue)
Returns the log base E of a value.

Parameters:
fValue - The value to log.
Returns:
The log of fValue base E
See Also:
Math.log(double)

log

public static float log(float value,
                        float base)
Returns the logarithm of value with given base, calculated as log(value)/log(base), so that pow(base, return)==value (contributed by vear)

Parameters:
value - The value to log.
base - Base of logarithm.
Returns:
The logarithm of value with given base

pow

public static float pow(float fBase,
                        float fExponent)
Returns a number raised to an exponent power. fBase^fExponent

Parameters:
fBase - The base value (IE 2)
fExponent - The exponent value (IE 3)
Returns:
base raised to exponent (IE 8)
See Also:
Math.pow(double, double)

sqr

public static float sqr(float fValue)
Returns the value squared. fValue ^ 2

Parameters:
fValue - The vaule to square.
Returns:
The square of the given value.

sqrt

public static float sqrt(float fValue)
Returns the square root of a given value.

Parameters:
fValue - The value to sqrt.
Returns:
The square root of the given value.
See Also:
Math.sqrt(double)

sqrt

public static double sqrt(double dValue)
Returns the square root of a given value.

Parameters:
dValue - The value to sqrt.
Returns:
The square root of the given value.
See Also:
Math.sqrt(double)

tan

public static float tan(float fValue)
Returns the tangent of a value. If USE_FAST_TRIG is enabled, an approximate value is returned. Otherwise, a direct value is used.

Parameters:
fValue - The value to tangent, in radians.
Returns:
The tangent of fValue.
See Also:
Math.tan(double)

sign

public static int sign(int iValue)
Returns 1 if the number is positive, -1 if the number is negative, and 0 otherwise

Parameters:
iValue - The integer to examine.
Returns:
The integer's sign.

sign

public static float sign(float fValue)
Returns 1 if the number is positive, -1 if the number is negative, and 0 otherwise

Parameters:
fValue - The float to examine.
Returns:
The float's sign.

determinant

public static float determinant(double m00,
                                double m01,
                                double m02,
                                double m03,
                                double m10,
                                double m11,
                                double m12,
                                double m13,
                                double m20,
                                double m21,
                                double m22,
                                double m23,
                                double m30,
                                double m31,
                                double m32,
                                double m33)
Returns the determinant of a 4x4 matrix.


nextRandomFloat

public static float nextRandomFloat()
Returns a random float between 0 and 1.

Returns:
A random float between 0.0f (inclusive) to 1.0f (exclusive).

nextRandomInt

public static int nextRandomInt(int min,
                                int max)
Returns a random float between min and max.

Returns:
A random int between min (inclusive) to max (inclusive).

nextRandomInt

public static int nextRandomInt()

sphericalToCartesian

public static processing.core.PVector sphericalToCartesian(processing.core.PVector sphereCoords,
                                                           processing.core.PVector store)
Converts a point from Spherical coordinates to Cartesian (using positive Y as up) and stores the results in the store var.


cartesianToSpherical

public static processing.core.PVector cartesianToSpherical(processing.core.PVector cartCoords,
                                                           processing.core.PVector store)
Converts a point from Cartesian coordinates (using positive Y as up) to Spherical and stores the results in the store var. (Radius, Azimuth, Polar)


sphericalToCartesianZ

public static processing.core.PVector sphericalToCartesianZ(processing.core.PVector sphereCoords,
                                                            processing.core.PVector store)
Converts a point from Spherical coordinates to Cartesian (using positive Z as up) and stores the results in the store var.


cartesianZToSpherical

public static processing.core.PVector cartesianZToSpherical(processing.core.PVector cartCoords,
                                                            processing.core.PVector store)
Converts a point from Cartesian coordinates (using positive Z as up) to Spherical and stores the results in the store var. (Radius, Azimuth, Polar)


normalize

public static float normalize(float val,
                              float min,
                              float max)
Takes an value and expresses it in terms of min to max.

Parameters:
val - - the angle to normalize (in radians)
Returns:
the normalized angle (also in radians)

copysign

public static float copysign(float x,
                             float y)
Parameters:
x - the value whose sign is to be adjusted.
y - the value whose sign is to be used.
Returns:
x with its sign changed to match the sign of y.

clamp

public static float clamp(float input,
                          float min,
                          float max)
Take a float input and clamp it between min and max.

Parameters:
input -
min -
max -
Returns:
clamped input