Class List

Math API

# Summary

### Static Methods

 bytesToInt24 Convert 3 8 bit Numbers into a single unsigned 24 bit Number. bytesToInt32 Convert 4 1-byte Numbers into a single unsigned 32bit Number. clamp Clamp a number between min and max inclusive. intToBytes24 Convert an 24 bit integer into an array of 3 bytes. intToBytes32 Convert an 32 bit integer into an array of 4 bytes. lerp Calculates the linear interpolation of two numbers. lerpAngle Calculates the linear interpolation of two angles ensuring that interpolation is correctly performed across the 360 to 0 degree boundary. nextPowerOfTwo Returns the next power of 2 for the specified value. powerOfTwo Returns true if argument is a power-of-two and false otherwise. random Return a pseudo-random number between min and max. smootherstep An improved version of the pc. smoothstep The function interpolates smoothly between two input values based on a third one that should be between the first two.

# Details

## 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

 r Number A single byte (0-255) g Number A single byte (0-255) b Number A 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

 r Number A single byte (0-255) g Number A single byte (0-255) b Number A single byte (0-255) a Number A single byte (0-255)

#### Returns

Number A single unsigned 32bit Number.

### clamp(value, min, max) 

Clamp a number between min and max inclusive.

#### Parameters

 value Number Number to clamp min Number Min value max Number Max 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

 i Number Number 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

 i Number Number holding an integer value

#### Returns

Number[] An array of 4 bytes

### lerp(a, b, alpha) 

Calculates the linear interpolation of two numbers.

#### Parameters

 a Number Number to linearly interpolate from. b Number Number to linearly interpolate to. alpha Number The 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

 a Number Angle (in degrees) to linearly interpolate from. b Number Angle (in degrees) to linearly interpolate to. alpha Number The 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

 val Number The 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

 x Number Number 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

 min Number Lower bound for range. max Number Upper 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

 min Number The lower bound of the interpolation range. max Number The upper bound of the interpolation range. x Number The 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

 min Number The lower bound of the interpolation range. max Number The upper bound of the interpolation range. x Number The value to interpolate.

#### Returns

Number The smoothly interpolated value clamped between zero and one.