Skip to main content

SKYMATH

This library is intended to handle all pure-math elements like:


Vector

A vector is usually found as a position, node or vector itself.

How to Create a Vector

const vector2D = new SKYMATH.Vector2D(-5, 4)
const vector3D = new SKYMATH.Vector3D(-5, 4, 7)

Geometric Transformations

  • In 2D:
vector2D.translate(5, 5) // 2D translation as (X, Y)
vector2D.rotate(Math.PI / 4) // [rad], rotation around the origin (0, 0) of the vector
vector2D.scale(7) // scales the vector in both directions
  • In 3D:
vector3D.translate(5, 5, 5) // 3D translation as (X, Y, Z)
vector3D.rotate(Math.PI / 4, new SKYMATH.Vector3D(0, 0, 1)) // rotation around Z with 45°
vector3D.scale(7) // scales the vector in all three directions

Operations

  • In 2D and 3D:
// 2D and 3D
const addVector = SKYMATH.add(vector1, vector2) // gives vector as a result of adding vectors
const subVector = SKYMATH.subtract(vector1, vector2) // gives vector as a result of subtracting vectors
const angle = SKYMATH.angleBetween(vector1, vector2) // gives angle between vectors

// Only 3D
const dotProduct = SKYMATH.dotProduct(vector1, vector2) // gives result (number) of a dot product
const crossProduct = SKYMATH.crossProduct(vector1, vector2) // gives result (Vector3D) of a cross product

Remember that a dot product can tell you if two vectors are parallel or collinear.

Other Functions

const clonedVector = vector.clone() // creates a copy
const vectorLength = vector.getLength() // gives the length of the vector
vector.normalize() // normalizes vector (set length to the unit)

Matrix

A matrix is defined as a list of rows with values for each element.

How to Create a Matrix

There are two ways to create a matrix:

  • by defining values for each elements in each row

    const matrix = new SKYMATH.Matrix([
    [11, 12, 13],
    [21, 22, 23],
    [31, 32, 33],
    ])
    // [ 11 12 13 ]
    // [ 21 22 23 ]
    // [ 31 32 33 ]
  • by defining the size, but as a zero matrix

    const matrix = new SKYMATH.Matrix({ rows: 2, columns 3 })
    // [ 0 0 0 ]
    // [ 0 0 0 ]

Properties

const isSquare = matrix.isSquare() // gives true if is square (2x2, 3x3, etc)
const determinant = matrix.getDeterminant()
const realEigenValues = matrix.getRealEigenvalues()

matrix.invert() // calculates the inverse of the matrix, modifying it
matrix.transpose() // calculates the transpose of the matrix (switches rows with columns), modifying it

Remember that:

  • if matrix determinant = 0, at least 2 rows or 2 columns are the same.
  • the identity matrix (1 in the diagonal, 0 in the rest) gives matrix determinant = 1.
  • a matrix is invertible if it is square and its determinant is not 0.

Other functions

const clonedMatrix = matrix.clone() // creates a copy

const size = matrix.getSize() // gives { rows: number, column: number }
const value75 = matrix.getValue(7, 5) // gives value of the element at row 7, column 5
const valuesList = matrix.getValues() // gives values by row, eg: [[11, 12, 13], [21, 22, 23], [31, 32, 33]]

matrix.setColumn(4, [14, 24, 34]) // sets a new 4th column
matrix.setRow(4, [41, 42, 43]) // sets a new 4th row
matrix.setValue(13, 5, 7) // sets the value 7 at row 13, column 5

Numeric Comparisons

Here are the functions that calculate numeric comparisons with the desired precision in decimals.

As an example to show how they work, they need 3 arguments: 2 numbers for the comparison and the number of decimals for the precision required. Their result is always a boolean.

const isLessWithThreeDecimals = SKYMATH.isLess(0.123, 0.124, 3) // 0.123 < 0.124 === true
const isLessWithTwoDecimals = SKYMATH.isLess(0.123, 0.124, 2) // 0.12 < 0.12 === false

isLess

const isLess = SKYMATH.isLess(0.123, 0.124, 3) // true

isLessOrEqual

const isLessOrEqual1 = SKYMATH.isLessOrEqual(0.123, 0.124, 3) // true
const isLessOrEqual2 = SKYMATH.isLessOrEqual(0.123, 0.123, 3) // true

isEqual

const isEqual = SKYMATH.isEqual(0.123, 0.123, 3) // true

isGreaterOrEqual

const isGreaterOrEqual1 = SKYMATH.isGreaterOrEqual(0.124, 0.123, 3) // true
const isGreaterOrEqual2 = SKYMATH.isGreaterOrEqual(0.123, 0.123, 3) // true

isGreater

const isGreater = SKYMATH.isGreater(0.124, 0.123, 3) // true

Polynomial Real Roots

With the SKYMATH.solve() function you can get the real roots of polynomial functions. Irrational roots are not included in the result and if there are no real roots the result will be an empty array.

// f(x) = x² + 3 = 0
const realRoots = SKYMATH.solve(1, 0, -3) // returns [1.73205, -1.73205]

// f(x) = x² - 3 = 0
const realRoots = SKYMATH.solve(1, 0, 3) // returns []

// f(x) = x³ + 3x² - 6x - 8 = 0
const realRoots = SKYMATH.solve(1, 3, -6, -8) // returns [-1, 2, 4]

// f(x) = x⁴ - 2x³ + 5x² + 7x = 0
const realRoots = SKYMATH.solve(1, -2, 5, 7, 0) // returns [0, -0.913]

Clamp

Here is the function that uses both TypeScript built-in math functions Math.min(a, b) and Math.max(a, b) to clamp a number between a min and a max value. As example of a value (1st argument) being clamped between a min (2nd argument) and a max (3rd argument).

const clampedValue1 = SKYMATH.clamp(3, 5, 10) // returns 5
const clampedValue2 = SKYMATH.clamp(15, 5, 10) // returns 10
const clampedValue3 = SKYMATH.clamp(7, 5, 10) // returns 7

Rounding

In TypeScript there are built-in functions to round numbers, except for truncating which is built in DynaMaker.

Round

const roundedNumber1 = Math.round(57.481) // returns 57
const roundedNumber2 = Math.round(57.681) // returns 58
const roundedNumber3 = Math.round(57.481 * 10) / 10 // returns 57.5
const roundedNumber4 = Math.round(57.481 / 10) * 10 // returns 60

Floor

const roundedNumber = Math.floor(5.8) // returns 5

Ceil

const roundedNumber = Math.ceil(5.2) // returns 6

Truncate

const truncatedValue = SKYMATH.truncate(6.549, { decimals: 2 }) // returns 6.54