Boboter
Loading...
Searching...
No Matches
map_value.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <algorithm> // Required for std::clamp
11
23inline int32_t map_value(const int32_t x, const int32_t in_min, const int32_t in_max, const int32_t out_min, const int32_t out_max) {
24 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
25}
26
38inline uint16_t map_value(const float x, const float in_min, const float in_max, const uint16_t out_min, const uint16_t out_max) {
39 // Perform mapping using float arithmetic
40 float result = (x - in_min) * (static_cast<float>(out_max) - static_cast<float>(out_min)) / (in_max - in_min) + static_cast<float>(out_min);
41
42 // Clamp the result to the output range to handle potential floating point errors or
43 // input 'x' being slightly outside [in_min, in_max].
44 // Then cast to uint16_t.
45 return static_cast<uint16_t>(std::clamp(result, static_cast<float>(out_min), static_cast<float>(out_max)));
46}
int32_t map_value(const int32_t x, const int32_t in_min, const int32_t in_max, const int32_t out_min, const int32_t out_max)
Maps an int32_t value of a range of values to another range of values.
Definition map_value.h:23