Const
Calculates the angle between two points in radians
First point x coordinate
First point y coordinate
Second point x coordinate
Second point y coordinate
Angle in radians
// Calculate angle between points (0,0) and (1,1)
const angle = MathUtils.angle(0, 0, 1, 1); // π/4 radians
Clamps a number between a minimum and maximum value
Number to clamp
Minimum value
Maximum value
Clamped number
MathUtils.clamp(10, 0, 5); // 5
MathUtils.clamp(-10, 0, 5); // 0
MathUtils.clamp(3, 0, 5); // 3
Calculates the Euclidean distance between two points
First point x coordinate
First point y coordinate
Second point x coordinate
Second point y coordinate
Distance between points
MathUtils.distance(0, 0, 3, 4); // 5
MathUtils.distance(1, 1, 4, 5); // 5
Checks if a number is within a range
Number to check
Minimum value
Maximum value
Whether to include min and max values
True if in range, false otherwise
MathUtils.isInRange(5, 0, 10); // true
MathUtils.isInRange(5, 0, 5, true); // true
MathUtils.isInRange(5, 0, 5, false); // false
Maps a number from one range to another
Number to map
Input range minimum
Input range maximum
Output range minimum
Output range maximum
Mapped number
// Map a value from 0-100 range to 0-255 range
MathUtils.map(50, 0, 100, 0, 255); // 127.5
// Map a percentage to a color value
MathUtils.map(0.75, 0, 1, 0, 255); // 191.25
Calculates the median of an array of numbers
Array of numbers
Median value
If array is empty
// Odd number of elements
MathUtils.median([1, 2, 3, 4, 5]); // 3
// Even number of elements (average of middle two)
MathUtils.median([1, 2, 3, 4]); // 2.5
Calculates the mode(s) of an array of numbers
Array of numbers
Array of mode values (most frequently occurring numbers)
// Single mode
MathUtils.mode([1, 2, 2, 3, 3, 3]); // [3]
// Multiple modes
MathUtils.mode([1, 1, 2, 2]); // [1, 2]
// All numbers appear once
MathUtils.mode([1, 2, 3]); // [1, 2, 3]
Calculates the percentile of a value in an array of numbers
Array of numbers
Value to find percentile for
Percentile (0-100)
const scores = [1, 2, 3, 4, 5];
MathUtils.percentile(scores, 3); // 40
MathUtils.percentile(scores, 5); // 100
MathUtils.percentile(scores, 1); // 0
Calculates the quartiles of an array of numbers
Array of numbers
Object containing quartiles (q1, q2, q3)
const { q1, q2, q3 } = MathUtils.quartiles([1, 2, 3, 4, 5, 6, 7, 8]);
// q1 = 2.5 (25th percentile)
// q2 = 4.5 (50th percentile/median)
// q3 = 6.5 (75th percentile)
Generates a random number between min and max (inclusive)
Minimum value
Maximum value
Random number
// Random number between 0 and 1
MathUtils.random(0, 1); // e.g., 0.2345
// Random number between -10 and 10
MathUtils.random(-10, 10); // e.g., 3.4567
Generates a random integer between min and max (inclusive)
Minimum value
Maximum value
Random integer
// Random integer between 1 and 6 (dice roll)
MathUtils.randomInt(1, 6); // e.g., 4
// Random integer between 0 and 100
MathUtils.randomInt(0, 100); // e.g., 42
Rounds a number to a specified number of decimal places
Number to round
Number of decimal places
Rounded number
MathUtils.round(3.14159, 2); // 3.14
MathUtils.round(3.14159, 3); // 3.142
MathUtils.round(3.14159, 0); // 3
Calculates the standard deviation of an array of numbers
Array of numbers
Standard deviation
MathUtils.standardDeviation([1, 2, 3, 4, 5]); // 1.4142...
MathUtils.standardDeviation([2, 4, 4, 4, 6]); // 1.4142...
MathUtils.standardDeviation([1, 1, 1, 1]); // 0
Calculates the absolute value of a number