 ! points at ymin and ymax at the gap bounds.
 : drawable-chunks ( data ymin,ymax -- chunks )
     first2 [
-        '[ [ second _ _ between<=> ] bi@ = ]
+        '[ [ y _ _ between<=> ] bi@ = ]
         monotonic-split-slice
     ] 2keep (drawable-chunks) ;
 

: min-max-cut ( min,max pairs -- seq )
    [ first2 ] dip right-cut left-cut ;

: clip-by-first ( min,max pairs -- pairs' )
    2dup first-in-bounds? [
        [ dup first ] dip [ search-first? not ] keep
        adjusted-tail-slice
        [ dup second ] dip [ search-first? not ] keep

! Return a slice of the seq with all elements equal to elt to the
! left of the index, plus one that's not equal, if requested.
:: adjusted-tail-slice ( n elt plus-one? seq -- slice )
    n seq elt x '[ x _ = not ] find-last-from drop seq swap
    [ plus-one? [ 1 + ] unless tail-slice ] when* ;

! Return a slice of the seq with all elements equal to elt to the
! right of the index, plus one that's not equal, if requested.
:: adjusted-head-slice ( n elt plus-one? seq -- slice )
    n seq elt x '[ x _ = not ] find-from drop seq swap
    [ plus-one? [ 1 + ] when short head-slice ] when* ;

! : data-rect ( data -- rect )
!    [ [ first x ] [ last x ] bi ] keep
!    [ y ] map minmax swapd
!    [ 2array ] bi@ <extent-rect> ;

: x-in-bounds? ( min,max pairs -- ? )
    {
        [ [ first ] dip last x > not ]
        [ [ second ] dip first x < not ]
    } 2&& ;

: y-in-bounds? ( min,max pairs -- ? )
    [ y ] map minmax 2array
    {
        [ [ first ] dip second > not ]
        [ [ second ] dip first < not ]
    } 2&& ;

! : xy-in-bounds? ( bounds pairs -- ? )
!    {
!        [ [ first ] dip x-in-bounds? ]
!        [ [ second ] dip y-in-bounds? ]
!    } 2&& ;

: calc-line-slope ( point1 point2 -- slope ) v- first2 swap / ;
