amd-utils / math

Math utilities.

Table of Contents #

clamp(val, min, max):Number #

Clamps value inside range.

Example:

clamp(-50, 1, 10); // 1
clamp(7, 1, 10);   // 7
clamp(12, 1, 10);  // 10

countSteps(val, step, [overflow]):Number #

Count number of full steps.

Arguments:

  1. val (Number) : Value.
  2. step (Number) : Step size.
  3. [overflow] (Number) : Maximum number of steps, nSteps will loop if >= than overflow.

Example:

countSteps(3,  5);    // 0
countSteps(6,  5);    // 1
countSteps(12, 5);    // 2
countSteps(18, 5);    // 3

countSteps(3, 5, 3);  // 0
countSteps(6, 5, 3);  // 1
countSteps(12, 5, 3); // 2
countSteps(18, 5, 3); // 0

inRange(val, min, max, [threshold]):Boolean #

Checks if value is inside the range.

Example:

inRange(-6, 1, 10);    // false
inRange( 5, 1, 10);    // true
inRange(12, 1, 10);    // false
inRange(12, 1, 10, 2); // true
inRange(13, 1, 10, 2); // false

isNear(val, target, threshold):Boolean #

Check if value is close to target.

Example:

isNear( 10.2, 10, 0.5); // true
isNear( 10.5, 10, 0.5); // true
isNear(10.51, 10, 0.5); // false

lerp(ratio, start, end):Number #

Linear interpolation.

Example:

lerp(0.5, 0, 10);  // 5
lerp(0.75, 0, 10); // 7.5

loop(val, min, max):Number #

Loops value inside range. Will return min if val > max and max if val < min, otherwise it returns val.

Example:

loop( 9, 0, 10); // 9
loop(10, 0, 10); // 10
loop(11, 0, 10); // 0
loop(-1, 0, 10); // 10

map(val, min1, max1, min2, max2):Number #

Maps a number from one scale to another.

Example:

map(3, 0, 4, -1, 1)   // 0.5
map(3, 0, 10, 0, 100) // 30

norm(val, min, max):Number #

Gets normalized ratio of value inside range.

Example:

norm(50, 0, 100); // 0.5
norm(75, 0, 100); // 0.75

snap(val, step):Number #

Snap value to full steps.

Example:

snap(7, 5);  // 5
snap(11, 5); // 10
snap(15, 5); // 15

Documentation generated by mdoc.