function alignClockwise(contours, direction) {
  let numReversed = 0;
  for (let i = 0; i < contours.length; i++) {
    let contour = contours[i];
    let restContours = contours.slice(0);
    restContours.splice(i, 1);
    if (contour.length === 0) continue;
    let isInside = insidePath(contour[0], restContours) &&
      insidePath(contour[Math.ceil(contour.length / 2)], restContours);
    let dir = isInside ? (!direction) : direction;
    if (isClockwise(contour) != dir) {
      contours[i] = reverseContour(contour);
      numReversed++;
    }
  }
  return numReversed;
}
/**
 * Align all contours' clockwiseness.
 * 
 * @param {Array} contours  Array of contour 
 * @param {boolean} direction  true for clockwise, false for counter-clockwise
 */
Align all contours' clockwiseness.

@param {Array} contours  Array of contour
@param {boolean} direction  true for clockwise, false for counter-clockwise
Align all contours ' clockwiseness . @param { Array } contours Array of contour @param { boolean } direction true for clockwise false for counter - clockwise
function alignClockwise(contours, direction) {
  let numReversed = 0;
  for (let i = 0; i < contours.length; i++) {
    let contour = contours[i];
    let restContours = contours.slice(0);
    restContours.splice(i, 1);
    if (contour.length === 0) continue;
    let isInside = insidePath(contour[0], restContours) &&
      insidePath(contour[Math.ceil(contour.length / 2)], restContours);
    let dir = isInside ? (!direction) : direction;
    if (isClockwise(contour) != dir) {
      contours[i] = reverseContour(contour);
      numReversed++;
    }
  }
  return numReversed;
}
function alignClockwise ( contours , direction ) { let numReversed = 0 ; for ( let i = 0 ; i < contours . length ; i ++ ) { let contour = contours [ i ] ; let restContours = contours . slice ( 0 ) ; restContours . splice ( i , 1 ) ; if ( contour . length === 0 ) continue ; let isInside = insidePath ( contour [ 0 ] , restContours ) && insidePath ( contour [ Math . ceil ( contour . length / 2 ) ] , restContours ) ; let dir = isInside ? ( ! direction ) : direction ; if ( isClockwise ( contour ) != dir ) { contours [ i ] = reverseContour ( contour ) ; numReversed ++ ; } } return numReversed ; }
Align all contours' clockwiseness.
Align all contours ' clockwiseness .