mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-22 19:49:19 +02:00
Refactor clamp_circular
This commit is contained in:
@@ -252,13 +252,19 @@ template <typename T> bool is_equal(T const &Left, T const &Right, T const Epsil
|
|||||||
}
|
}
|
||||||
|
|
||||||
// keeps the provided value in specified range 0-Range, as if the range was circular buffer
|
// keeps the provided value in specified range 0-Range, as if the range was circular buffer
|
||||||
template <typename Type_> Type_ clamp_circular(Type_ Value, Type_ const Range = static_cast<Type_>(360))
|
template <typename T> T clamp_circular(T Value, T const Range = T(360))
|
||||||
{
|
{
|
||||||
|
if constexpr (std::is_floating_point_v<T>)
|
||||||
|
{
|
||||||
|
Value = std::fmod(Value, Range);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Value %= Range;
|
||||||
|
}
|
||||||
|
|
||||||
Value -= Range * (int)(Value / Range); // clamp the range to 0-360
|
if (Value < T(0))
|
||||||
if (Value < Type_(0))
|
|
||||||
Value += Range;
|
Value += Range;
|
||||||
|
|
||||||
return Value;
|
return Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user