librmb 1.0
Rambunction 4330 Utility Library
Loading...
Searching...
No Matches
misc.h
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5
6namespace rmb::math {
7
15template <typename T> inline bool withinRange(T n, T lo, T hi) {
16 if (hi < lo)
17 std::swap(lo, hi);
18
19 return n > lo && n < hi;
20}
21
32template <typename T>
33inline T map(T n, T start1, T stop1, T start2, T stop2,
34 bool withinBounds = true) {
35 const T newval = (n - start1) / (stop1 - start1) * (stop2 - start2) + start2;
36 if (!withinBounds) {
37 return newval;
38 }
39 if (start2 < stop2) {
40 return std::clamp(newval, start2, stop2);
41 } else {
42 return std::clamp(newval, stop2, start2);
43 }
44}
45} // namespace rmb::math