Math API
- Source:
Members
(static) DEG_TO_RAD :Number
Conversion factor between degrees and radians
Type:
- Number
- Source:
Example
// Convert 180 degrees to pi radians
var rad = 180 * pc.math.DEG_TO_RAD;
(private, static) INV_LOG2 :Number
Inverse log 2. Use Math.LOG2E instead.
Type:
- Number
- Deprecated:
- Yes
- Source:
(static) RAD_TO_DEG :Number
Conversion factor between degrees and radians
Type:
- Number
- Source:
Example
// Convert pi radians to 180 degrees
var deg = Math.PI * pc.math.RAD_TO_DEG;
Methods
(static) bytesToInt24(r, g, b) → {Number}
Convert 3 8 bit Numbers into a single unsigned 24 bit Number.
Parameters:
Name | Type | Description |
---|---|---|
r |
Number | A single byte (0-255) |
g |
Number | A single byte (0-255) |
b |
Number | A single byte (0-255) |
- Source:
Returns:
A single unsigned 24 bit Number.
- Type
- Number
Example
// 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);
(static) bytesToInt32(r, g, b, a) → {Number}
Convert 4 1-byte Numbers into a single unsigned 32bit Number.
Parameters:
Name | Type | Description |
---|---|---|
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) |
- Source:
Returns:
A single unsigned 32bit Number.
- Type
- Number
Example
// 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);
(static) clamp(value, min, max) → {Number}
Clamp a number between min and max inclusive.
Parameters:
Name | Type | Description |
---|---|---|
value |
Number | Number to clamp |
min |
Number | Min value |
max |
Number | Max value |
- Source:
Returns:
The clamped value
- Type
- Number
(static) intToBytes24(i) → {Array.<Number>}
Convert an 24 bit integer into an array of 3 bytes.
Parameters:
Name | Type | Description |
---|---|---|
i |
Number | Number holding an integer value |
- Source:
Returns:
An array of 3 bytes.
- Type
- Array.<Number>
Example
// Set bytes to [0x11, 0x22, 0x33]
var bytes = pc.math.intToBytes24(0x112233);
(static) intToBytes32(i) → {Array.<Number>}
Convert an 32 bit integer into an array of 4 bytes.
Parameters:
Name | Type | Description |
---|---|---|
i |
Number | Number holding an integer value |
- Source:
Returns:
An array of 4 bytes
- Type
- Array.<Number>
Example
// Set bytes to [0x11, 0x22, 0x33, 0x44]
var bytes = pc.math.intToBytes32(0x11223344);
(static) lerp(a, b, alpha) → {Number}
Calculates the linear interpolation of two numbers.
Parameters:
Name | Type | Description |
---|---|---|
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. |
- Source:
Returns:
The linear interpolation of two numbers.
- Type
- Number
(static) lerpAngle(a, b, alpha) → {Number}
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:
Name | Type | Description |
---|---|---|
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. |
- Source:
Returns:
The linear interpolation of two angles
- Type
- Number
(static) nextPowerOfTwo(val) → {Number}
Returns the next power of 2 for the specified value.
Parameters:
Name | Type | Description |
---|---|---|
val |
Number | The value for which to calculate the next power of 2. |
- Source:
Returns:
The next power of 2.
- Type
- Number
(static) powerOfTwo(x) → {Boolean}
Returns true if argument is a power-of-two and false otherwise.
Parameters:
Name | Type | Description |
---|---|---|
x |
Number | Number to check for power-of-two property. |
- Source:
Returns:
true if power-of-two and false otherwise.
- Type
- Boolean
(static) random(min, max) → {Number}
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:
Name | Type | Description |
---|---|---|
min |
Number | Lower bound for range. |
max |
Number | Upper bound for range. |
- Source:
Returns:
Pseudo-random number between the supplied range.
- Type
- Number
(static) smootherstep(min, max, x) → {Number}
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.
See http://en.wikipedia.org/wiki/Smoothstep for more details.
Parameters:
Name | Type | Description |
---|---|---|
min |
Number | The lower bound of the interpolation range. |
max |
Number | The upper bound of the interpolation range. |
x |
Number | The value to interpolate. |
- Source:
Returns:
The smoothly interpolated value clamped between zero and one.
- Type
- Number
(static) smoothstep(min, max, x) → {Number}
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.
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:
Name | Type | Description |
---|---|---|
min |
Number | The lower bound of the interpolation range. |
max |
Number | The upper bound of the interpolation range. |
x |
Number | The value to interpolate. |
- Source:
Returns:
The smoothly interpolated value clamped between zero and one.
- Type
- Number