import * as d3 from 'd3' export function class2color( classes2colors: Record, cls: string, colors: string[], numClasses: number ): string { if (!classes2colors[cls]) { classes2colors[cls] = colors[numClasses % colors.length] } return classes2colors[cls] } export function class2darkenColor(color: string): string { return d3.rgb(color).darker(1).toString() } export function unitaryVector(source: any, target: any, newLength?: number) { const length = Math.sqrt(Math.pow(target.x - source.x, 2) + Math.pow(target.y - source.y, 2)) / Math.sqrt(newLength || 1) return { x: (target.x - source.x) / length, y: (target.y - source.y) / length } } export function unitaryNormalVector(source: any, target: any, newLength?: number) { const vector = unitaryVector(source, target, newLength) return rotate({ x: 0, y: 0 }, vector, 90) } export function rotate(center: { x: number; y: number }, point: { x: number; y: number }, angle: number) { const radians = (Math.PI / 180) * angle const cos = Math.cos(radians) const sin = Math.sin(radians) return { x: cos * (point.x - center.x) + sin * (point.y - center.y) + center.x, y: cos * (point.y - center.y) - sin * (point.x - center.x) + center.y } }