MathUtils
A collection of math utilities.
For Kotlin users, all the following except for the vararg functions are extension functions
MU.toIn
Converts a value to inches
- Java
- Kotlin
MU.toIn(1, DistanceUnit.METER); // 39.37007874015748
1.0.toIn(from = DistanceUnit.METER) // 39.37007874015748
MU.toCm
Converts a value to centimeters
- Java
- Kotlin
MU.toCm(1, DistanceUnit.METER); // 100.0
1.0.toCm(from = DistanceUnit.METER) // 100.0
MU.toRad
Converts a value to radians
- Java
- Kotlin
MU.toRad(1, AngleUnit.DEGREES); // 0.017453292519943295
1.0.toRad(from = AngleUnit.DEGREES) // 0.017453292519943295
MU.zeroIfNaN
Returns 0 if the value is NaN
An overload exists for Floats
- Java
- Kotlin
MU.zeroIfNaN(Double.NaN); // 0.0
MU.zeroIfNaN(1.0); // 1.0
Double.NaN.zeroIfNaN() // 0.0
1.0.zeroIfNaN() // 1.0
MU.isInRange
Checks if a value is within a range
- Java
- Kotlin
MU.isInRange(1, 0, 2); // true
1.isInRange(0, 2) // true
MU.clamp
Clamps a value to a range. Returns it as a Double.
- Java
- Kotlin
MU.clamp(1, 0, 2); // 1.0
MU.clamp(3, 0, 2); // 2.0
1.clamp(0, 2) // 1.0
3.clamp(0, 2) // 2.0
MU.avg
Calculates the average of a list of numbers
This (and the other vararg) method(s) can not take in array of primitives. You must use an array of boxed types (Double, Float, Integer, etc.) if not directly using varargs.
- Java
- Kotlin
double average = MU.avg(1, 2, 3); // 2.0 (can be used as normal varargs)
Double[] values = new Double[] { 1.0, 2.0, 3.0 };
double average = MU.avg(values); // 2.0 (boxed array is fine)
double[] error = new double[] { 1.0, 2.0, 3.0 };
double average = MU.avg(error); // Error because of primitive array
val average = avg(1.0, 2.0, 3.0) // 2.0 (can be used as normal varargs)
val values = arrayOf(1.0, 2.0, 3.0)
val average = avg(*values) // 2.0 (use spread operator to pass array)
MU.maxByMagnitude
Returns the value with the largest magnitude (not absolute value), or 0.0 if the list is empty
- Java
- Kotlin
double max = MU.maxByMagnitude(-1, 2, -3); // 3.0
Double[] values = new Double[] { 1.0, 2.0, 3.0 };
double max = MU.maxByMagnitude(values); // 3.0 (boxed array is fine)
double[] error = new double[] { 1.0, 2.0, 3.0 };
double max = MU.maxByMagnitude(error); // Error because of primitive array
val max = maxByMagnitude(-1.0, 2.0, -3.0) // 3.0
val values = arrayOf(1.0, 2.0, 3.0)
val max = maxByMagnitude(*values) // 3.0 (use spread operator to pass array)
MU.maxMagnitude
Returns the largest magnitude (as absolute value) of a list of numbers, or 0.0 if the list is empty
- Java
- Kotlin
double max = MU.maxMagnitude(-1, 2, -3); // 3.0
Double[] values = new Double[] { 1.0, 2.0, 3.0 };
double max = MU.maxMagnitude(values); // 3.0 (boxed array is fine)
double[] error = new double[] { 1.0, 2.0, 3.0 };
double max = MU.maxMagnitude(error); // Error because of primitive array
val max = maxMagnitude(-1.0, 2.0, -3.0) // 3.0
val values = arrayOf(1.0, 2.0, 3.0)
val max = maxMagnitude(*values) // 3.0 (use spread operator to pass array)
MU.withDeadzone
Returns a value with a deadzone applied. If the value is within the deadzone, it will be set to given origin (defaults to 0.0).
0.0
- Java
- Kotlin
MU.withDeadzone(0.5, 0.1); // 0.5
MU.withDeadzone(0.05, 0.1); // 0.0
MU.withDeadzone(10.5, 1, 10); // 10
0.5.withDeadzone(0.1) // 0.5
0.05.withDeadzone(0.1) // 0.0
10.5.withDeadzone(1.0, origin = 10.0) // 10