import { Vector } from '../math/vector' ; import { minus } from '../math/minus' ; const check = ( threshold , startI , endI , paths ) => { let max = 0 ; let maxI = 0 ; const baseVector = new Vector ( minus ( paths [ endI ] , paths [ startI ] ) , minus ( paths [ endI + 1 ] , paths [ startI + 1 ] ) ) ; for ( let i = startI + 2 ; i < endI ; i += 2 ) { const vectorToStart = new Vector ( minus ( paths [ i ] , paths [ startI ] ) , minus ( paths [ i + 1 ] , paths [ startI + 1 ] ) ) ; const vectorToEnd = new Vector ( minus ( paths [ i ] , paths [ endI ] ) , minus ( paths [ i + 1 ] , paths [ endI + 1 ] ) ) ; let distance = 0 ; if ( ! vectorToStart . isZero && ! vectorToEnd . isZero ) { const prjToStart = Vector . projected ( vectorToStart , baseVector ) ; const prjToEnd = Vector . projected ( vectorToEnd , baseVector ) ; if ( prjToStart . modulo > baseVector . modulo ) { distance = prjToStart . modulo ; } else if ( prjToEnd . modulo > baseVector . modulo ) { distance = prjToEnd . modulo ; } else { distance = Vector . plumb ( vectorToStart , baseVector ) . modulo ; } if ( distance > max ) { max = distance ; maxI = i ; } } } if ( max <= threshold ) { paths . splice ( startI + 2 , endI - startI - 2 ) ; } else { if ( maxI < endI - 2 ) { check ( threshold , maxI , endI , paths ) ; } if ( maxI > startI + 2 ) { check ( threshold , startI , maxI , paths ) ; } } } ; export const douglasPeucker = ( threshold , pathArr ) => { const pathCopy = pathArr . slice ( ) ; check ( threshold , 0 , pathCopy . length - 2 , pathCopy ) ; return pathCopy ; } ;