export const sum = (a: number, b: number) => (a + b); export const count = (previous: number, current: number) => (previous + (current ? 1 : 0)); export const polarToCartesian = (angle: number, radius: number) => { // This looks weird, but believe me it is right // 0 degrees refers to straight up in this case because of how d3 handles things // because the screen 0,0 position is top left we need to flip the y value return { x: radius * Math.sin(angle), y: - radius * Math.cos(angle) }; }; export const cartesianToPolar = (x: number, y: number) => { return { angle: Math.atan(y / x), radius: Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) }; }; export const distance = (x0: number, y0: number, x1: number, y1: number) => { return Math.sqrt(Math.pow(x1 - x0, 2) + Math.pow(y1 - y0, 2)); }; export const unitVector = (x, y): number[] => { const magnitude = distance(0, 0, x, y); return [x / magnitude, y / magnitude]; }; export const averagePoint = (points: number[][]): number[] => { let x = 0; let y = 0; points.forEach(point => { const [px, py] = point; x += px; y += py; }); return [x / points.length, y / points.length]; }