new Math()
A collection of useful mathematical functions.
These are normally accessed through game.math.
- Source - math/Math.js, line 17
- See:
Members
-
<static> PI2
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - Not used internally. Use `2 * Math.PI` instead.
- Default Value:
- ~6.283
- Source - math/Math.js, line 25
Twice PI.
Properties:
| Name | Type | Description |
|---|---|---|
Phaser.Math#PI2 |
number |
Methods
-
angleBetween(x1, y1, x2, y2) → {number}
-
Find the angle of a segment from (x1, y1) -> (x2, y2).
Parameters:
Name Type Description x1number y1number x2number y2number Returns:
number -The angle, in radians.
- Source - math/Math.js, line 326
-
angleBetweenPoints(point1, point2) → {number}
-
Find the angle of a segment from (point1.x, point1.y) -> (point2.x, point2.y).
Parameters:
Name Type Description point1Phaser.Point point2Phaser.Point Returns:
number -The angle, in radians.
- Source - math/Math.js, line 355
-
angleBetweenPointsY(point1, point2) → {number}
-
Find the angle of a segment from (point1.x, point1.y) -> (point2.x, point2.y).
Parameters:
Name Type Description point1Phaser.Point point2Phaser.Point Returns:
number -The angle, in radians.
- Source - math/Math.js, line 366
-
angleBetweenY(x1, y1, x2, y2) → {number}
-
Find the angle of a segment from (x1, y1) -> (x2, y2). Note that the difference between this method and Math.angleBetween is that this assumes the y coordinate travels down the screen.
Parameters:
Name Type Description x1number y1number x2number y2number Returns:
number -The angle, in radians.
- Source - math/Math.js, line 339
-
angleLimit(angle, min, max) → {number}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - Use Phaser.Math#clamp instead
- Source - math/Math.js, line 737
-
average() → {number}
-
Averages all values passed to the function and returns the result.
Returns:
number -The average of all given values.
- Source - math/Math.js, line 93
-
<internal> bernstein(n, i) → {number}
-
Parameters:
Name Type Description nnumber inumber Returns:
number -- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - math/Math.js, line 865
-
bezierInterpolation(v, k) → {number}
-
A Bezier Interpolation Method, mostly used by Phaser.Tween.
Parameters:
Name Type Description vArray knumber Returns:
number -- Source - math/Math.js, line 791
-
<internal> catmullRom(p0, p1, p2, p3, t) → {number}
-
Calculates a callmum rom value.
Parameters:
Name Type Description p0number p1number p2number p3number tnumber Returns:
number -- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - math/Math.js, line 899
-
catmullRomInterpolation(v, k) → {number}
-
A Catmull Rom Interpolation Method, mostly used by Phaser.Tween.
Parameters:
Name Type Description vArray knumber Returns:
number -- Source - math/Math.js, line 812
-
ceil(value) → {integer}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - Use Phaser.Math#roundAwayFromZero instead.
- Source - math/Math.js, line 981
-
ceilTo(value, place, base) → {number}
-
Parameters:
Name Type Description valuenumber The value to round.
placenumber The place to round to.
basenumber The base to round in... default is 10 for decimal.
Returns:
number -- Source - math/Math.js, line 295
-
chanceRoll(chance) → {boolean}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - Use Phaser.Utils.chanceRoll
- Source - math/Math.js, line 422
-
chanceRoll(chance) → {boolean}
-
Generate a random bool result based on the chance value.
Returns true or false based on the chance value (default 50%). For example if you wanted a player to have a 30% chance of getting a bonus, call chanceRoll(30) - true means the chance passed, false means it failed.
Parameters:
Name Type Description chancenumber The chance of receiving the value. A number between 0 and 100 (effectively 0% to 100%).
Returns:
boolean -True if the roll passed, or false otherwise.
- Source - utils/Utils.js, line 77
-
clamp(x, a, b) → {number}
-
Force a value within the boundaries by clamping
xto the range[a, b].Parameters:
Name Type Description xnumber anumber bnumber Returns:
number -- Source - math/Math.js, line 1130
-
clampBottom(x, a) → {number}
-
Clamp
xto the range[a, Infinity). Roughly the same asMath.max(x, a), except for NaN handling.Parameters:
Name Type Description xnumber anumber Returns:
number -- Source - math/Math.js, line 1143
-
degToRad(degrees) → {number}
-
Convert degrees to radians.
Parameters:
Name Type Description degreesnumber Angle in degrees.
Returns:
number -Angle in radians.
- Source - math/Math.js, line 1259
-
difference(a, b) → {number}
-
The (absolute) difference between two values.
Parameters:
Name Type Description anumber bnumber Returns:
number -- Source - math/Math.js, line 919
-
distance(x1, y1, x2, y2) → {number}
-
Returns the distance between the two given set of coordinates.
Parameters:
Name Type Description x1number y1number x2number y2number Returns:
number -The distance between the two sets of coordinates.
- Source - math/Math.js, line 1077
-
distancePow(x1, y1, x2, y2, pow) → {number}
-
Returns the distance between the two given set of coordinates at the power given.
Parameters:
Name Type Argument Default Description x1number y1number x2number y2number pownumber <optional>
2 Returns:
number -The distance between the two sets of coordinates.
- Source - math/Math.js, line 1096
-
distanceRounded(x1, y1, x2, y2) → {number}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - Do the rounding locally.
- Source - math/Math.js, line 1115
-
factorial(value) → {number}
-
Parameters:
Name Type Description valuenumber the number you want to evaluate
Returns:
number -- Source - math/Math.js, line 876
-
floor(value) → {integer}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - Use Phaser.Math#truncate or `Math.trunc` instead.
- Source - math/Math.js, line 965
-
floorTo(value, place, base) → {number}
-
Parameters:
Name Type Description valuenumber The value to round.
placenumber The place to round to.
basenumber The base to round in... default is 10 for decimal.
Returns:
number -- Source - math/Math.js, line 277
-
fuzzyCeil(val, epsilon) → {boolean}
-
Parameters:
Name Type Argument Default Description valnumber epsilonnumber <optional>
(small value) Returns:
boolean -ceiling(val-epsilon)
- Source - math/Math.js, line 69
-
fuzzyEqual(a, b, epsilon) → {boolean}
-
Two number are fuzzyEqual if their difference is less than epsilon.
Parameters:
Name Type Argument Default Description anumber bnumber epsilonnumber <optional>
(small value) Returns:
boolean -True if |a-b|<epsilon
- Source - math/Math.js, line 27
-
fuzzyFloor(val, epsilon) → {boolean}
-
Parameters:
Name Type Argument Default Description valnumber epsilonnumber <optional>
(small value) Returns:
boolean -floor(val-epsilon)
- Source - math/Math.js, line 81
-
fuzzyGreaterThan(a, b, epsilon) → {boolean}
-
ais fuzzyGreaterThanbif it is more than b - epsilon.Parameters:
Name Type Argument Default Description anumber bnumber epsilonnumber <optional>
(small value) Returns:
boolean -True if a>b+epsilon
- Source - math/Math.js, line 55
-
fuzzyLessThan(a, b, epsilon) → {boolean}
-
ais fuzzyLessThanbif it is less than b + epsilon.Parameters:
Name Type Argument Default Description anumber bnumber epsilonnumber <optional>
(small value) Returns:
boolean -True if a<b+epsilon
- Source - math/Math.js, line 41
-
getRandom(objects, startIndex, length) → {object}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - Use Phaser.ArrayUtils.getRandomItem
- Source - math/Math.js, line 931
-
interpolateFloat(a, b, weight) → {number}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - See Phaser.Math#linear
- Source - math/Math.js, line 313
-
isEven(n) → {boolean}
-
Returns true if the number given is even.
Parameters:
Name Type Description ninteger The number to check.
Returns:
boolean -True if the given number is even. False if the given number is odd.
- Source - math/Math.js, line 587
-
isOdd(n) → {boolean}
-
Returns true if the number given is odd.
Parameters:
Name Type Description ninteger The number to check.
Returns:
boolean -True if the given number is odd. False if the given number is even.
- Source - math/Math.js, line 575
-
limitValue(value, min, max) → {number}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - Use Phaser.Math#clamp
- Source - math/Math.js, line 550
-
linear(p0, p1, t) → {number}
-
Calculates a linear (interpolation) value over t.
Parameters:
Name Type Description p0number p1number tnumber Returns:
number -- Source - math/Math.js, line 852
-
linearInterpolation(v, k) → {number}
-
A Linear Interpolation Method, mostly used by Phaser.Tween.
Parameters:
Name Type Description vArray knumber Returns:
number -- Source - math/Math.js, line 764
-
mapLinear(x, a1, a2, b1, b2) → {number}
-
Linear mapping from range <a1, a2> to range <b1, b2>
Parameters:
Name Type Description xnumber the value to map
a1number first endpoint of the range <a1, a2>
a2number final endpoint of the range <a1, a2>
b1number first endpoint of the range <b1, b2>
b2number final endpoint of the range <b1, b2>
Returns:
number -- Source - math/Math.js, line 1170
-
max() → {number}
-
Variation of Math.max that can be passed either an array of numbers or the numbers as parameters.
Prefer the standard
Math.maxfunction when appropriate.Returns:
number -The largest value from those given.
- Source - math/Math.js, line 631
- See:
-
maxAdd(value, amount, max) → {number}
-
Adds the given amount to the value, but never lets the value go over the specified maximum.
Parameters:
Name Type Description valuenumber The value to add the amount to.
amountnumber The amount to add to the value.
maxnumber The maximum the value is allowed to be.
Returns:
number -- Source - math/Math.js, line 470
-
maxProperty() → {number}
-
Variation of Math.max that can be passed a property and either an array of objects or the objects as parameters. It will find the largest matching property value from the given objects.
Returns:
number -The largest value from those given.
- Source - math/Math.js, line 693
-
min() → {number}
-
Variation of Math.min that can be passed either an array of numbers or the numbers as parameters.
Prefer the standard
Math.minfunction when appropriate.Returns:
number -The lowest value from those given.
- Source - math/Math.js, line 599
- See:
-
minProperty() → {number}
-
Variation of Math.min that can be passed a property and either an array of objects or the objects as parameters. It will find the lowest matching property value from the given objects.
Returns:
number -The lowest value from those given.
- Source - math/Math.js, line 663
-
minSub(value, amount, min) → {number}
-
Subtracts the given amount from the value, but never lets the value go below the specified minimum.
Parameters:
Name Type Description valuenumber The base value.
amountnumber The amount to subtract from the base value.
minnumber The minimum the value is allowed to be.
Returns:
number -The new value.
- Source - math/Math.js, line 483
-
normalizeAngle(angleRad) → {number}
-
Normalizes an angle to the [0,2pi) range.
Parameters:
Name Type Description angleRadnumber The angle to normalize, in radians.
Returns:
number -Returns the angle, fit within the [0,2pi] range, in radians.
- Source - math/Math.js, line 387
-
normalizeLatitude(lat) → {number}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - Use Phaser.Math#clamp.
- Source - math/Math.js, line 400
-
normalizeLongitude(lng) → {number}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - Use Phaser.Math#wrap.
- Source - math/Math.js, line 411
-
numberArray(start, end) → {number[]}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - See Phaser.ArrayUtils.numberArray
- Source - math/Math.js, line 437
-
numberArray(start, end) → {number[]}
-
Create an array representing the inclusive range of numbers (usually integers) in
[start, end]. This is equivalent tonumberArrayStep(start, end, 1).Parameters:
Name Type Description startnumber The minimum value the array starts with.
endnumber The maximum value the array contains.
Returns:
number[] -The array of number values.
- Source - utils/ArrayUtils.js, line 214
-
numberArrayStep(start, end, step) → {Array}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - See Phaser.ArrayUtils.numberArrayStep
- Source - math/Math.js, line 450
-
numberArrayStep(start, end, step) → {Array}
-
Create an array of numbers (positive and/or negative) progressing from
startup to but not includingendby advancing bystep.If
startis less thanstopa zero-length range is created unless a negativestepis specified.Certain values for
startandend(eg. NaN/undefined/null) are currently coerced to 0; for forward compatibility make sure to pass in actual numbers.Parameters:
Name Type Argument Default Description startnumber The start of the range.
endnumber The end of the range.
stepnumber <optional>
1 The value to increment or decrement by.
Returns:
Array -Returns the new array of numbers.
- Source - utils/ArrayUtils.js, line 236
Example
Phaser.Math.numberArrayStep(4); // => [0, 1, 2, 3] Phaser.Math.numberArrayStep(1, 5); // => [1, 2, 3, 4] Phaser.Math.numberArrayStep(0, 20, 5); // => [0, 5, 10, 15] Phaser.Math.numberArrayStep(0, -4, -1); // => [0, -1, -2, -3] Phaser.Math.numberArrayStep(1, 4, 0); // => [1, 1, 1] Phaser.Math.numberArrayStep(0); // => []
-
percent(a, b, base) → {number}
-
Work out what percentage value
ais of valuebusing the given base.Parameters:
Name Type Argument Default Description anumber The value to work out the percentage for.
bnumber The value you wish to get the percentage of.
basenumber <optional>
0 The base value.
Returns:
number -The percentage a is of b, between 0 and 1.
- Source - math/Math.js, line 1226
-
radToDeg(radians) → {number}
-
Convert degrees to radians.
Parameters:
Name Type Description radiansnumber Angle in radians.
Returns:
number -Angle in degrees
- Source - math/Math.js, line 1270
-
randomSign() → {number}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - Use Phaser.Utils.randomChoice or other
- Source - math/Math.js, line 564
-
removeRandom(objects, startIndex, length) → {object}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - Use Phaser.ArrayUtils.removeRandomItem
- Source - math/Math.js, line 948
-
reverseAngle(angleRad) → {number}
-
Reverses an angle.
Parameters:
Name Type Description angleRadnumber The angle to reverse, in radians.
Returns:
number -Returns the reverse angle, in radians.
- Source - math/Math.js, line 377
-
roundAwayFromZero(value) → {integer}
-
Round to the next whole number away from zero.
Parameters:
Name Type Description valuenumber Any number.
Returns:
integer -The rounded value of that number.
- Source - math/Math.js, line 997
-
roundTo(value, place, base) → {number}
-
Round to some place comparative to a
base, default is 10 for decimal place. Theplaceis represented by the power applied tobaseto get that place.e.g. 2000/7 ~= 285.714285714285714285714 ~= (bin)100011101.1011011011011011 roundTo(2000/7,3) === 0 roundTo(2000/7,2) == 300 roundTo(2000/7,1) == 290 roundTo(2000/7,0) == 286 roundTo(2000/7,-1) == 285.7 roundTo(2000/7,-2) == 285.71 roundTo(2000/7,-3) == 285.714 roundTo(2000/7,-4) == 285.7143 roundTo(2000/7,-5) == 285.71429 roundTo(2000/7,3,2) == 288 -- 100100000 roundTo(2000/7,2,2) == 284 -- 100011100 roundTo(2000/7,1,2) == 286 -- 100011110 roundTo(2000/7,0,2) == 286 -- 100011110 roundTo(2000/7,-1,2) == 285.5 -- 100011101.1 roundTo(2000/7,-2,2) == 285.75 -- 100011101.11 roundTo(2000/7,-3,2) == 285.75 -- 100011101.11 roundTo(2000/7,-4,2) == 285.6875 -- 100011101.1011 roundTo(2000/7,-5,2) == 285.71875 -- 100011101.10111Note what occurs when we round to the 3rd space (8ths place), 100100000, this is to be assumed because we are rounding 100011.1011011011011011 which rounds up.
Parameters:
Name Type Description valuenumber The value to round.
placenumber The place to round to.
basenumber The base to round in... default is 10 for decimal.
Returns:
number -- Source - math/Math.js, line 231
-
shear(n) → {number}
-
Parameters:
Name Type Description nnumber Returns:
number -n mod 1
- Source - math/Math.js, line 122
-
shift(array) → {any}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - Use Phaser.ArrayUtils.rotate instead
- Source - math/Math.js, line 1049
-
shuffleArray(array) → {any[]}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - Use Phaser.ArrayUtils.shuffle
- Source - math/Math.js, line 1066
-
sign(x) → {integer}
-
A value representing the sign of the value: -1 for negative, +1 for positive, 0 if value is 0.
This works differently from
Math.signfor values of NaN and -0, etc.Parameters:
Name Type Description xnumber Returns:
integer -An integer in {-1, 0, 1}
- Source - math/Math.js, line 1213
-
sinCosGenerator(length, sinAmplitude, cosAmplitude, frequency) → {Object}
-
Generate a sine and cosine table simultaneously and extremely quickly. Based on research by Franky of scene.at
The parameters allow you to specify the length, amplitude and frequency of the wave. Once you have called this function you should get the results via getSinTable() and getCosTable(). This generator is fast enough to be used in real-time.
Parameters:
Name Type Description lengthnumber The length of the wave
sinAmplitudenumber The amplitude to apply to the sine table (default 1.0) if you need values between say -+ 125 then give 125 as the value
cosAmplitudenumber The amplitude to apply to the cosine table (default 1.0) if you need values between say -+ 125 then give 125 as the value
frequencynumber The frequency of the sine and cosine table data
Returns:
Object -Returns the table data.
- Source - math/Math.js, line 1009
-
smootherstep(x, min, max) → {number}
-
Smootherstep function as detailed at http://en.wikipedia.org/wiki/Smoothstep
Parameters:
Name Type Description xnumber minnumber maxnumber Returns:
number -- Source - math/Math.js, line 1199
-
smoothstep(x, min, max) → {number}
-
Smoothstep function as detailed at http://en.wikipedia.org/wiki/Smoothstep
Parameters:
Name Type Description xnumber minnumber maxnumber Returns:
number -- Source - math/Math.js, line 1185
-
snapTo(input, gap, start) → {number}
-
Snap a value to nearest grid slice, using rounding.
Example: if you have an interval gap of 5 and a position of 12... you will snap to 10 whereas 14 will snap to 15.
Parameters:
Name Type Argument Description inputnumber The value to snap.
gapnumber The interval gap of the grid.
startnumber <optional>
Optional starting offset for gap.
Returns:
number -- Source - math/Math.js, line 131
-
snapToCeil(input, gap, start) → {number}
-
Snap a value to nearest grid slice, using ceil.
Example: if you have an interval gap of 5 and a position of 12... you will snap to 15. As will 14 will snap to 15... but 16 will snap to 20.
Parameters:
Name Type Argument Description inputnumber The value to snap.
gapnumber The interval gap of the grid.
startnumber <optional>
Optional starting offset for gap.
Returns:
number -- Source - math/Math.js, line 183
-
snapToFloor(input, gap, start) → {number}
-
Snap a value to nearest grid slice, using floor.
Example: if you have an interval gap of 5 and a position of 12... you will snap to 10. As will 14 snap to 10... but 16 will snap to 15
Parameters:
Name Type Argument Description inputnumber The value to snap.
gapnumber The interval gap of the grid.
startnumber <optional>
Optional starting offset for gap.
Returns:
number -- Source - math/Math.js, line 157
-
snapToInArray(input, arr, sort) → {number}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - See Phaser.ArrayUtils.findClosest for an alternative.
- Source - math/Math.js, line 209
-
truncate(n) → {integer}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- 2.2.0 - Use `Math.trunc` (now with polyfill)
- Source - math/Math.js, line 112
-
within(a, b, tolerance) → {boolean}
-
Checks if two values are within the given tolerance of each other.
Parameters:
Name Type Description anumber The first number to check
bnumber The second number to check
tolerancenumber The tolerance. Anything equal to or less than this is considered within the range.
Returns:
boolean -True if a is <= tolerance of b.
- Source - math/Math.js, line 1156
- See:
-
- Phaser.Math.fuzzyEqual
-
wrap(value, min, max) → {number}
-
Ensures that the value always stays between min and max, by wrapping the value around.
If
maxis not larger thanminthe result is 0.Parameters:
Name Type Description valuenumber The value to wrap.
minnumber The minimum the value is allowed to be.
maxnumber The maximum the value is allowed to be, should be larger than
min.Returns:
number -The wrapped value.
- Source - math/Math.js, line 496
-
wrapAngle(angle, radians) → {number}
-
Keeps an angle value between -180 and +180; or -PI and PI if radians.
Parameters:
Name Type Argument Default Description anglenumber The angle value to wrap
radiansboolean <optional>
false Set to
trueif the angle is given in radians, otherwise degrees is expected.Returns:
number -The new angle value; will be the same as the input angle if it was within bounds.
- Source - math/Math.js, line 723
-
wrapValue(value, amount, max) → {number}
-
Adds value to amount and ensures that the result always stays between 0 and max, by wrapping the value around.
Values must be positive integers, and are passed through Math.abs. See Phaser.Math#wrap for an alternative.
Parameters:
Name Type Description valuenumber The value to add the amount to.
amountnumber The amount to add to the value.
maxnumber The maximum the value is allowed to be.
Returns:
number -The wrapped value.
- Source - math/Math.js, line 527
Keeps an angle value between the given min and max values.
Parameters:
| Name | Type | Description |
|---|---|---|
angle |
number | The angle value to check. Must be between -180 and +180. |
min |
number | The minimum angle that is allowed (must be -180 or greater). |
max |
number | The maximum angle that is allowed (must be 180 or less). |
Returns:
The new angle value, returns the same as the input angle if it was within bounds
Do not use this function.
Round to the next whole number away from zero.
E.g. ceil(1.3) == 2, and ceil(-2.3) == -3.
Parameters:
| Name | Type | Description |
|---|---|---|
value |
number | Any number. |
Returns:
The rounded value of that number.
Generate a random bool result based on the chance value.
Returns true or false based on the chance value (default 50%). For example if you wanted a player to have a 30% chance of getting a bonus, call chanceRoll(30) - true means the chance passed, false means it failed.
Parameters:
| Name | Type | Description |
|---|---|---|
chance |
number | The chance of receiving the value. A number between 0 and 100 (effectively 0% to 100%). |
Returns:
True if the roll passed, or false otherwise.
Returns the rounded distance between the two given set of coordinates.
Parameters:
| Name | Type | Description |
|---|---|---|
x1 |
number | |
y1 |
number | |
x2 |
number | |
y2 |
number |
Returns:
The distance between this Point object and the destination Point object.
Do not use this function.
Round to the next whole number towards zero.
E.g. floor(1.7) == 1, and floor(-2.7) == -2.
Parameters:
| Name | Type | Description |
|---|---|---|
value |
number | Any number. |
Returns:
The rounded value of that number.
Fetch a random entry from the given array.
Will return null if there are no array items that fall within the specified range or if there is no item for the randomly choosen index.
Parameters:
| Name | Type | Description |
|---|---|---|
objects |
any[] | An array of objects. |
startIndex |
integer | Optional offset off the front of the array. Default value is 0, or the beginning of the array. |
length |
integer | Optional restriction on the number of values you want to randomly select from. |
Returns:
The random object that was selected.
A one dimensional linear interpolation of a value.
Parameters:
| Name | Type | Description |
|---|---|---|
a |
number | |
b |
number | |
weight |
number |
Returns:
Ensures the given value is between min and max inclusive.
Parameters:
| Name | Type | Description |
|---|---|---|
value |
number | The value to limit. |
min |
number | The minimum the value can be. |
max |
number | The maximum the value can be. |
Returns:
The limited value.
Normalizes a latitude to the [-90,90] range. Latitudes above 90 or below -90 are capped, not wrapped.
Parameters:
| Name | Type | Description |
|---|---|---|
lat |
number | The latitude to normalize, in degrees. |
Returns:
Returns the latitude, fit within the [-90,90] range.
Normalizes a longitude to the [-180,180] range. Longitudes above 180 or below -180 are wrapped.
Parameters:
| Name | Type | Description |
|---|---|---|
lng |
number | The longitude to normalize, in degrees. |
Returns:
Returns the longitude, fit within the [-180,180] range.
Create an array representing the inclusive range of numbers (usually integers) in [start, end].
Parameters:
| Name | Type | Description |
|---|---|---|
start |
number | The minimum value the array starts with. |
end |
number | The maximum value the array contains. |
Returns:
The array of number values.
Create an array of numbers (positive and/or negative) progressing from start
up to but not including end by advancing by step.
If start is less than stop a zero-length range is created unless a negative step is specified.
Certain values for start and end (eg. NaN/undefined/null) are coerced to 0;
for forward compatibility make sure to pass in actual numbers.
Parameters:
| Name | Type | Argument | Default | Description |
|---|---|---|---|---|
start |
number | The start of the range. |
||
end |
number | The end of the range. |
||
step |
number |
<optional> |
1 | The value to increment or decrement by. |
Returns:
Returns the new array of numbers.
Randomly returns either a 1 or -1.
Returns:
Either 1 or -1
Removes a random object from the given array and returns it.
Will return null if there are no array items that fall within the specified range or if there is no item for the randomly choosen index.
Parameters:
| Name | Type | Description |
|---|---|---|
objects |
any[] | An array of objects. |
startIndex |
integer | Optional offset off the front of the array. Default value is 0, or the beginning of the array. |
length |
integer | Optional restriction on the number of values you want to randomly select from. |
Returns:
The random object that was removed.
Moves the element from the start of the array to the end, shifting all items in the process.
Parameters:
| Name | Type | Description |
|---|---|---|
array |
any[] | The array to shift/rotate. The array is modified. |
Returns:
The shifted value.
Shuffles the data in the given array into a new order.
Parameters:
| Name | Type | Description |
|---|---|---|
array |
any[] | The array to shuffle |
Returns:
The array
Snaps a value to the nearest value in an array.
Parameters:
| Name | Type | Description |
|---|---|---|
input |
number | |
arr |
number[] | |
sort |
boolean | True if the array needs to be sorted. |
Returns:
Parameters:
| Name | Type | Description |
|---|---|---|
n |
number |
