graphUtils.js (1066B)
1 function scale(vector, factor) { 2 return vector.map(a => a * factor); 3 } 4 5 function sum(arr) { 6 return arr.reduce((a, b) => a + b); 7 } 8 9 export function sumVectors(a, b) { 10 return a.map((ai, i) => b[i] + ai); 11 } 12 13 export function getVector(start, end) { 14 return start.map((a, i) => end[i] - a); 15 } 16 17 function normalise(vector, scaleFactor = 1) { 18 const length = Math.sqrt(sum(vector.map(a => a*a))); 19 return scale(vector, scaleFactor / length); 20 } 21 22 function interpolate(start, end, along) { 23 return sumVectors(start, scale(getVector(start, end), along)); 24 } 25 26 export function interpolateDistance(start, end, distance) { 27 return sumVectors(start, normalise(getVector(start, end), distance)); 28 } 29 30 export function getMidpoint(points) { 31 return interpolate(points[0], points[1], 0.5); 32 } 33 34 export function getSymmetricBounds(midX, dx, p) { 35 const maxDx = Math.max(midX - p.minX, p.maxX - midX); 36 const dx0 = Math.min(maxDx, dx); 37 return [midX - dx0, midX + dx0]; 38 } 39 40 export function getColourClass(colour) { 41 if (colour == 'auto') return ''; 42 return `color-${colour}`; 43 }