Skip to content

Instantly share code, notes, and snippets.

@delaneyj
Created May 18, 2023 17:03
Show Gist options
  • Save delaneyj/98b95bfea377c40bf979bfd2bfe462bd to your computer and use it in GitHub Desktop.
Save delaneyj/98b95bfea377c40bf979bfd2bfe462bd to your computer and use it in GitHub Desktop.
export const ceil = Math.ceil
export const floor = Math.floor
export const pow = Math.pow
export const sqrt = Math.sqrt
export const sin = Math.sin
export const sinh = Math.sinh
export const asinh = Math.asinh
export const asin = Math.asin
export const cos = Math.cos
export const acos = Math.acos
export const tan = Math.tan
export const tanh = Math.tanh
export const atan = Math.atan
export const atan2 = Math.atan2
export const min = Math.min
export const max = Math.max
export const abs = Math.abs
export const exp = Math.exp
export const log = Math.log
export const log10 = Math.log10
export const log2 = Math.log2
export const rand = Math.random
export const PI = Math.PI
export const Tau = 2 * PI
export const epsilon = 0.0001
export const deg2Rad = Tau / 360
export const rad2Deg = 360 / Tau
export const round = Math.round
export const one60x18 = 10n ** 18n
export const toRad = (value: number): number => (value * PI) / 180
export const toDeg = (value: number): number => (value * 180) / PI
// Linear mapping from range <a1, a2> to range <b1, b2>
export function fit(
x: number,
oldMin: number,
oldMax: number,
newMin: number,
newMax: number,
): number {
return newMin + ((x - oldMin) * (newMax - newMin)) / (oldMax - oldMin)
}
export function fit01(x: number, newMin: number, newMax: number): number {
return fit(x, 0, 1, newMin, newMax)
}
export function roundFit01(x: number, newMin: number, newMax: number): number {
return round(fit01(x, newMin, newMax))
}
export function fitMax(x: number, newMax: number): number {
return fit01(x, 0, newMax)
}
export function clamp(v: number, minimum: number, maximum: number): number {
let realMin = minimum
let realMax = maximum
if (maximum < realMin) {
realMin = maximum
realMax = minimum
}
return max(realMin, min(realMax, v))
}
export function clampFit(
x: number,
oldMin: number,
oldMax: number,
newMin: number,
newMax: number,
): number {
const f = fit(x, oldMin, oldMax, newMin, newMax)
return clamp(f, newMin, newMax)
}
export function clampFit01(x: number, newMin: number, newMax: number): number {
const f = fit01(x, newMin, newMax)
return clamp(f, newMin, newMax)
}
export function clamp01(v: number): number {
return clamp(v, 0, 1)
}
export function fitArray(
arr: ArrayLike<number>,
oldMin: number,
oldMax: number,
newMin: number,
newMax: number,
): Array<number> {
const l = arr.length
const out = new Array<number>(l)
for (let i = 0; i < l; i++) {
out[i] = fit(arr[i], oldMin, oldMax, newMin, newMax)
}
return out
}
export function randRange(min: number, max: number, int = false): number {
const n = clamp(rand() * max, min, max)
if (int) return floor(n)
return n
}
export const alphaNumeric = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
export function randID(length = 16): string {
const result = new Array<string>()
for (let i = 0; i < length; i++) {
result.push(alphaNumeric.charAt(Math.floor(Math.random() * alphaNumeric.length)))
}
return result.join('')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment