Module: CesiumMath

Math functions.
Source:

Members

(static, constant) DEGREES_PER_RADIAN :Number

The number of degrees in a radian.
Type:
  • Number
Default Value:
  • 180.0 / Math.PI
Source:

(static, constant) EPSILON1 :Number

0.1
Type:
  • Number
Source:

(static, constant) EPSILON2 :Number

0.01
Type:
  • Number
Source:

(static, constant) EPSILON3 :Number

0.001
Type:
  • Number
Source:

(static, constant) EPSILON4 :Number

0.0001
Type:
  • Number
Source:

(static, constant) EPSILON5 :Number

0.00001
Type:
  • Number
Source:

(static, constant) EPSILON6 :Number

0.000001
Type:
  • Number
Source:

(static, constant) EPSILON7 :Number

0.0000001
Type:
  • Number
Source:

(static, constant) EPSILON8 :Number

0.00000001
Type:
  • Number
Source:

(static, constant) EPSILON9 :Number

0.000000001
Type:
  • Number
Source:

(static, constant) EPSILON10 :Number

0.0000000001
Type:
  • Number
Source:

(static, constant) EPSILON11 :Number

0.00000000001
Type:
  • Number
Source:

(static, constant) EPSILON12 :Number

0.000000000001
Type:
  • Number
Source:

(static, constant) EPSILON13 :Number

0.0000000000001
Type:
  • Number
Source:

(static, constant) EPSILON14 :Number

0.00000000000001
Type:
  • Number
Source:

(static, constant) EPSILON15 :Number

0.000000000000001
Type:
  • Number
Source:

(static, constant) EPSILON16 :Number

0.0000000000000001
Type:
  • Number
Source:

(static, constant) EPSILON17 :Number

0.00000000000000001
Type:
  • Number
Source:

(static, constant) EPSILON18 :Number

0.000000000000000001
Type:
  • Number
Source:

(static, constant) EPSILON19 :Number

0.0000000000000000001
Type:
  • Number
Source:

(static, constant) EPSILON20 :Number

0.00000000000000000001
Type:
  • Number
Source:

(static, constant) GRAVITATIONALPARAMETER :Number

3.986004418e14
Type:
  • Number
Source:

(static, constant) LUNAR_RADIUS :Number

The mean radius of the moon, according to the "Report of the IAU/IAG Working Group on Cartographic Coordinates and Rotational Elements of the Planets and satellites: 2000", Celestial Mechanics 82: 83-110, 2002.
Type:
  • Number
Source:

(static, constant) ONE_OVER_PI :Number

1/pi
Type:
  • Number
Source:

(static, constant) ONE_OVER_TWO_PI :Number

1/2pi
Type:
  • Number
Source:

(static, constant) PI :Number

pi
Type:
  • Number
Source:

(static, constant) PI_OVER_FOUR :Number

pi/4
Type:
  • Number
Source:

(static, constant) PI_OVER_SIX :Number

pi/6
Type:
  • Number
Source:

(static, constant) PI_OVER_THREE :Number

pi/3
Type:
  • Number
Source:

(static, constant) PI_OVER_TWO :Number

pi/2
Type:
  • Number
Source:

(static, constant) RADIANS_PER_ARCSECOND :Number

The number of radians in an arc second.
Type:
  • Number
Default Value:
  • CesiumMath.RADIANS_PER_DEGREE / 3600.0
Source:

(static, constant) RADIANS_PER_DEGREE :Number

The number of radians in a degree.
Type:
  • Number
Default Value:
  • Math.PI / 180.0
Source:

(static, constant) SIXTY_FOUR_KILOBYTES :Number

64 * 1024
Type:
  • Number
Source:

(static, constant) SOLAR_RADIUS :Number

Radius of the sun in meters: 6.955e8
Type:
  • Number
Source:

(static, constant) THREE_PI_OVER_TWO :Number

3pi/2
Type:
  • Number
Source:

(static, constant) TWO_PI :Number

2pi
Type:
  • Number
Source:

Methods

(static) acosClamped(value) → {Number}

Computes Math.acos(value), but first clamps value to the range [-1.0, 1.0] so that the function will never return NaN.
Parameters:
Name Type Description
value Number The value for which to compute acos.
Source:
Returns:
The acos of the value if the value is in the range [-1.0, 1.0], or the acos of -1.0 or 1.0, whichever is closer, if the value is outside the range.
Type
Number

(static) asinClamped(value) → {Number}

Computes Math.asin(value), but first clamps value to the range [-1.0, 1.0] so that the function will never return NaN.
Parameters:
Name Type Description
value Number The value for which to compute asin.
Source:
Returns:
The asin of the value if the value is in the range [-1.0, 1.0], or the asin of -1.0 or 1.0, whichever is closer, if the value is outside the range.
Type
Number

(static) chordLength(angle, radius) → {Number}

