Mathf

object Mathf

Extended math utility functions.

This class is effectively a combination of the math found in WPILib's MathUtil and Unity's Mathf features, adjusted to use WPIUnits and custom classes.

This class internally uses the Apache Commons FastMath methods.

Since

1.0.0-pre

See also

FastMath
MathUtils

Types

Link copied to clipboard

Exception thrown if no intercept is found when using the intersection methods of this class.

Functions

Link copied to clipboard
@JvmName(name = "applyDeadbandInfix")
infix fun Number.applyDeadband(deadband: Number): Double

Returns 0.0 if the given value is within the specified range around zero. The remaining range between the deadband and the maximum magnitude is scaled from 0.0 to the maximum magnitude. Maximal magnitude is 1 for this infix overload.

fun Number.applyDeadband(deadband: Number, maxMagnitude: Number = 1): Double

Returns 0.0 if the given value is within the specified range around zero. The remaining range between the deadband and the maximum magnitude is scaled from 0.0 to the maximum magnitude.

Link copied to clipboard
infix fun Number.approx(other: Number): Boolean

Checks if two values are approximately equal, such that floating point errors are accounted for.

Link copied to clipboard

Returns value clamped between low and high boundaries.

Link copied to clipboard

Returns the closest power of two that is equal to or larger than the specified value.

Link copied to clipboard

Convert degrees to radians.

Link copied to clipboard

Calculates the shortest difference between two given angles.

Link copied to clipboard
fun Number.gamma(absMax: Number, gamma: Number): Double

Applies gamma correction to a given value within a specified range.

Link copied to clipboard

Determines the index with the highest value in this array.

Link copied to clipboard

Determines the index with the lowest value in this array.

Link copied to clipboard

Return where within interpolation range 0, 1 q is between the start and end pair.

fun inverseLerp(startValue: Number, endValue: Number, q: Number): Double

Return where within interpolation range 0, 1 q is between startValue and endValue.

Link copied to clipboard
fun Number.isNear(expected: Number, tolerance: Number): Boolean

Checks if the given value matches an expected value within a certain tolerance.

fun Number.isNear(expected: Number, tolerance: Number, min: Number, max: Number): Boolean

Checks if the given value matches an expected value within a certain tolerance. Supports continuous input for cases like absolute encoders.

Link copied to clipboard

Returns whether the given value is a power of two.

Link copied to clipboard

Perform linear interpolation like lerp, but interpolates correctly when they wrap around 1 revolution (360 degrees).

fun lerp(startValue: Number, endValue: Number, t: Number): Double

Perform linear interpolation between two values.

Link copied to clipboard
fun lerpUnclamped(startValue: Number, endValue: Number, t: Number): Double

Perform linear interpolation between two values.

Link copied to clipboard
fun lineCircleIntersection(p1: Vector2d, p2: Vector2d, center: Vector2d, radius: Number): Pair<Vector2d, Vector2d>

Find the intersection between a line and a circle.

Link copied to clipboard
fun lineIntersection(p1: Vector2d, p2: Vector2d, p3: Vector2d, p4: Vector2d): Vector2d

Find the intersection of two lines.

Link copied to clipboard
fun lineSegmentCircleIntersection(p1: Vector2d, p2: Vector2d, center: Vector2d, radius: Number): Pair<Vector2d, Vector2d>

Find the intersection between a line segment and a circle.

Link copied to clipboard
fun lineSegmentIntersection(p1: Vector2d, p2: Vector2d, p3: Vector2d, p4: Vector2d): Vector2d

Find the intersection of two line segments.

Link copied to clipboard

Same as moveTowards, but makes sure the values interpolate correctly when they wrap around 1 revolution (360 degrees).

fun Number.moveTowards(target: Number, maxDelta: Number): Double

Moves a current value towards target.

Link copied to clipboard

Returns the next power of two that is equal to or larger than the specified value.

Link copied to clipboard
fun Array<Point>.orderPoints(): Array<Point?>

Orders OpenCV camera points to be in the order top-left, top-right, bottom-right and bottom-left.

Link copied to clipboard
infix fun Number.pingPong(length: Number): Double

PingPongs (bounces) the value t, so that it is never larger than length and never smaller than 0.

Link copied to clipboard

Convert radians to degrees.

Link copied to clipboard
infix fun Number.repeat(length: Number): Double

Loops the value, so that it is never larger than length and never smaller than 0.

Link copied to clipboard
@JvmName(name = "roundInfix")
infix fun Number?.round(thDigits: Int): Double
fun Number?.round(thDigits: Int, sigFigs: Int = -1): Double

Round a number to a certain number of decimal points.

Link copied to clipboard

Scale a number in the range of x1 to x2, to the range of y1 to y2.

Link copied to clipboard
fun Measure<Angle>.smoothDamp(target: Measure<Angle>, currentVelocity: Reference<Double>, smoothTime: Measure<Time>, maxVelocity: Number, deltaTime: Measure<Time>): Measure<Angle>

Gradually changes an angle towards a desired goal over time.

fun Number.smoothDamp(target: Number, currentVelocity: Reference<Double>, smoothTime: Measure<Time>, maxVelocity: Number, deltaTime: Measure<Time>): Double

Gradually changes a value towards a desired goal over time.

Link copied to clipboard

Interpolates between min and max with smoothing at the limits.

Link copied to clipboard

Solve a quadratic for x, given coefficients a, b, and c.

Link copied to clipboard

Normalizes the given angle to be within the range of [0, 2pi) radians or [0, 360) degrees.

fun Number.wrap(minimumInput: Number, maximumInput: Number): Double

Returns modulus of input.

Link copied to clipboard

Wraps an angle to the range -pi to pi radians.

Link copied to clipboard

Wraps radians to the range -pi to pi.

Link copied to clipboard

Normalizes the given radians to be within the range [0, 2pi).