me.Math

a collection of math utility functions

Members

static DEG_TO_RAD :Number

constant to convert from degrees to radians

static ETA :Number

constant equals to half pi

static RAD_TO_DEG :Number

constant to convert from radians to degrees

static TAU :Number

constant equals to 2 times pi

Methods

static clamp(val, low, high) → {number}

clamp the given value

Parameters:
Name Type Description
val number

the value to clamp

low number

lower limit

high number

higher limit

Returns:
number -

clamped value

static degToRad(angle) → {number}

Converts an angle in degrees to an angle in radians

Parameters:
Name Type Description
angle number

angle in degrees

Returns:
number -

corresponding angle in radians

Example
// convert a specific angle
me.Math.degToRad(60); // return 1.0471...

static isPowerOfTwo(val) → {boolean}

returns true if the given value is a power of two

Parameters:
Name Type Description
val Number
Returns:
boolean

static nextPowerOfTwo(val) → {boolean}

returns the next power of two for the given value

Parameters:
Name Type Description
val Number
Returns:
boolean

static radToDeg(radians) → {number}

Converts an angle in radians to an angle in degrees.

Parameters:
Name Type Description
radians number

angle in radians

Returns:
number -

corresponding angle in degrees

Example
// convert a specific angle
me.Math.radToDeg(1.0471975511965976); // return 60

static random(min, max) → {number}

return a random integer between min (included) and max (excluded)

Parameters:
Name Type Description
min number

minimum value.

max number

maximum value.

Returns:
number -

random value

Example
// Print a random number; one of 5, 6, 7, 8, 9
console.log(me.Math.random(5, 10) );

static randomFloat(min, max) → {number}

return a random float between min, max (exclusive)

Parameters:
Name Type Description
min number

minimum value.

max number

maximum value.

Returns:
number -

random value

Example
// Print a random number; one of 5, 6, 7, 8, 9
console.log(me.Math.randomFloat(5, 10) );

static round(num, decopt) → {number}

round a value to the specified number of digit

Parameters:
Name Type Attributes Default Description
num number

value to be rounded.

dec number <optional>
0

number of decimal digit to be rounded to.

Returns:
number -

rounded value

Example
// round a specific value to 2 digits
me.Math.round(10.33333, 2); // return 10.33

static toBeCloseTo(expected, actual, precisionopt) → {boolean}

check if the given value is close to the expected one

Parameters:
Name Type Attributes Default Description
expected number

value to be compared with.

actual number

actual value to compare

precision number <optional>
2

float precision for the comparison

Returns:
boolean -

if close to

Example
// test if the given value is close to 10
if (me.Math.toBeCloseTo(10, value)) {
    // do something
}

static weightedRandom(min, max) → {number}

return a weighted random between min, max (exclusive)

Parameters:
Name Type Description
min number

minimum value.

max number

maximum value.

Returns:
number -

random value

Example
// Print a random number; one of 5, 6, 7, 8, 9
console.log(me.Math.weightedRandom(5, 10) );