za.co.iocom.math
Class MathUtil

java.lang.Object
  extended by za.co.iocom.math.MathUtil

public class MathUtil
extends java.lang.Object

Class MathUtil contains some utility functions to calculate special mathematical functions.


Field Summary
static java.util.Random random
           
 
Constructor Summary
MathUtil()
           
 
Method Summary
static int getFactorial(int n)
          Calculates the factorial of n, that is n!, and returns the result.
static int getTriangular(int n)
          Calculates the nth triangular number, and returns the result.
static double lerp(double v1, double v2, double ratio)
          Linearly interpolates between two values.
static double lerp(double value, double inputMin, double inputMax, double outputMin, double outputMax)
          Linearly interpolates a value in a given range.
static double line(double value, double inputMin, double inputMax, double outputMin, double outputMax)
          The returned value is outputMin + ((value - inputMin) / (inputMax - inputMin)) * (outputMax - outputMin).
static double max(double v1, double v2, double v3)
          Returns the maximum of three values.
static double min(double v1, double v2, double v3)
          Returns the minimum of three values.
static double ramp(double value, double inputMin, double inputMax, double outputMin, double outputMax)
          If the value is below the inputMin, the outputMin is returned.
static double sigmoid(double value, double inputMin, double inputMax, double outputMin, double outputMax)
          This function is a smooth approximation for lerp.
static double sqr(byte x)
          Returns the square of the given number.
static double sqr(double x)
          Returns the square of the given number.
static double sqr(float x)
          Returns the square of the given number.
static int sqr(int x)
          Returns the square of the given number.
static double sqr(long x)
          Returns the square of the given number.
static double sqr(short x)
          Returns the square of the given number.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

random

public static java.util.Random random
Constructor Detail

MathUtil

public MathUtil()
Method Detail

getTriangular

public static int getTriangular(int n)
                         throws za.co.iocom.math.NegativeNumberException
Calculates the nth triangular number, and returns the result. The nth triangular number is simply all integers up to n summed, that is Tn = 1 + 2 + 3 + ... + n. The 0th triangular number is by convention 0, while it is not defined for negative values of n.

Parameters:
n - The triangular number to be calculated. n has to be non-nagative.
Returns:
the nth triangular number.
Throws:
NegativeNumberException - when a negative argument is passed to the function.

getFactorial

public static int getFactorial(int n)
                        throws za.co.iocom.math.NegativeNumberException
Calculates the factorial of n, that is n!, and returns the result. The factorial of n is simply all integers up to n multiplied, that is Tn = 1 * 2 * 3 * ... * n. The 0! is by convention 1, while it is not defined for negative values of n.

Parameters:
n - The factorial to be calculated. n has to be non-nagative.
Returns:
the factorial of n number.
Throws:
NegativeNumberException - when a negative argument is passed to the function.

sqr

public static int sqr(int x)
Returns the square of the given number.


sqr

public static double sqr(double x)
Returns the square of the given number.


sqr

public static double sqr(float x)
Returns the square of the given number.


sqr

public static double sqr(long x)
Returns the square of the given number.


sqr

public static double sqr(short x)
Returns the square of the given number.


sqr

public static double sqr(byte x)
Returns the square of the given number.


lerp

public static double lerp(double value,
                          double inputMin,
                          double inputMax,
                          double outputMin,
                          double outputMax)
Linearly interpolates a value in a given range. If the value is below the inputMin, the outputMin is returned. If the value is above the inputMax, the outputMax is returned. Otherwise, the returned value is outputMin + ((value - inputMin) / (inputMax - inputMin)) * (outputMax - outputMin).


sigmoid

public static double sigmoid(double value,
                             double inputMin,
                             double inputMax,
                             double outputMin,
                             double outputMax)
This function is a smooth approximation for lerp.


ramp

public static double ramp(double value,
                          double inputMin,
                          double inputMax,
                          double outputMin,
                          double outputMax)
If the value is below the inputMin, the outputMin is returned. Otherwise, the returned value is outputMin + ((value - inputMin) / (inputMax - inputMin)) * (outputMax - outputMin).


line

public static double line(double value,
                          double inputMin,
                          double inputMax,
                          double outputMin,
                          double outputMax)
The returned value is outputMin + ((value - inputMin) / (inputMax - inputMin)) * (outputMax - outputMin).


min

public static double min(double v1,
                         double v2,
                         double v3)
Returns the minimum of three values.


max

public static double max(double v1,
                         double v2,
                         double v3)
Returns the maximum of three values.


lerp

public static double lerp(double v1,
                          double v2,
                          double ratio)
Linearly interpolates between two values.