Finds the chord length between two points given the circle's radius and the angle between the points.
Parameters:
Name Type Description
angle Number The angle between the two points.
radius Number The radius of the circle.
Source:
Returns:
The chord length.
Type
Number

(static) clamp(value, min, max) → {Number}

Constraint a value to lie between two values.
Parameters:
Name Type Description
value Number The value to constrain.
min Number The minimum value.
max Number The maximum value.
Source:
Returns:
The value clamped so that min <= value <= max.
Type
Number

(static) clampToLatitudeRange(angle) → {Number}

Convenience function that clamps a latitude value, in radians, to the range [-Math.PI/2, Math.PI/2). Useful for sanitizing data before use in objects requiring correct range.
Parameters:
Name Type Description
angle Number The latitude value, in radians, to clamp to the range [-Math.PI/2, Math.PI/2).
Source:
Returns:
The latitude value clamped to the range [-Math.PI/2, Math.PI/2).
Type
Number
Example
// Clamp 108 degrees latitude to 90 degrees latitude
var latitude = Cesium.Math.clampToLatitudeRange(Cesium.Math.toRadians(108.0));

(static) convertLongitudeRange(angle) → {Number}

Converts a longitude value, in radians, to the range [-Math.PI, Math.PI).
Parameters:
Name Type Description
angle Number The longitude value, in radians, to convert to the range [-Math.PI, Math.PI).
Source:
Returns:
The equivalent longitude value in the range [-Math.PI, Math.PI).
Type
Number
Example
// Convert 270 degrees to -90 degrees longitude
var longitude = Cesium.Math.convertLongitudeRange(Cesium.Math.toRadians(270.0));

(static) cosh(value) → {Number}

Returns the hyperbolic cosine of a number. The hyperbolic cosine of value is defined to be (ex + e-x)/2.0 where e is Euler's number, approximately 2.71828183.

Special cases:

  • If the argument is NaN, then the result is NaN.
  • If the argument is infinite, then the result is positive infinity.
  • If the argument is zero, then the result is 1.0.

Parameters:
Name Type Description
value Number The number whose hyperbolic cosine is to be returned.
Source:
Returns:
The hyperbolic cosine of value.
Type
Number

(static) equalsEpsilon(left, right, relativeEpsilon, absoluteEpsilonopt) → {Boolean}

Determines if two values are equal using an absolute or relative tolerance test. This is useful to avoid problems due to roundoff error when comparing floating-point values directly. The values are first compared using an absolute tolerance test. If that fails, a relative tolerance test is performed. Use this test if you are unsure of the magnitudes of left and right.
Parameters:
Name Type Attributes Default Description
left Number The first value to compare.
right Number The other value to compare.
relativeEpsilon Number The maximum inclusive delta between left and right for the relative tolerance test.
absoluteEpsilon Number <optional>
relativeEpsilon The maximum inclusive delta between left and right for the absolute tolerance test.
Source:
Returns:
true if the values are equal within the epsilon; otherwise, false.
Type
Boolean
Example
var a = Cesium.Math.equalsEpsilon(0.0, 0.01, Cesium.Math.EPSILON2); // true
var b = Cesium.Math.equalsEpsilon(0.0, 0.1, Cesium.Math.EPSILON2);  // false
var c = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON7); // true
var d = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON9); // false

(static) factorial(n) → {Number}

Computes the factorial of the provided number.
Parameters:
Name Type Description
n Number The number whose factorial is to be computed.
Source:
See:
Throws:
A number greater than or equal to 0 is required.
Type
DeveloperError
Returns:
The factorial of the provided number or undefined if the number is less than 0.
Type
Number
Example
//Compute 7!, which is equal to 5040
var computedFactorial = Cesium.Math.factorial(7);

(static) fromSNorm(value, rangeMaxopt) → {Number}

Converts a SNORM value in the range [0, rangeMax] to a scalar in the range [-1.0, 1.0].
Parameters:
Name Type Attributes Default Description
value Number SNORM value in the range [0, 255]
rangeMax Number <optional>
255 The maximum value in the SNORM range, 255 by default.
Source:
See:
  • CesiumMath.toSNorm
Returns:
Scalar in the range [-1.0, 1.0].
Type
Number

(static) incrementWrap(nopt, maximumValueopt, minimumValueopt) → {Number}

Increments a number with a wrapping to a minimum value if the number exceeds the maximum value.
Parameters:
Name Type Attributes Default Description
n Number <optional>
The number to be incremented.
maximumValue Number <optional>
The maximum incremented value before rolling over to the minimum value.
minimumValue Number <optional>
0.0 The number reset to after the maximum value has been exceeded.
Source:
Throws:
Maximum value must be greater than minimum value.
Type
DeveloperError
Returns:
The incremented number.
Type
Number
Example
var n = Cesium.Math.incrementWrap(5, 10, 0); // returns 6
var n = Cesium.Math.incrementWrap(10, 10, 0); // returns 0

