All files mathUtils.ts

56.1% Statements 23/41
75% Branches 6/8
70% Functions 7/10
56.1% Lines 23/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 881x   37x   4x   159x     1x   1x       2x     1x       189x     1x 2x     1x 1x     1x 189x   38x   151x     1x           244x     1x                           1x   1x                                 1x        
export function clamp(i: number, min: number, max: number): number {
    if (isNaN(i) || i <= min) {
        return min;
    } else if (i >= max) {
        return max;
    }
    return i;
}
 
export function normalize(i: number, min: number, max: number): number {
    if (isNaN(i) || i <= min) {
        return 0.0;
    } else if (i >= max) {
        return 1.0;
    }
    return i / (max - min);
}
 
export function denormalize(i: number, min: number, max: number): number {
    if (isNaN(i)) {
        return min;
    }
    return min + i * (max - min);
}
 
export function degreesToRadians(i: number): number {
    return i * (Math.PI / 180.0);
}
 
export function radiansToDegrees(i: number): number {
    return i * (180.0 / Math.PI);
}
 
export function getHexStringForByte(i: number): string {
    const s: string = Math.round(clamp(i, 0.0, 255.0)).toString(16);
    if (s.length === 1) {
        return "0" + s;
    }
    return s;
}
 
export function lerp(i: number, min: number, max: number): number {
    if (isNaN(i) || i <= 0.0) {
        return min;
    } else if (i >= 1.0) {
        return max;
    }
    return min + i * (max - min);
}
 
export function lerpAnglesInDegrees(i: number, min: number, max: number): number {
    if (i <= 0.0) {
        return min % 360.0;
    } else if (i >= 1.0) {
        return max % 360.0;
    }
    const a: number = (min - max + 360.0) % 360.0;
    const b: number = (max - min + 360.0) % 360.0;
    if (a <= b) {
        return (min - a * i + 360.0) % 360.0;
    }
    return (min + a * i + 360.0) % 360.0;
}
 
const TwoPI: number = Math.PI * 2;
 
export function lerpAnglesInRadians(i: number, min: number, max: number): number {
    if (isNaN(i) || i <= 0.0) {
        return min % TwoPI;
    } else if (i >= 1.0) {
        return max % TwoPI;
    }
    const a: number = (min - max + TwoPI) % TwoPI;
    const b: number = (max - min + TwoPI) % TwoPI;
    if (a <= b) {
        return (min - a * i + TwoPI) % TwoPI;
    }
    return (min + a * i + TwoPI) % TwoPI;
}
 
// Will return infinity if i*10^(precision) overflows number
// note that floating point rounding rules come into play here so values that end up rouding on a .5 round to the nearest even not always up
// so 2.5 rounds to 2
export function roundToPrecisionSmall(i: number, precision: number): number {
    const factor: number = Math.pow(10, precision);
    return Math.round(i * factor) / factor;
}