import { Position } from './SuperPath2D' import { DEG_360 } from './conts' export const duration = function (start, end) { return normalizeMinutes(end - start) } export const minutesToHoursMinutes = function (min) { const hours = Math.floor(min / 60) + '' let minutes = min % 60 minutes = Math.floor(minutes) return { hours: hours, minutes: minutes, } } export const minutesToReadable = function (min, breakpoint = 60) { if (min > breakpoint) { const hm = minutesToHoursMinutes(min) return hm.hours + 'h ' + hm.minutes + 'm' } else { return min + 'm' } } type NumberBetween0And1 = number export const getProgressBetweenTwoValuesDeprecated = function ( pos: number, start: number, end: number, ): NumberBetween0And1 { const result = duration(start, pos) / duration(start, end) if (isNaN(result)) { // I am doing this check because when dividing 0/0 it outputs NaN return 1 } else { return result } } export const getProgressBetweenTwoValues = function (pos: number, start: number, end: number): number { const progress = (pos - start) / (end - start) return isNaN(progress) ? 1 : progress } export const normalizeMinutes = function (value: number) { if (value == 1440) return 1440 return value - 1440 * Math.floor(value / 1440) } export const isNumberInsideRange = function (point: number, start: number, end: number) { if (end > start) { if (point < end && point > start) { return true } } else if (start > end) { if (point > start || point < end) { return true } } if (point == start || point == end) { return true } return false } export function normalizeRad(angle: number) { if (angle == DEG_360) { return angle } angle = angle % DEG_360 if (angle < 0) { angle += DEG_360 } return angle } export const distancePointToPoint = function (pointA: Position, pointB: Position) { const y_adj = pointB.y - pointA.y const x_adj = pointB.x - pointA.x return Math.sqrt(y_adj * y_adj + x_adj * x_adj) } // i have a feeling there is something wrong with this function export const angleBetweenTwoPoints = function (b: Position, a: Position) { const d = distancePointToPoint(b, a) const y = (a.y - b.y) / d const x = (a.x - b.x) / d let angle = Math.atan(y / x) if (x >= 0) { angle += Math.PI } angle += Math.PI / 2 return angle } export const minutesToClock = function ( chart: { config: { ampm: boolean } }, minutes, ) { // eslint-disable-next-line @typescript-eslint/no-unsafe-argument minutes = Math.floor(minutes) const hours = Math.floor(minutes / 60) let hoursString = hours + '' minutes = (minutes % 60) + '' if (hoursString.length == 1) { hoursString = '0' + hoursString } // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access if (minutes.length == 1) { minutes = '0' + minutes } if (chart.config.ampm) { if (minutes == 0 && hours % 12 === 0) { return hours > 12 || hours == 0 ? 'midnight' : 'noon' } return ((hours * 1 + 11) % 12) + 1 + ':' + minutes + (hours < 12 ? ' am' : ' pm') } else { return hoursString + ':' + minutes } } export const distanceFromPointToLineSegment = function (x: number, y: number, a: Position, b: Position): number { const x1 = a.x const y1 = a.y const x2 = b.x const y2 = b.y const A = x - x1 const B = y - y1 const C = x2 - x1 const D = y2 - y1 const dot = A * C + B * D const len_sq = C * C + D * D let param = -1 if (len_sq != 0) { // in case of 0 length line param = dot / len_sq } let xx, yy if (param < 0) { xx = x1 yy = y1 } else if (param > 1) { xx = x2 yy = y2 } else { xx = x1 + param * C yy = y1 + param * D } const dx = x - xx const dy = y - yy return Math.sqrt(dx * dx + dy * dy) } export const radToDeg = function (rad) { return Math.round(rad * (180 / Math.PI)) + '°' }