Mathf

class 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.

Since

1.0.0-pre

See also

Types

Link copied to clipboard
Exception thrown if no intercept of two lines is found.

Functions

Link copied to clipboard
open fun angleModulus(angle: Measure<Angle>): Measure<Angle>
Wraps an angle to the range -pi to pi radians.
Link copied to clipboard
open fun applyDeadband(value: Double, deadband: Double): Double
open fun applyDeadband(value: Float, deadband: Float): Float
open fun applyDeadband(value: Double, deadband: Double, maxMagnitude: Double): Double
open fun applyDeadband(value: Float, deadband: Float, maxMagnitude: Float): Float
Returns 0.0 if the given value is within the specified range around zero.
Link copied to clipboard
open fun clamp(value: Double, low: Double, high: Double): Double
open fun clamp(value: Int, low: Int, high: Int): Int
Returns value clamped between low and high boundaries.
open fun clamp(value: Float, low: Float, high: Float): Float
Return value clamped between low and high boundaries.
Link copied to clipboard
open fun closestPowerOfTwo(value: Int): Int
Returns the closest power of two that is equal to or larger than the specified value.
Link copied to clipboard
open fun deltaAngle(current: Measure<Angle>, target: Measure<Angle>): Measure<Angle>
Calculates the shortest difference between two given angles.
Link copied to clipboard
open fun gamma(value: Double, absMax: Double, gamma: Double): Double
open fun gamma(value: Float, absMax: Float, gamma: Float): Float
Applies gamma correction to a given value within a specified range.
Link copied to clipboard
open fun inputModulus(input: Double, minimumInput: Double, maximumInput: Double): Double
open fun inputModulus(input: Float, minimumInput: Float, maximumInput: Float): Float
Returns modulus of input.
Link copied to clipboard
open fun interpolate(startValue: Double, endValue: Double, t: Double): Double
open fun interpolate(startValue: Float, endValue: Float, t: Float): Float
Perform linear interpolation between two values.
Link copied to clipboard
Perform linear interpolation like interpolate, but interpolates correctly when they wrap around 1 revolution (360 degrees).
Link copied to clipboard
open fun interpolateUnclamped(startValue: Double, endValue: Double, t: Double): Double
open fun interpolateUnclamped(startValue: Float, endValue: Float, t: Float): Float
Perform linear interpolation between two values.
Link copied to clipboard
open fun inverseInterpolate(startValue: Double, endValue: Double, q: Double): Double
open fun inverseInterpolate(startValue: Float, endValue: Float, q: Float): Float
Return where within interpolation range [0, 1] q is between startValue and endValue.
Link copied to clipboard
open fun isNear(expected: Double, actual: Double, tolerance: Double): Boolean
open fun isNear(expected: Float, actual: Float, tolerance: Float): Boolean
open fun isNear(expected: Double, actual: Double, tolerance: Double, min: Double, max: Double): Boolean
open fun isNear(expected: Float, actual: Float, tolerance: Float, min: Float, max: Float): Boolean
Checks if the given value matches an expected value within a certain tolerance.
Link copied to clipboard
open fun isPowerOfTwo(value: Int): Boolean
Returns whether the given value is a power of two.
Link copied to clipboard
open fun lineIntersection(p1: Vector2d, p2: Vector2d, p3: Vector2d, p4: Vector2d): Vector2d
Find the intersection of two lines.
Link copied to clipboard
open fun lineSegmentIntersection(p1: Vector2d, p2: Vector2d, p3: Vector2d, p4: Vector2d): Vector2d
Find the intersection of two line segments.
Link copied to clipboard
open fun moveTowards(current: Double, target: Double, maxDelta: Double): Double
open fun moveTowards(current: Float, target: Float, maxDelta: Float): Float
Moves a value current towards target.
Link copied to clipboard
open fun moveTowardsAngle(current: Measure<Angle>, target: Measure<Angle>, maxDelta: Measure<Angle>): Measure<Angle>
Same as moveTowards, but makes sure the values interpolate correctly when they wrap around 1 revolution (360 degrees).
Link copied to clipboard
open fun nextPowerOfTwo(value: Int): Int
Returns the next power of two that is equal to or larger than the specified value.
Link copied to clipboard
Normalizes the given angle to be within the range of [0, 2pi].
Link copied to clipboard
open fun pingPong(t: Double, length: Double): Double
open fun pingPong(t: Float, length: Float): Float
PingPongs (bounces) the value t, so that it is never larger than length and never smaller than 0.
Link copied to clipboard
open fun repeat(t: Double, length: Double): Double
open fun repeat(t: Float, length: Float): Float
Loops the value t, so that it is never larger than length and never smaller than 0.
Link copied to clipboard
open fun scale(n: Double, x1: Double, x2: Double, y1: Double, y2: Double): Double
open fun scale(n: Float, x1: Float, x2: Float, y1: Float, y2: Float): Float
Scale a number in the range of x1 to x2, to the range of y1 to y2.
Link copied to clipboard
open fun smoothDamp(current: Double, target: Double, @NonNull currentVelocity: Reference<Double>, smoothTime: Measure<Time>, maxVelocity: Double, deltaTime: Measure<Time>): Double
open fun smoothDamp(current: Float, target: Float, currentVelocity: Reference<Double>, smoothTime: Measure<Time>, maxVelocity: Float, deltaTime: Measure<Time>): Float
Gradually changes a value towards a desired goal over time.
Link copied to clipboard
open fun smoothDampAngle(current: Measure<Angle>, target: Measure<Angle>, currentVelocity: Reference<Double>, smoothTime: Measure<Time>, maxVelocity: Double, deltaTime: Measure<Time>): Measure<Angle>
Gradually changes an angle towards a desired goal over time.
Link copied to clipboard
open fun smoothStep(from: Double, to: Double, t: Double): Double
open fun smoothStep(from: Float, to: Float, t: Float): Float
Interpolates between min and max with smoothing at the limits.
Link copied to clipboard
open fun solveQuadratic(a: Double, b: Double, c: Double): List<Double>
open fun solveQuadratic(a: Float, b: Float, c: Float): List<Float>
Solve a quadratic for x, given coefficients a, b, and c.