export class MathHelper {
    /**约束n在[min,max]之间*/
    public static Clamp(n: number, min: number, max: number) {
        return n > max ? max : (n < min ? min : n);
    }

    /**将n从[a,b]映射到区间[min,max]*/
    public static ClampRange(n: number, a: number, b: number, min: number, max: number) {
        return (MathHelper.Clamp(n, a, b) - a) / (b - a) * (max - min) + min;
    }

    public static ToPercentage(n: number, min: number, max: number) {
        return (n - min) / (max - min);
    }

    /**1deg=?rad*/
    public static ToRad() {
        return Math.PI / 180;
    }

    /**1rad=?deg*/
    public static ToDeg() {
        return 180 / Math.PI;
    }

    /**[min,max)之间小数*/
    public static Rand(min: number, max: number) {
        return Math.random() * (max - min) + min;
    }

    /**[min,max]之间整数*/
    public static RandInt(min: number, max: number) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    }

    /**线性插值*/
    public static Lerp(a: number, b: number, t: number) {
        return a * (1 - t) + b * t;
    }

    /**在num前面补prefix，长度固定len位*/
    public static PrefixNum(num: number, len: number, prefix: string = "0") {
        return (Array(len).join(prefix) + num).slice(-len);
    }

    /**限制角度在[-180,180] */
    public static ClampEuler(angle: number) {
        while (angle > 180) {
            angle -= 360;
        }
        while (angle < -180) {
            angle += 360;
        }
        return angle;
    }
}