Class MathEx

The MathEx class provides additional helpers utility function for math operations. It extends the built-in Math class with additional functionality.

Constructors

Properties

Deg2Rad: number

Constant that converts degrees to radians.

HalfPi: rsx.Radian

Half of pi constant.

Pi constant.

Rad2Deg: number

Constant that converts radians to degrees.

TwoPi: rsx.Radian

Two times pi constant.

Methods

  • Returns the angle whose cosine is the specified number.

    Parameters

    • cosine: number

      Cosine of an angle.

    Returns rsx.Radian

    Angle in radians.

  • Returns the angle whose sine is the specified number.

    Parameters

    • sine: number

      Sine value.

    Returns rsx.Radian

    Cosine in radians.

  • Returns the angle whose tangent is the specified number.

    Parameters

    • tangent: number

      Tangent of an angle.

    Returns rsx.Radian

    Angle in radians.

  • Returns an angle of the point x and y in radians.

    Parameters

    • y: number

      Y coordinate of the point.

    • x: number

      X coordinate of the point.

    Returns rsx.Radian

    Angle in radians in range [Pi, -Pi].

  • Calculates the point at t of the Cubic Bezier curve in 2D-space.

    Parameters

    • start: Immutable<Vector2>

      The starting point of the Bezier curve.

    • controlPoint1: Immutable<Vector2>

      The first cubic Bezier control point.

    • controlPoint2: Immutable<Vector2>

      The second cubic Bezier control point.

    • end: Immutable<Vector2>

      The ending point of the Bezier curve.

    • t: number

      The point at which to calculate the curve derivative for. Must be in the range [0, 1].

    Returns Vector2

    The tangent vector to at the desired point.

  • Calculates the point at t of the Cubic Bezier curve in 3D-space.

    Parameters

    • start: Immutable<Vector3>

      The starting point of the Bezier curve.

    • controlPoint1: Immutable<Vector3>

      The first cubic Bezier control point.

    • controlPoint2: Immutable<Vector3>

      The second cubic Bezier control point.

    • end: Immutable<Vector3>

      The ending point of the Bezier curve.

    • t: number

      The point at which to calculate the curve derivative for. Must be in the range [0, 1].

    Returns Vector3

    The tangent vector to at the desired point.

  • Calculates the tangent at point t of the Cubic Bezier curve.

    Parameters

    • start: Immutable<Vector2>

      The starting point of the Bezier curve.

    • controlPoint1: Immutable<Vector2>

      The first cubic Bezier control point.

    • controlPoint2: Immutable<Vector2>

      The second cubic Bezier control point.

    • end: Immutable<Vector2>

      The ending point of the Bezier curve.

    • t: number

      The point at which to calculate the curve derivative for. Must be in the range [0, 1].

    Returns Vector2

    The tangent vector to at the desired point.

  • Calculates the tangent at point t of the Cubic Bezier curve.

    Parameters

    • start: Immutable<Vector3>

      The starting point of the Bezier curve.

    • controlPoint1: Immutable<Vector3>

      The first cubic Bezier control point.

    • controlPoint2: Immutable<Vector3>

      The second cubic Bezier control point.

    • end: Immutable<Vector3>

      The ending point of the Bezier curve.

    • t: number

      The point at which to calculate the curve derivative for. Must be in the range [0, 1].

    Returns Vector3

    The tangent vector to at the desired point.

  • Return a cubic root of the provided value.

    Parameters

    • val: number

      Value to take the cubic root of.

    Returns number

    Cubic root of the provided value.

  • Clamps the value between min and max values.

    Type Parameters

    Parameters

    • value: T

      Value to clamp.

    • min: T

      Minimum value of the range to clamp. Must be lower than max

    • max: T

      Maximum value of the range to clamp. Must be higher than min

    Returns T

    Returns unchanged value if it is in valid range, otherwise returns value clamped to the range extremes.

  • Clamps a value between zero and one.

    Type Parameters

    Parameters

    • value: T

      Value to clamp.

    Returns T

    Returns unchanged value if it is in [0, 1] range, otherwise returns value clamped to the range.

  • Returns the cosine of the specified angle in radians.

    Parameters

    Returns number

    Cosine in radians.

  • Performs cubic interpolation between two values bound between two other values where f is the alpha value in range [0, 1]. If it is 0 the method returns val2. If it is 1 the method returns val3.

    Parameters

    • val1: number

      First value.

    • val2: number

      Second value.

    • val3: number

      Third value.

    • val4: number

      Fourth value.

    • f: number

      Value in range [0, 1].

    Returns number

    Value resulting from cubic interpolation.

  • Converts the value in degrees to radians.

    Parameters

    • value: number

    Returns number

  • Divides an integer value by another value and returns the result, rounded up.

    Parameters

    • n: number

      Numerator.

    • d: number

      Denominator.

    Returns number

    Divided and rounded value.

    Only works if both values are positive.

  • Evaluates the specified math expression and returns the result.

    Type Parameters

    Parameters

    • resultType: ClassOf<T>

      The return type of the expression. Only numbers and vectors of this type may be referenced in the expression.

    • fnExpression: (() => number)

      The lambda expression to evaluate.

        • (): number
        • Returns number

    Returns ValueTypeOf<T>

    A vector type containing the result of the expression.

    This is a helper to make writing math expressions in Javascript easier. There is a slight overhead compared to doing math expressions manually as the expression must be evaluted N-times for each component of the vector type. However, the cost amortizes well as no temporary objects must be constructed that hold the result. The expression is evaluated using only numbers, and then assigned to the respective component of the vector result.

    The fastest possible way of doing math is still using the do math type APIs such as doAdd as these APIs will avoid the construction of a temporary object, but will instead, apply the expression on the calling object and return a reference to the same object. This allows to quickly chain a math expression together.

    To make a variable evaluatable it must either be casted to the Evaluatable<T> type where T is an evaluatable type such as Vector3. Alternatively, the variable can be prefixed with the unary plus to allow the compiler to use the variable in the expression.

    The following example shows basic usage:

    		// This vector is marked as `Evaluatable` and can be directly referenced in the expression.
    const myVector = new Vector3(10, 20, 30) as Evaluatable<Vector3>;
    // This vector is not evaluatable, it must be prefixed with the `unary plus`.
    const myVector2 = new Vector3(-10, -20, 20);

    const result:Vector3 = MathEx.eval(Vector3, () => (myVector + +myVector2) * 2)

    The result in the above example will be [0, 0, 100].

    It's important to note that performing function invocations can dramatic overhead as the function must be invoked once for each component of the resultType.

    The following example shows a bad practice and should be avoided.

    		const result:Vector3 = MathEx.eval(Vector3, () => myVector.normalized() * myVector2)

    The normalized function will be slowing down the execution as it must be invoked once for each component of the resultType. In the case of the Vector3 it results in calling the expensive function three times.

    While calling eval recursively, it results in evaluating the sub expression once for each component. So calling eval inside of a Vector3 evaluation for a Vector3 type results in 3 * 3 invocations.

  • Returns the fractional part of the provided value.

    Parameters

    • value: number

      Parameter to take fractional value from.

    Returns number

    Fractional value of value.

  • Returns an inverse square root (1/sqrt(x)) of the provided value.

    Parameters

    • f: number

      Value to take the inverse square root of. Must not be negative or zero.

    Returns number

    Inverse square root of the provided value.

  • Compares two numbers with an error margin.

    Parameters

    • a: number

      First number to compare.

    • b: number

      Second number to compare.

    • Optionalepsilon: number

      Error margin within which the numbers should be considered equal.

    Returns boolean

    True if equal, false otherwise.

  • Linearly interpolates between two values.

    Type Parameters

    Parameters

    • a: T

      Starting value to interpolate from.

    • b: T

      Ending value to interpolate towards.

    • t: number

      Interpolation factor in specified range.

    • Optionaltmin: number

      Minimum value for the range of t

    • Optionaltmax: number

      Maximum value for the range of t

    Returns T

    Interpolated value.

  • Returns the logarithm of a number in base 10.

    Parameters

    • f: number

      Value to get the logarithm of.

    Returns number

    Logarithm of a number in base 10.

  • Returns the logarithm of a number in a specified base.

    Parameters

    • value: number

      Value to get the logarithm of.

    • base: number

      Base of the logarithm

    Returns number

    Logarithm of a number in the specified base.

  • Returns the maximum value of the two provided.

    Type Parameters

    Parameters

    • a: T

      First value to compare.

    • b: T

      Second value to compare.

    Returns T

    Maximum of the two values.

  • Returns the minimum value of the two provided.

    Type Parameters

    Parameters

    • a: T

      First value to compare.

    • b: T

      Second value to compare.

    Returns T

    Minimum of the two values.

  • Returns dividend % divisor (floating point modulo).

    Parameters

    • dividend: number

      Value to repeat.

    • divisor: number

      Length after which to repeat the value.

    Returns number

    Result of f % length.

  • Normalize the value between based on the range. Returns 0 if f is less or equal that minimum, 1 if f is equal or greater than maximum, and value in range (0, 1) otherwise.

    Parameters

    • value: number

      Value to take inverse lerp of.

    • minimum: number

      Minimum value of the range.

    • maximum: number

      Maximum value of the range.

    Returns number

    The normalized value.

  • Wraps the value in range [0, length] and reverses the direction every length increment. This results in f incrementing until length, then decrementing back to 0, and so on.

    Parameters

    • value: number

      Value to ping-pong.

    • length: number

      Length after which to reverse the direction.

    Returns number

    Result of the ping-pong operation.

  • Performs quintic interpolation where value is the value to map onto a quintic S-curve.

    Parameters

    • value: number

      Value in range [0, 1].

    Returns number

    Value resulting from quintic interpolation.

  • Converts the value in radians to degrees.

    Parameters

    • value: number

    Returns number

  • Rounds to the nearest multiple.

    Parameters

    • value: number

      Value to round.

    • multiple: number

      Multiple to round to.

    Returns number

    Value rounded to the nearest multiple.

  • Returns the sign of the provided value (positive or negative).

    Parameters

    • value: number

      Value to get the sign of.

    Returns number

    -1.0f if negative or 1.0f if positive.

  • Returns the sine of the specified angle in radians.

    Parameters

    Returns number

    Sine in radians.

  • Performs smooth Hermite interpolation between values.

    Parameters

    • val1: number

      First value.

    • val2: number

      Second Value.

    • t: number

      Value in range [0, 1] that determines how much to interpolate.

    Returns number

    Hermite interpolated value at position t.

  • Returns the tangent of the specified angle in radians.

    Parameters

    Returns number

    Tangent.

  • Wraps an angle in [0, 360) range. Values lower than zero, or higher or equal to 360 will get wrapped around back into [0, 360) range.

    Parameters

    Returns rsx.Degree

    Angle in [0, 360) range.