Created
February 28, 2019 11:27
-
-
Save tripulse/eda1639c908be6fd315485c70866cc7a to your computer and use it in GitHub Desktop.
The p5's map function in native JavaScript!
This file contains hidden or 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
| /** | |
| * @description Maps a number in a range into another range. | |
| * @param {Number} val - The value to map. | |
| * @param {Number} v_min - The minimum value of the 'value'. | |
| * @param {Number} v_max - The maximum value of the 'value'. | |
| * @param {Number} m_min - The minimum value to map. | |
| * @param {Number} m_max - The maximum value to map. | |
| * @returns Number | |
| */ | |
| Math.map = function(val, v_min, v_max, m_min, m_max) { | |
| // Make this range into (0..1) | |
| val = (val - v_min) / (v_max - v_min); | |
| // Convert the range to (m_min..m_max) | |
| return m_min + val * (m_max - m_min); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AW,CAP