modules/polygon/src/lineclip.js
// handle polylines rather than just segments
handle polylines rather than just segments
handle polylines rather than just segments
function clipPolyline(positions, bbox, options = {}) {
  const {size = 2, startIndex = 0, endIndex = positions.length} = options;
  const numPoints = (endIndex - startIndex) / size;
  const result = [];
  let part = [];
  let a;
  let b;
  let codeA = -1;
  let codeB;
  let lastCode;
  for (let i = 1; i < numPoints; i++) {
    a = getPointAtIndex(positions, i - 1, size, startIndex, a);
    b = getPointAtIndex(positions, i, size, startIndex, b);
    if (codeA < 0) {
      codeA = bitCode(a, bbox);
    }
    codeB = lastCode = bitCode(b, bbox);
    while (true) {
      if (!(codeA | codeB)) {
        push(part, a);
        if (codeB !== lastCode) {
          push(part, b);
          if (i < numPoints - 1) {
            result.push(part);
            part = [];
          }
        } else if (i === numPoints - 1) {
          push(part, b);
        }
        break;
      } else if (codeA & codeB) {
        break;
      } else if (codeA) {
        intersect(a, b, codeA, bbox, a);
        codeA = bitCode(a, bbox);
      } else {
        intersect(a, b, codeB, bbox, b);
        codeB = bitCode(b, bbox);
      }
    }
    codeA = lastCode;
  }
  if (part.length) result.push(part);
  return result;
}
function clipPolyline ( positions , bbox , options = { } ) { const { size = 2 , startIndex = 0 , endIndex = positions . length } = options ; const numPoints = ( endIndex - startIndex ) / size ; const result = [ ] ; let part = [ ] ; let a ; let b ; let codeA = - 1 ; let codeB ; let lastCode ; for ( let i = 1 ; i < numPoints ; i ++ ) { a = getPointAtIndex ( positions , i - 1 , size , startIndex , a ) ; b = getPointAtIndex ( positions , i , size , startIndex , b ) ; if ( codeA < 0 ) { codeA = bitCode ( a , bbox ) ; } codeB = lastCode = bitCode ( b , bbox ) ; while ( true ) { if ( ! ( codeA | codeB ) ) { push ( part , a ) ; if ( codeB !== lastCode ) { push ( part , b ) ; if ( i < numPoints - 1 ) { result . push ( part ) ; part = [ ] ; } } else if ( i === numPoints - 1 ) { push ( part , b ) ; } break ; } else if ( codeA & codeB ) { break ; } else if ( codeA ) { intersect ( a , b , codeA , bbox , a ) ; codeA = bitCode ( a , bbox ) ; } else { intersect ( a , b , codeB , bbox , b ) ; codeB = bitCode ( b , bbox ) ; } } codeA = lastCode ; } if ( part . length ) result . push ( part ) ; return result ; }
handle polylines rather than just segments
handle polylines rather than just segments
// eslint-disable-next-line no-constant-condition // accept // segment went outside // start a new line // trivial reject // a outside, intersect with clip edge // b outside