API Reference

Class List

pc.math

Math API

Summary

Static Properties

DEG_TO_RADConversion factor between degrees and radians
INV_LOG2Inverse log 2
RAD_TO_DEGConversion factor between degrees and radians

Static Methods

bytesToInt24Convert 3 8 bit Numbers into a single unsigned 24 bit Number.
bytesToInt32Convert 4 1-byte Numbers into a single unsigned 32bit Number.
clampClamp a number between min and max inclusive.
intToBytes24Convert an 24 bit integer into an array of 3 bytes.
intToBytes32Convert an 32 bit integer into an array of 4 bytes.
lerpCalculates the linear interpolation of two numbers.
lerpAngleCalculates the linear interpolation of two angles ensuring that interpolation is correctly performed across the 360 to 0 degree boundary.
nextPowerOfTwoReturns the next power of 2 for the specified value.
powerOfTwoReturns true if argument is a power-of-two and false otherwise.
randomReturn a pseudo-random number between min and max.
smootherstepAn improved version of the pc.
smoothstepThe function interpolates smoothly between two input values based on a third one that should be between the first two.

Details

Static Properties

DEG_TO_RADConversion factor between degrees and radians
INV_LOG2Inverse log 2
RAD_TO_DEGConversion factor between degrees and radians

Static Methods

bytesToInt24(r, g, b)

Convert 3 8 bit Numbers into a single unsigned 24 bit Number.

// Set result1 to 0x112233 from an array of 3 values
var result1 = pc.math.bytesToInt24([0x11, 0x22, 0x33]);

// Set result2 to 0x112233 from 3 discrete values
var result2 = pc.math.bytesToInt24(0x11, 0x22, 0x33);

Parameters

rNumberA single byte (0-255)
gNumberA single byte (0-255)
bNumberA single byte (0-255)

Returns

Number A single unsigned 24 bit Number.

bytesToInt32(r, g, b, a)

Convert 4 1-byte Numbers into a single unsigned 32bit Number.

// Set result1 to 0x11223344 from an array of 4 values
var result1 = pc.math.bytesToInt32([0x11, 0x22, 0x33, 0x44]);

// Set result2 to 0x11223344 from 4 discrete values
var result2 = pc.math.bytesToInt32(0x11, 0x22, 0x33, 0x44);

Parameters

rNumberA single byte (0-255)
gNumberA single byte (0-255)
bNumberA single byte (0-255)
aNumberA single byte (0-255)

Returns

Number A single unsigned 32bit Number.

clamp(value, min, max)

Clamp a number between min and max inclusive.

Parameters

valueNumberNumber to clamp
minNumberMin value
maxNumberMax value

Returns

Number The clamped value

intToBytes24(i)

Convert an 24 bit integer into an array of 3 bytes.

// Set bytes to [0x11, 0x22, 0x33]
var bytes = pc.math.intToBytes24(0x112233);

Parameters

iNumberNumber holding an integer value

Returns

Number[] An array of 3 bytes.

intToBytes32(i)

Convert an 32 bit integer into an array of 4 bytes.

// Set bytes to [0x11, 0x22, 0x33, 0x44]
var bytes = pc.math.intToBytes32(0x11223344);

Parameters

iNumberNumber holding an integer value

Returns

Number[] An array of 4 bytes

lerp(a, b, alpha)

Calculates the linear interpolation of two numbers.

Parameters

aNumberNumber to linearly interpolate from.
bNumberNumber to linearly interpolate to.
alphaNumberThe value controlling the result of interpolation. When alpha is 0, a is returned. When alpha is 1, b is returned. Between 0 and 1, a linear interpolation between a and b is returned. alpha is clamped between 0 and 1.

Returns

Number The linear interpolation of two numbers.

lerpAngle(a, b, alpha)

Calculates the linear interpolation of two angles ensuring that interpolation is correctly performed across the 360 to 0 degree boundary. Angles are supplied in degrees.

Parameters

aNumberAngle (in degrees) to linearly interpolate from.
bNumberAngle (in degrees) to linearly interpolate to.
alphaNumberThe value controlling the result of interpolation. When alpha is 0, a is returned. When alpha is 1, b is returned. Between 0 and 1, a linear interpolation between a and b is returned. alpha is clamped between 0 and 1.

Returns

Number The linear interpolation of two angles

nextPowerOfTwo(val)

Returns the next power of 2 for the specified value.

Parameters

valNumberThe value for which to calculate the next power of 2.

Returns

Number The next power of 2.

powerOfTwo(x)

Returns true if argument is a power-of-two and false otherwise.

Parameters

xNumberNumber to check for power-of-two property.

Returns

Boolean true if power-of-two and false otherwise.

random(min, max)

Return a pseudo-random number between min and max. The number generated is in the range [min, max), that is inclusive of the minimum but exclusive of the maximum.

Parameters

minNumberLower bound for range.
maxNumberUpper bound for range.

Returns

Number Pseudo-random number between the supplied range.

smootherstep(min, max, x)

An improved version of the pc.math.smoothstep function which has zero 1st and 2nd order derivatives at t=0 and t=1.
See http://en.wikipedia.org/wiki/Smoothstep for more details.

Parameters

minNumberThe lower bound of the interpolation range.
maxNumberThe upper bound of the interpolation range.
xNumberThe value to interpolate.

Returns

Number The smoothly interpolated value clamped between zero and one.

smoothstep(min, max, x)

The function interpolates smoothly between two input values based on a third one that should be between the first two. The returned value is clamped between 0 and 1.
The slope (i.e. derivative) of the smoothstep function starts at 0 and ends at 0. This makes it easy to create a sequence of transitions using smoothstep to interpolate each segment rather than using a more sophisticated or expensive interpolation technique.
See http://en.wikipedia.org/wiki/Smoothstep for more details.

Parameters

minNumberThe lower bound of the interpolation range.
maxNumberThe upper bound of the interpolation range.
xNumberThe value to interpolate.

Returns

Number The smoothly interpolated value clamped between zero and one.