Created
July 11, 2019 23:52
-
-
Save alexeden/45c089f78db4891d8486b690e28ffee9 to your computer and use it in GitHub Desktop.
Math Utility Functions
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
| import { curry } from 'ramda'; | |
| export const range = (from: number, to: number) => { | |
| const result = []; | |
| let n = from - 1; | |
| while (++n < to) result.push(n); | |
| return result; | |
| }; | |
| export const clamp = curry((min: number, max: number, value: number) => { | |
| return Math.max(min, Math.min(max, value)); | |
| }); | |
| export const clampLoop = curry((min: number, max: number, value: number) => { | |
| const len = Math.abs(max - min); | |
| let a = value % len; | |
| if (a > max) a -= len; | |
| else if (a < min) a += len; | |
| return a; | |
| }); | |
| export const rad = (degs: number) => Math.PI * degs / 180; | |
| export const absDiff = (a: number, b: number) => Math.abs(b - a); | |
| export const normalize = curry((a: number, b: number, x: number) => (x - Math.min(a, b)) / absDiff(a, b)); | |
| export const mapToRange = curry((a: number, b: number, c: number, d: number, x: number) => { | |
| const r = (d - c) / (b - a); | |
| return (x - a) * r + c; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment