modules/polygon/src/lineclip.js
// polygon must be closed (first vertex == last vertex)
polygon must be closed (first vertex == last vertex)
polygon must be closed ( first vertex == last vertex )
function clipPolygon(positions, bbox, options = {}) {
  const {size = 2, endIndex = positions.length} = options;
  let {startIndex = 0} = options;
  let numPoints = (endIndex - startIndex) / size;
  let result;
  let p;
  let prev;
  let inside;
  let prevInside;
  for (let edge = 1; edge <= 8; edge *= 2) {
    result = [];
    prev = getPointAtIndex(positions, numPoints - 1, size, startIndex, prev);
    prevInside = !(bitCode(prev, bbox) & edge);
    for (let i = 0; i < numPoints; i++) {
      p = getPointAtIndex(positions, i, size, startIndex, p);
      inside = !(bitCode(p, bbox) & edge);
      if (inside !== prevInside) push(result, intersect(prev, p, edge, bbox));
      if (inside) push(result, p); 
      copy(prev, p);
      prevInside = inside;
    }
    positions = result;
    startIndex = 0;
    numPoints = result.length / size;
    if (!numPoints) break;
  }
  return result;
}
function clipPolygon ( positions , bbox , options = { } ) { const { size = 2 , endIndex = positions . length } = options ; let { startIndex = 0 } = options ; let numPoints = ( endIndex - startIndex ) / size ; let result ; let p ; let prev ; let inside ; let prevInside ; for ( let edge = 1 ; edge <= 8 ; edge *= 2 ) { result = [ ] ; prev = getPointAtIndex ( positions , numPoints - 1 , size , startIndex , prev ) ; prevInside = ! ( bitCode ( prev , bbox ) & edge ) ; for ( let i = 0 ; i < numPoints ; i ++ ) { p = getPointAtIndex ( positions , i , size , startIndex , p ) ; inside = ! ( bitCode ( p , bbox ) & edge ) ; if ( inside !== prevInside ) push ( result , intersect ( prev , p , edge , bbox ) ) ; if ( inside ) push ( result , p ) ; copy ( prev , p ) ; prevInside = inside ; } positions = result ; startIndex = 0 ; numPoints = result . length / size ; if ( ! numPoints ) break ; } return result ; }
polygon must be closed (first vertex == last vertex)
polygon must be closed ( first vertex == last vertex )
// clip against each side of the clip rectangle // if segment goes through the clip window, add an intersection // add a point if it's inside // close loop