com.NeuroSky.Util
Class MathHelper

java.lang.Object
  extended by com.NeuroSky.Util.MathHelper

public class MathHelper
extends java.lang.Object

This class implements a bunch of utility functions to perform various transformation and computational operations. Some of the functions are simply to fill holes in J2ME's implementation of the Math class.


Constructor Summary
MathHelper()
           
 
Method Summary
static float clamp(float t, float floor, float ceiling)
          Limit a value to between floor and ceiling.
static Vector2 cubicBezier(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t)
          Generate a point in a cubic Bezier curve defined by two anchor and two control points, and a parameter.
static int dampColor(int startColor, float dampFactor)
          Apply a multiplicative factor to each component in a color represented by an integer (0x00RRGGBB).
static float exp(float x)
          Calculate the value of e^x given an input x.
static float lerp(float start, float end, float t)
          Linearly interpolate a value between the current value and target value.
static int lerpColor(int startColor, int endColor, float t)
          Linearly interpolate between two colors.
static float log(float x)
          Calculate the natural log of an input x, i.e.
static float normalizeValue(float value)
          Perform a custom normalization procedure on a value, involving applying a natural logarithm, then scaling the number to a range between 0 and 1.
static Vector2 rotate(float angle, Vector2 point)
          Rotate a point about the origin (0,0) over an angle.
static float smoothDamp(float current, float target, float[] currentVelocity, int index, float smoothTime, float deltaTime)
          Critically damp a value towards a target value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MathHelper

public MathHelper()
Method Detail

lerp

public static float lerp(float start,
                         float end,
                         float t)
Linearly interpolate a value between the current value and target value. This could be seen as a linear Bezier curve.

Parameters:
start - the starting value
end - the ending value
t - the parameter
Returns:
a value that represents a linearly interpolated value between "start" and "end"

lerpColor

public static int lerpColor(int startColor,
                            int endColor,
                            float t)
Linearly interpolate between two colors.

Parameters:
startColor - the starting color, defined by 0x00RRGGBB
endColor - the finishing color, defined by 0x00RRGGBB
t - the parameter
Returns:
a 0x00RRGGBB value that represents a linearly interpolated value

clamp

public static float clamp(float t,
                          float floor,
                          float ceiling)
Limit a value to between floor and ceiling. This may be useful for making sure that a number never exceeds a certain range.

Parameters:
t - the value to be clamped
floor - the minimum value the returned value should take
ceiling - the maximum value the returned value should take
Returns:
the clamped value

dampColor

public static int dampColor(int startColor,
                            float dampFactor)
Apply a multiplicative factor to each component in a color represented by an integer (0x00RRGGBB).

Parameters:
startColor - the initial color, in the form 0x00RRGGBB
dampFactor - the multiplicative factor applied to the color
Returns:
the damped color (startColor * dampFactor)

exp

public static float exp(float x)
Calculate the value of e^x given an input x. This only gives us good precision to about x = +/- 2, since it's calculated using a fairly coarse Power Series approximation.

Parameters:
x - the value that e is raised to
Returns:
the result of the calculation e^x

log

public static float log(float x)
Calculate the natural log of an input x, i.e. ln(x). This function gives us reasonably good precision, and is accurate to around 3 significant figures. The precision of this function is limited by the accuracy of a T(4) Taylor Series approximation of the natural logarithm function between 1 and 2.

Parameters:
x - the value from which the natural log should be calculated
Returns:
the natural log of x

smoothDamp

public static float smoothDamp(float current,
                               float target,
                               float[] currentVelocity,
                               int index,
                               float smoothTime,
                               float deltaTime)
Critically damp a value towards a target value. This method uses the equation for a critically damped system: x(t) = (A + Bt) * e^(-w0 * t), where A = x(0) B = x'(0) + x(0)

Parameters:
current - the current value
target - the target value
currentVelocity - an array of floats storing the current velocity x'(0)
index - the index into the currentVelocity array referencing the desired float element
smoothTime - - the time taken to reach the target value
deltaTime - - the elapsed time since the last call to this method
Returns:
a critically damped value

rotate

public static Vector2 rotate(float angle,
                             Vector2 point)
Rotate a point about the origin (0,0) over an angle.

Parameters:
angle - the angle (in radians) over which to rotate the point
point - the point to be rotated
Returns:
a point representing the rotated original point

cubicBezier

public static Vector2 cubicBezier(Vector2 p0,
                                  Vector2 p1,
                                  Vector2 p2,
                                  Vector2 p3,
                                  float t)
Generate a point in a cubic Bezier curve defined by two anchor and two control points, and a parameter.

Parameters:
p0 - the starting anchor point
p1 - the first control point
p2 - the second control point
p3 - the ending anchor point
t - the parameter for the Bezier curve
Returns:
a point along the cubic Bezier curve

normalizeValue

public static float normalizeValue(float value)
Perform a custom normalization procedure on a value, involving applying a natural logarithm, then scaling the number to a range between 0 and 1.

Parameters:
value - the value to be normalized
Returns:
a normalized float between 0 and 1.