mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
[wpiutil,wpimath] Add generic InterpolatingTreeMap (#5372)
This commit is contained in:
@@ -146,6 +146,26 @@ public final class MathUtil {
|
||||
return startValue + (endValue - startValue) * MathUtil.clamp(t, 0, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return where within interpolation range [0, 1] q is between startValue and endValue.
|
||||
*
|
||||
* @param startValue Lower part of interpolation range.
|
||||
* @param endValue Upper part of interpolation range.
|
||||
* @param q Query.
|
||||
* @return Interpolant in range [0, 1].
|
||||
*/
|
||||
public static double inverseInterpolate(double startValue, double endValue, double q) {
|
||||
double totalRange = endValue - startValue;
|
||||
if (totalRange <= 0) {
|
||||
return 0.0;
|
||||
}
|
||||
double queryToStart = q - startValue;
|
||||
if (queryToStart <= 0) {
|
||||
return 0.0;
|
||||
}
|
||||
return queryToStart / totalRange;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given value matches an expected value within a certain tolerance.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user