blob: a00b1235b394b0e1f315254ca3bbb6d09c23ca30 [file] [log] [blame]
James Kuszmaul6035c322024-01-31 22:27:53 -08001#ifndef FRC971_MATH_INTERPOLATE_H_
2#define FRC971_MATH_INTERPOLATE_H_
3namespace frc971::math {
4
5// Takes a and b linear interpolates between the two based on the scalar t.
6// If t == 0, returns a; if t == 1.0, returns b.
7// The semantics of this should be identical to std::lerp().
8template <typename T, typename Scalar>
9T lerp(const T &a, const T &b, Scalar t) {
10 return (static_cast<Scalar>(1.0) - t) * a + t * b;
11}
12
13// For two points (x1, y1) and (x2, y2) uses a linear interpolation
14// to identify the value of y at x.
15// Will linearly extrapolate if x is outside of [x1, x2].
16template <typename T, typename Scalar>
17T Interpolate(Scalar x1, Scalar x2, const T &y1, const T &y2, Scalar x) {
18 return lerp(y1, y2, (x - x1) / (x2 - x1));
19}
20} // namespace frc971::math
21#endif // FRC971_MATH_INTERPOLATE_H_