(static) isPowerOfTwo(n) → {Boolean}

Determines if a positive integer is a power of two.
Parameters:
Name Type Description
n Number The positive integer to test.
Source:
Throws:
A number greater than or equal to 0 is required.
Type
DeveloperError
Returns:
true if the number if a power of two; otherwise, false.
Type
Boolean
Example
var t = Cesium.Math.isPowerOfTwo(16); // true
var f = Cesium.Math.isPowerOfTwo(20); // false

(static) lerp(p, q, time) → {Number}

Computes the linear interpolation of two values.
Parameters:
Name Type Description
p Number The start value to interpolate.
q Number The end value to interpolate.
time Number The time of interpolation generally in the range [0.0, 1.0].
Source:
Returns:
The linearly interpolated value.
Type
Number
Example
var n = Cesium.Math.lerp(0.0, 2.0, 0.5); // returns 1.0

(static) logBase(number, base) → {Number}

Finds the logarithm of a number to a base.
Parameters:
Name Type Description
number Number The number.
base Number The base.
Source:
Returns:
The result.
Type
Number

(static) mod(m, n) → {Number}

The modulo operation that also works for negative dividends.
Parameters:
Name Type Description
m Number The dividend.
n Number The divisor.
Source:
Returns:
The remainder.
Type
Number

(static) negativePiToPi(angle) → {Number}

Produces an angle in the range -Pi <= angle <= Pi which is equivalent to the provided angle.
Parameters:
Name Type Description
angle Number in radians
Source:
Returns:
The angle in the range [-CesiumMath.PI, CesiumMath.PI].
Type
Number

(static) nextPowerOfTwo(n) → {Number}

Computes the next power-of-two integer greater than or equal to the provided positive integer.
Parameters:
Name Type Description
n Number The positive integer to test.
Source:
Throws:
A number greater than or equal to 0 is required.
Type
DeveloperError
Returns:
The next power-of-two integer.
Type
Number
Example
var n = Cesium.Math.nextPowerOfTwo(29); // 32
var m = Cesium.Math.nextPowerOfTwo(32); // 32

(static) nextRandomNumber() → {Number}

Generates a random number in the range of [0.0, 1.0) using a Mersenne twister.
Source:
See:
Returns:
A random number in the range of [0.0, 1.0).
Type
Number

(static) setRandomNumberSeed(seed)

Sets the seed used by the random number generator in CesiumMath#nextRandomNumber.
Parameters:
Name Type Description
seed Number An integer used as the seed.
Source:

(static) sign(value) → {Number}

Returns the sign of the value; 1 if the value is positive, -1 if the value is negative, or 0 if the value is 0.
Parameters:
Name Type Description
value Number The value to return the sign of.
Source:
Returns:
The sign of value.
Type
Number

(static) signNotZero(value) → {Number}

Returns 1.0 if the given value is positive or zero, and -1.0 if it is negative. This is similar to CesiumMath#sign except that returns 1.0 instead of 0.0 when the input value is 0.0.
Parameters:
Name Type Description
value Number The value to return the sign of.
Source:
Returns:
The sign of value.
Type
Number

(static) sinh(value) → {Number}

Returns the hyperbolic sine of a number. The hyperbolic sine of value is defined to be (ex - e-x)/2.0 where e is Euler's number, approximately 2.71828183.

Special cases:

  • If the argument is NaN, then the result is NaN.
  • If the argument is infinite, then the result is an infinity with the same sign as the argument.
  • If the argument is zero, then the result is a zero with the same sign as the argument.

Parameters:
Name Type Description
value Number The number whose hyperbolic sine is to be returned.
Source:
Returns:
The hyperbolic sine of value.
Type
Number

(static) toDegrees(radians) → {Number}

Converts radians to degrees.
Parameters:
Name Type Description
radians Number The angle to convert in radians.
Source:
Returns:
The corresponding angle in degrees.
Type
Number

(static) toRadians(degrees) → {Number}

Converts degrees to radians.
Parameters:
Name Type Description
degrees Number The angle to convert in degrees.
Source:
Returns:
The corresponding angle in radians.
Type
Number

(static) toSNorm(value, rangeMaxopt) → {Number}

Converts a scalar value in the range [-1.0, 1.0] to a SNORM in the range [0, rangeMax]
Parameters:
Name Type Attributes Default Description
value Number The scalar value in the range [-1.0, 1.0]
rangeMax Number <optional>
255 The maximum value in the mapped range, 255 by default.
Source:
See:
  • CesiumMath.fromSNorm
Returns:
A SNORM value, where 0 maps to -1.0 and rangeMax maps to 1.0.
Type
Number

(static) zeroToTwoPi(angle) → {Number}

Produces an angle in the range 0 <= angle <= 2Pi which is equivalent to the provided angle.
Parameters:
Name Type Description
angle Number in radians
Source:
Returns:
The angle in the range [0, CesiumMath.TWO_PI].
Type
Number