Class: MathUtils

MathUtils

A collection of useful math functions.

Methods

(static) clamp(value, minopt, maxopt) → {number}

Clamp a number between 2 values.

Parameters:
Name Type Attributes Default Description
value number

Value to clamp.

min number <optional>
0

Minumum value.

max number <optional>
1

Maximum value.

Returns:
Type
number

(static) closestPointOnLine(a, b, p) → {Array.<number>}

Calculates the closest point on a given 2D line segement
from a given 2D point.

Parameters:
Name Type Description
a Array.<number>

First point on line segment.

b Array.<number>

Second point on line segment.

p Array.<number>

2D point.

Returns:
Type
Array.<number>

(static) dampValue(currentValue, targetValue-, valueStoreopt, deltaTimeopt, smoothTimeopt, maxSpeedopt) → {Array.<number>}

Gradually change a value of a numeric property towards a goal over time using
a critically damped spring function.

Parameters:
Name Type Attributes Default Description
currentValue number

The starting value.

targetValue- number

The goal value.

valueStore Array.<number> <optional>
[0, 0]

An Array consisting of two
numbers where the first number holds the result value and the second holds
the velocity that resulted in that value. The same array should be provided
with each call to this function.

deltaTime number <optional>
1e-7

The time since the last call to this function
in seconds.

smoothTime number <optional>
0.3

The approximate amount of time in seconds
it should take to reach the target value.

maxSpeed number <optional>
1e7

A clamping value for the maximum speed the
value can change.

Returns:
  • The valueStore array.
Type
Array.<number>

(static) distanceSquared(a, b) → {number}

Gets the distance squared for two 2D points.

Parameters:
Name Type Description
a Array.<number>

2D point.

b Array.<number>

2D point.

Returns:
Type
number

(static) getAngleBetween(vectorA, vectorB) → {number}

Return the angle in radians between vectorA and vectorB.

Parameters:
Name Type Description
vectorA Array.<number>

Array consisting of numbers.

vectorB Array.<number>

Array consisting of numbers.

Returns:
Type
number

(static) getDelaunayTriangulation(vertices) → {Array.<Array.<number>>}

Triangulates a set of 2D points using an implementation
of the Bowyer-Watson incremental Delaunay triangulation.

Parameters:
Name Type Description
vertices Array.<Array.<number>>

Array of 2D points.

Returns:
  • Array of triangle indices.
Type
Array.<Array.<number>>

(static) getDotProduct(vectorA, vectorB) → {number}

Return the dot product between two vectors.

Parameters:
Name Type Description
vectorA Array.<number>

Array consisting of numbers.

vectorB Array.<number>

Array consisting of numbers.

Returns:
Type
number

(static) getRotationMatrix(matrix4) → {Array.<number>}

Extract the 3x3 rotation matrix from a given 4x4 transformation matrix.

Parameters:
Name Type Description
matrix4 Array.<number>

An array of 16 numbers representing a row
major transformation matrix.

Returns:
  • An array of 9 numbers representing a row major
    rotation matrix.
Type
Array.<number>

(static) getVectorMagnitude(vector) → {number}

Return the magnitude of a given vector array.

Parameters:
Name Type Description
vector Array.<number>

Array consisting of numbers.

Returns:
Type
number

(static) isPointInCircumCircle(a, b, c, p) → {boolean}

Determines if a given 2D point is within the cicrumcircle
defined by three 2D points. The triangle points must be in
counter-clockwise order a -> b -> c.

Parameters:
Name Type Description
a Array.<number>

First triangle point.

b Array.<number>

Second triangle point.

c Array.<number>

Third triangle point.

p Array.<number>

2D point.

Returns:
Type
boolean

(static) isPointInTriangle(a, b, c, p) → {boolean}

Determines if a given 2D point is within a given triangle.

Parameters:
Name Type Description
a Array.<number>

First triangle point.

b Array.<number>

Second triangle point.

c Array.<number>

Third triangle point.

p Array.<number>

2D point.

Returns:
Type
boolean

(static) lerp(from, to, factor) → {number}

Linearly interpolate between two values.

Parameters:
Name Type Description
from number

Start value.

to number

Target value.

factor number

0-1 amount to interpolate between from and to.

Returns:
Type
number

(static) normalizeVector(vector) → {Array.<number>}

Normalize a given vector array.

Parameters:
Name Type Description
vector Array.<number>

Array consisting of numbers.

Returns:

The original vector with normalized values, for chaining.

Type
Array.<number>

(static) rotateVector(vector3, matrix3) → {Array.<number>}

Multiply a 3x3 rotation matrix with a vector3.

Parameters:
Name Type Description
vector3 Array.<number>

Array consisting of 3 numbers representing
a direction vector.

matrix3 Array.<number>

An array of 9 numbers representing a row
major rotation matrix.

Returns:
  • An array of 3 numbers representing the new direction
    of the vector.
Type
Array.<number>

(static) sortPointsCCW(indices, vertices) → {Array.<Array.<number>>}

Gets the sorted indices of a given set of 2D points in
counter-clockwise order.

Parameters:
Name Type Description
indices Array.<number>

List of indices.

vertices Array.<Array.<number>>

List of 2D points.

Returns:
  • List of sorted indices.
Type
Array.<Array.<number>>

(static) toDegrees(radians) → {number}

Convert the given angle from radians to degrees.

Parameters:
Name Type Description
radians number

Angle in radians.

Returns:
  • Angle in degrees.
Type
number

(static) toRadians(degrees) → {number}

Convert the given angle from degrees to radians.

Parameters:
Name Type Description
degrees number

Angle in degrees.

Returns:
  • Angle in radians.
Type
number

(static) triangleArea(a, b, c) → {number}

Cacluates the area of a triangle

Parameters:
Name Type Description
a Array.<number>

First triangle point.

b Array.<number>

Second triangle point.

c Array.<number>

Third triangle point.

Returns:
Type
number