MathUtil
A library of useful math functions.
Types
MathOperation
type
MathOperation =
(
"+"
|
"
-
"
|
"*"
|
"/"
|
"^"
|
"%"
)
A type consisting of all the valid math operations in string format.
Functions
snap
MathUtil.
snap
(
v:
number
,
--
The value to snap.
...:
number
|
{
number
}
--
The array or variadic of number values to snap to.
) →
number
--
The closest value to the given value.
Takes a value and snaps it to the closest one of the following values.
random
MathUtil.
random
(
min:
number
,
--
The minimum value.
max:
number
--
The maximum value.
) →
number
--
The random float.
Returns a random float between the given min and max.
randomFromNumberRange
MathUtil.
randomFromNumberRange
(
) →
number
--
The generated random number.
Returns a random float in the given number range.
randomFromArray
MathUtil.
randomFromArray
(
tbl:
{
number
}
--
The array to get a random number from.
) →
number
--
The random number.
Gets a random number within the given array.
randomFromRanges
Gets a random number within the given ranges.
By default the numbers within the ranges have an equal chance of being selected
(unless the given table has a Weight
index)
local n = MathUtil.randomFromRanges({1, 10}, {20, 40}) -- Returns a random number between 1 and 10 or 20 and 40.
tryRandom
MathUtil.
tryRandom
(
data:
number
|
NumberRange
|
NumberSequence
|
{
number
}
,
--
The data to try and generate a random number from.
...:
any
--
The optional arguments to pass to the random function.
) →
number
Trys to return a random number from the given data. It parses the data to try and figure out which random methodology to use.
round
MathUtil.
round
(
numToRound:
number
,
--
The number to round.
multiple:
number?
--
The multiple to round to. If not specified, will round to the nearest integer.
) →
The
rounded
number.
Rounds a number to the nearest specified multiple.
lerp
MathUtil.
lerp
(
a:
number
,
--
The first number.
b:
number
,
--
The second number.
t:
number
--
The alpha to lerp between the two numbers.
) →
The
lerped
number.
Lerps a number
between two other numbers based on a given alpha.
isBetween
MathUtil.
isBetween
(
numToCheck:
number
,
--
The number to check.
bound1:
number
,
--
The first bound.
bound2:
number
--
The second bound.
) →
boolean
--
Whether or not the number is between the two bounds.
Checks if a number is between two other numbers.
isClose
MathUtil.
isClose
(
num1:
number
,
--
The first number.
num2:
number
,
--
The second number.
epsilon:
number?
--
The epsilon to check between the two numbers. Defaults to 0.0001
.
) →
boolean
--
Whether or not the two numbers are close to each other.
Checks if two numbers are close to each other within a given epsilon.
numbersToSequence
MathUtil.
numbersToSequence
(
values:
CanBeState
<
{
CanBeState
<
number
>
}
>
|
CanBeState
<
number
>
,
--
The values to convert to a NumberSequence.
splitPoints:
(
CanBeState
<
{
CanBeState
<
number
>
}
>
|
CanBeState
<
number
>
)
?
,
--
The points along the line at which the values are split. Optional only if there is one value.
use:
(
(
v:
any
)
→
any
)
?
--
Optional function to provide if you are using this inside a Fusion state object and want to react to changes.
) →
NumberSequence
Converts a table of numbers to a NumberSequence grouped by split points. This is very useful when working with UI Gradient's transparency.
local values = {4, 8}
local sequence = MathUtil.numbersToSequence(values, 0.5)
-- The sequence will be 4 at 0, 4 at 0.5, 8 at 0.5 + EPSILON, and 8 at 1.
operate
MathUtil.
operate
(
a:
number
,
operator:
string
,
b:
number
) →
number
Performs a math operation on two numbers.