import type { Vector } from "./Vectors" ; export const { PI } = Math ; export const TAU = PI * 2 ; summary worklet export const bin = ( value ) : 0 | 1 => { "worklet" ; return value ? 1 : 0 ; } ; param value param x param y worklet export const mix = ( value , x , y ) => { "worklet" ; return x * ( 1 - value ) + y * value ; } ; summary worklet export const approximates = ( value , target , epsilon = 0.001 ) => { "worklet" ; return Math . abs ( value - target ) < epsilon ; } ; summary worklet export const normalizeRad = ( value ) => { "worklet" ; const rest = value % TAU ; return rest > 0 ? rest : TAU + rest ; } ; summary worklet export const toDeg = ( rad ) => { "worklet" ; return ( rad * 180 ) / Math . PI ; } ; summary worklet export const toRad = ( deg ) => { "worklet" ; return ( deg * Math . PI ) / 180 ; } ; summary worklet export const avg = ( values ) => { "worklet" ; return values . reduce ( ( a , v ) => a + v , 0 ) / values . length ; } ; summary worklet export const between = ( value , lowerBound , upperBound , inclusive = true ) => { "worklet" ; if ( inclusive ) { return value >= lowerBound && value <= upperBound ; } return value > lowerBound && value < upperBound ; } ; summary example worklet export const clamp = ( value , lowerBound , upperBound ) => { "worklet" ; return Math . min ( Math . max ( lowerBound , value ) , upperBound ) ; } ; description worklet export const cubicBezier = ( t , from , c1 , c2 , to ) => { "worklet" ; const term = 1 - t ; const a = 1 * term ** 3 * t ** 0 * from ; const b = 3 * term ** 2 * t ** 1 * c1 ; const c = 3 * term ** 1 * t ** 2 * c2 ; const d = 1 * term ** 0 * t ** 3 * to ; return a + b + c + d ; } ; summary worklet export const round = ( value , precision = 0 ) => { "worklet" ; const p = Math . pow ( 10 , precision ) ; return Math . round ( value * p ) / p ; } ; const cuberoot = ( x ) => { "worklet" ; const y = Math . pow ( Math . abs ( x ) , 1 / 3 ) ; return x < 0 ? - y : y ; } ; const solveCubic = ( a , b , c , d ) => { "worklet" ; if ( Math . abs ( a ) < 1e-8 ) { a = b ; b = c ; c = d ; if ( Math . abs ( a ) < 1e-8 ) { a = b ; b = c ; if ( Math . abs ( a ) < 1e-8 ) { return [ ] ; } return [ - b / a ] ; } const D = b * b - 4 * a * c ; if ( Math . abs ( D ) < 1e-8 ) { return [ - b / ( 2 * a ) ] ; } else if ( D > 0 ) { return [ ( - b + Math . sqrt ( D ) ) / ( 2 * a ) , ( - b - Math . sqrt ( D ) ) / ( 2 * a ) ] ; } return [ ] ; } const p = ( 3 * a * c - b * b ) / ( 3 * a * a ) ; const q = ( 2 * b * b * b - 9 * a * b * c + 27 * a * a * d ) / ( 27 * a * a * a ) ; let roots ; if ( Math . abs ( p ) < 1e-8 ) { roots = [ cuberoot ( - q ) ] ; } else if ( Math . abs ( q ) < 1e-8 ) { roots = [ 0 ] . concat ( p < 0 ? [ Math . sqrt ( - p ) , - Math . sqrt ( - p ) ] : [ ] ) ; } else { const D = ( q * q ) / 4 + ( p * p * p ) / 27 ; if ( Math . abs ( D ) < 1e-8 ) { roots = [ ( - 1.5 * q ) / p , ( 3 * q ) / p ] ; } else if ( D > 0 ) { const u = cuberoot ( - q / 2 - Math . sqrt ( D ) ) ; roots = [ u - p / ( 3 * u ) ] ; } else { const u = 2 * Math . sqrt ( - p / 3 ) ; const t = Math . acos ( ( 3 * q ) / p / u ) / 3 ; const k = ( 2 * Math . PI ) / 3 ; roots = [ u * Math . cos ( t ) , u * Math . cos ( t - k ) , u * Math . cos ( t - 2 * k ) ] ; } } for ( let i = 0 ; i < roots . length ; i ++ ) { roots [ i ] -= b / ( 3 * a ) ; } return roots ; } ; summary example worklet export const cubicBezierYForX = ( x , a , b , c , d , precision = 2 ) => { "worklet" ; const pa = - a . x + 3 * b . x - 3 * c . x + d . x ; const pb = 3 * a . x - 6 * b . x + 3 * c . x ; const pc = - 3 * a . x + 3 * b . x ; const pd = a . x - x ; const t = solveCubic ( pa , pb , pc , pd ) . map ( ( root ) => round ( root , precision ) ) . filter ( ( root ) => root >= 0 && root <= 1 ) [ 0 ] ; return cubicBezier ( t , a . y , b . y , c . y , d . y ) ; } ; export const fract = ( x ) => { "worklet" ; return x - Math . floor ( x ) ; } ;