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

Vectors operations

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)

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

Polinomial Real Roots

With the SKYMATH.solve() function you can get the real roots of polinomial 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