import { Range2D } from './types'; /** * Computes the magnitude of a vector. * @param vector the vector for which to compute the magnitude. */ export const magnitude = (vector: Range2D): number => { const width = vector.x.end - vector.x.start; const height = vector.y.end - vector.y.start; return Math.sqrt(width * width + height * height); }; /** * Computes the angle of a vector in degrees. * @param vector the vector for which to compute the angle. */ export const angle = (vector: Range2D): number => { const width = vector.x.end - vector.x.start; const height = vector.y.end - vector.y.start; if (width === 0 && height === 0) return 0.0; return (Math.atan(height / width) * 180.0) / Math.PI; }; /** * Returns path data for drawing an arrow head on a horizontal SVG line ending at the given x coordinate. * @param x the x coordinate where the line ends. * @param arrowHeadSize the length of each half of the arrow head. */ export const arrowHead = (x: number, arrowHeadSize: number): string => { const d: number = arrowHeadSize / Math.SQRT2; const t: number = x - d; return `M ${t} ${d} L ${x} 0 L ${t} ${-d} M ${x} 0`; };