param n param min export function clamp ( n , min ) ; export function clamp ( n , min , max ) ; export function clamp ( n , min , max ? ) { return Math . max ( min , typeof max !== "undefined" ? Math . min ( n , max ) : n ) ; } param A export function neg ( A ) { return [ - A [ 0 ] , - A [ 1 ] ] ; } param A param B export function add ( A , B ) { return [ A [ 0 ] + B [ 0 ] , A [ 1 ] + B [ 1 ] ] ; } param A param B export function sub ( A , B ) { return [ A [ 0 ] - B [ 0 ] , A [ 1 ] - B [ 1 ] ] ; } param A param B export function vec ( A , B ) { return [ B [ 0 ] - A [ 0 ] , B [ 1 ] - A [ 1 ] ] ; } param A param n export function mul ( A , n ) { return [ A [ 0 ] * n , A [ 1 ] * n ] ; } export function mulV ( A , B ) { return [ A [ 0 ] * B [ 0 ] , A [ 1 ] * B [ 1 ] ] ; } param A param n export function div ( A , n ) { return [ A [ 0 ] / n , A [ 1 ] / n ] ; } param A param n export function divV ( A , B ) { return [ A [ 0 ] / B [ 0 ] , A [ 1 ] / B [ 1 ] ] ; } param A export function per ( A ) { return [ A [ 1 ] , - A [ 0 ] ] ; } param A param B export function dpr ( A , B ) { return A [ 0 ] * B [ 0 ] + A [ 1 ] * B [ 1 ] ; } param A param B export function cpr ( A , B ) { return A [ 0 ] * B [ 1 ] - B [ 0 ] * A [ 1 ] ; } param A export function len2 ( A ) { return A [ 0 ] * A [ 0 ] + A [ 1 ] * A [ 1 ] ; } param A export function len ( A ) { return Math . hypot ( A [ 0 ] , A [ 1 ] ) ; } param A param B export function pry ( A , B ) { return dpr ( A , B ) / len ( B ) ; } param A export function uni ( A ) { return div ( A , len ( A ) ) ; } param A export function normalize ( A ) { return uni ( A ) ; } param A param B returns export function tangent ( A , B ) { return normalize ( sub ( A , B ) ) ; } param A param B export function dist2 ( A , B ) { return len2 ( sub ( A , B ) ) ; } param A param B export function dist ( A , B ) { return Math . hypot ( A [ 1 ] - B [ 1 ] , A [ 0 ] - B [ 0 ] ) ; } param A param B returns export function fastDist ( A , B ) { const V = [ B [ 0 ] - A [ 0 ] , B [ 1 ] - A [ 1 ] ] ; const aV = [ Math . abs ( V [ 0 ] ) , Math . abs ( V [ 1 ] ) ] ; let r = 1 / Math . max ( aV [ 0 ] , aV [ 1 ] ) ; r = r * ( 1.29289 - ( aV [ 0 ] + aV [ 1 ] ) * r * 0.29289 ) ; return [ V [ 0 ] * r , V [ 1 ] * r ] ; } param A param B export function ang ( A , B ) { return Math . atan2 ( cpr ( A , B ) , dpr ( A , B ) ) ; } param A param B export function angle ( A , B ) { return Math . atan2 ( B [ 1 ] - A [ 1 ] , B [ 0 ] - A [ 0 ] ) ; } param A param B export function med ( A , B ) { return mul ( add ( A , B ) , 0.5 ) ; } param A param r export function rot ( A , r ) { return [ A [ 0 ] * Math . cos ( r ) - A [ 1 ] * Math . sin ( r ) , A [ 0 ] * Math . sin ( r ) + A [ 1 ] * Math . cos ( r ) , ] ; } param A param C param r export function rotWith ( A , C , r ) { const s = Math . sin ( r ) ; const c = Math . cos ( r ) ; const px = A [ 0 ] - C [ 0 ] ; const py = A [ 1 ] - C [ 1 ] ; const nx = px * c - py * s ; const ny = px * s + py * c ; return [ nx + C [ 0 ] , ny + C [ 1 ] ] ; } param A param B export function isEqual ( A , B ) { return A [ 0 ] === B [ 0 ] && A [ 1 ] === B [ 1 ] ; } param A param B param t export function lrp ( A , B , t ) { return add ( A , mul ( vec ( A , B ) , t ) ) ; } param A param B param from param to param s export function int ( A , B , from , to , s = 1 ) { const t = ( clamp ( from , to ) - from ) / ( to - from ) ; return add ( mul ( A , 1 - t ) , mul ( B , s ) ) ; } param p1 param pc param p2 export function ang3 ( p1 , pc , p2 ) { const v1 = vec ( pc , p1 ) ; const v2 = vec ( pc , p2 ) ; return ang ( v1 , v2 ) ; } param A returns export function abs ( A ) { return [ Math . abs ( A [ 0 ] ) , Math . abs ( A [ 1 ] ) ] ; } export function rescale ( a , n ) { const l = len ( a ) ; return [ ( n * a [ 0 ] ) / l , ( n * a [ 1 ] ) / l ] ; } param p1 param pc param p2 export function isLeft ( p1 , pc , p2 ) { return ( pc [ 0 ] - p1 [ 0 ] ) * ( p2 [ 1 ] - p1 [ 1 ] ) - ( p2 [ 0 ] - p1 [ 0 ] ) * ( pc [ 1 ] - p1 [ 1 ] ) ; } export function clockwise ( p1 , pc , p2 ) { return isLeft ( p1 , pc , p2 ) > 0 ; } const rounds = [ 1 , 10 , 100 , 1000 ] ; export function round ( a , d = 2 ) { return [ Math . round ( a [ 0 ] * rounds [ d ] ) / rounds [ d ] , Math . round ( a [ 1 ] * rounds [ d ] ) / rounds [ d ] , ] ; } param A param B param P returns param A param B param P param clamp returns param A param u param P returns export function nearestPointOnLineThroughPoint ( A , u , P ) { return add ( A , mul ( u , pry ( sub ( P , A ) , u ) ) ) ; } param A param u param P returns export function distanceToLineThroughPoint ( A , u , P ) { return dist ( P , nearestPointOnLineThroughPoint ( A , u , P ) ) ; } param A param B param P param clamp returns export function nearestPointOnLineSegment ( A , B , P , clamp = true ) { const delta = sub ( B , A ) ; const length = len ( delta ) ; const u = div ( delta , length ) ; const pt = add ( A , mul ( u , pry ( sub ( P , A ) , u ) ) ) ; if ( clamp ) { const da = dist ( A , pt ) ; const db = dist ( B , pt ) ; if ( db < da && da > length ) return B ; if ( da < db && db > length ) return A ; } return pt ; } param A param B param P param clamp returns export function distanceToLineSegment ( A , B , P , clamp = true ) { return dist ( P , nearestPointOnLineSegment ( A , B , P , clamp ) ) ; } param A param B param d returns export function nudge ( A , B , d ) { return add ( A , mul ( uni ( vec ( A , B ) ) , d ) ) ; }