mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
refactor(packages/stdlib): add comments for math utils
This commit is contained in:
@@ -11,8 +11,13 @@ import { clamp } from "../clamp";
|
||||
* @returns {number} The mapped value
|
||||
*/
|
||||
export function mapRange(value: number, in_min: number, in_max: number, out_min: number, out_max: number): number {
|
||||
// Zero input range means invalid input, so return lowest output range value
|
||||
if (in_min === in_max)
|
||||
return out_min;
|
||||
|
||||
return (clamp(value, in_min, in_max) - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
||||
// To ensure the value is within the input range, clamp it
|
||||
const clampedValue = clamp(value, in_min, in_max);
|
||||
|
||||
// Finally, map the value from the input range to the output range
|
||||
return (clampedValue - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
||||
}
|
||||
Reference in New Issue
Block a user