import { Point } from "./Polygon"; /** * Concatenates two arrays component wise. * a[i], b[i+offset] => [a[i], b[i+offset]] */ export function zip<S,T>(a:S[], b:T[], offset:number = 0): Array<[S, T]> { if(offset < 0) { throw new Error("The offset is negative."); } if(offset % 1 !== 0) { throw new Error("The offset is not an integer."); } if(a.length !== b.length) { throw new Error("Input arrays have different lengths."); } const output:Array<[S, T]> = []; const length = a.length; for(let i=0;i<length;i++) { output.push([ a[i], b[(i+offset)%length], ]); } return output; } export function midPoint(a:Point, b:Point, scalar:number): Point { const cx = scalar * (b.x - a.x) + a.x; const cy = scalar * (b.y - a.y) + a.y; return new Point(cx, cy); } export function clamp(value:number, lowerBound:number|null = 0, upperBound:number|null = 1): number { if(lowerBound != null) { value = Math.max(lowerBound, value); } if(upperBound != null) { value = Math.min(upperBound, value); } return value; } export function randomizeScalar( baseValue:number, randomizeAmplitude:number, lowerBound:number | null, upperBound:number | null, ): number { if(randomizeAmplitude !== 0) { baseValue += 2 * randomizeAmplitude * (Math.random() - 0.5); } return clamp(baseValue, lowerBound, upperBound); }