tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
lerp.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_CMATH_LERP_HPP
5#define TETL_CMATH_LERP_HPP
6
7#include <etl/_concepts/floating_point.hpp>
8
9namespace etl {
10
11/// Computes a+t(b−a), i.e. the linear interpolation between a and b for
12/// the parameter t (or extrapolation, when t is outside the range [0,1]).
13/// \details https://en.cppreference.com/w/cpp/numeric/lerp
14/// \ingroup cmath
15template <floating_point Float>
16[[nodiscard]] constexpr auto lerp(Float a, Float b, Float t) noexcept -> Float
17{
18 if ((a <= 0 && b >= 0) || (a >= 0 && b <= 0)) {
19 return t * b + (1 - t) * a;
20 }
21
22 if (t == 1) {
23 return b;
24 }
25
26 auto const x = a + t * (b - a);
27 if ((t > 1) == (b > a)) {
28 return b < x ? x : b;
29 }
30 return x < b ? x : b;
31}
32
33} // namespace etl
34
35#endif // TETL_CMATH_LERP_HPP
constexpr auto lerp(Float a, Float b, Float t) noexcept -> Float
Computes a+t(b−a), i.e. the linear interpolation between a and b for the parameter t (or extrapolatio...
Definition lerp.hpp:16
Definition adjacent_find.hpp:9