import type { Predicate2 } from "@thi.ng/api" ; import { BitField , defBitField } from "@thi.ng/bitfield/bitfield" ; remarks example param pred param x param y param width param height export function * floodFill ( pred , x , y , width , height ) { x |= 0 ; y |= 0 ; if ( ! pred ( x , y ) ) return ; const queue = [ [ x , y ] ] ; const visited = defBitField ( width * height ) ; height -- ; while ( queue . length ) { [ x , y ] = queue . pop ( ) ! ; yield * partialRow ( pred , queue , visited , x , y , width , height , - 1 ) ; yield * partialRow ( pred , queue , visited , x + 1 , y , width , height , 1 ) ; } } internal function * partialRow ( pred , queue , visited , x , y , width , height1 , step ) { let idx = y * width + x ; if ( visited . at ( idx ) ) return ; let scanUp = false ; let scanDown = false ; while ( x >= 0 && x < width && pred ( x , y ) ) { visited . setAt ( idx ) ; yield [ x , y ] ; if ( y > 0 ) { if ( pred ( x , y - 1 ) && ! scanUp ) { queue . push ( [ x , y - 1 ] ) ; scanUp = true ; } else { scanUp = false ; } } if ( y < height1 ) { if ( pred ( x , y + 1 ) && ! scanDown ) { queue . push ( [ x , y + 1 ] ) ; scanDown = true ; } else { scanDown = false ; } } x += step ; idx += step ; } }