Created
July 15, 2022 22:19
-
-
Save odbol/7c4bfb51f9a32c02c5ed28c0b9e31351 to your computer and use it in GitHub Desktop.
Collection of mathy math functions for mathing and animating
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type float = number; | |
/** | |
* Interpolate a value with specified extrema, to a new value between new extrema. | |
* | |
* @param value the current value | |
* @param inputMin minimum the input value can reach | |
* @param inputMax maximum the input value can reach | |
* @param outputMin minimum the output value can reach | |
* @param outputMax maximum the output value can reach | |
*/ | |
export function map(value: float, inputMin: float, inputMax: float, outputMin: float, outputMax: float): float { | |
return outputMin + (outputMax - outputMin) * ((value - inputMin) / (inputMax - inputMin)); | |
} | |
/** | |
* Same as {@link #map}, but clamps the values so they never exceed the extrema. | |
*/ | |
export function mapClamped(value: float, inputMin: float, inputMax: float, outputMin: float, outputMax: float): float { | |
return Math.max(outputMin, Math.min(outputMax, map(value, inputMin, inputMax, outputMin, outputMax))); | |
} | |
/*** | |
* Interpolate between values a and b. | |
*/ | |
export function lerp(a: float, b: float, fraction: float): float { | |
return (a * (1.0 - fraction)) + (b * fraction); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment