Skip to content

Commit

Permalink
Initial implementation of lerp
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinzii authored Mar 5, 2024
1 parent 22fba25 commit d4421ca
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions include/ccmath/detail/lerp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,40 @@

namespace ccm
{
namespace
{
namespace impl
{
template <typename Real>
inline constexpr Real lerp_impl(Real a, Real b, Real dt)
{
if ((a <= 0 && b >= 0) || (a >= 0 && b <= 0))
{
return dt * b + (1 - dt) * a;
}

if (dt == 1)
{
return b;
}

const Real x = a + dt * (b - a);
if ((dt > 1) == (b > a))
{
return b < x ? x : b;
}
else
{
return x < b ? x : b;
}
}
}
}

template <typename T>
inline constexpr T lerp(T a, T b, T t)
{
// TODO: Implement promotion and edge cases.
return lerp_impl(a, b, t);
}
} // namespace ccm

0 comments on commit d4421ca

Please sign in to comment.