earcutLinked function

dynamic earcutLinked(
  1. Node? ear,
  2. dynamic triangles,
  3. dynamic dim,
  4. dynamic minX,
  5. dynamic minY,
  6. num? invSize,
  7. num? pass,
)

Implementation

earcutLinked(Node? ear, triangles, dim, minX, minY, num? invSize, num? pass) {
  if (ear == null) return;

  // interlink polygon nodes in z-order
  if (pass == null && invSize != null) indexCurve(ear, minX, minY, invSize);

  var stop = ear, prev, next;

  // iterate through ears, slicing them one by one
  while (ear!.prev != ear.next) {
    prev = ear.prev;
    next = ear.next;

    if (invSize != null ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)) {
      // cut off the triangle
      triangles.add(prev.i / dim);
      triangles.add(ear.i / dim);
      triangles.add(next.i / dim);

      removeNode(ear);

      // skipping the next vertex leads to less sliver triangles
      ear = next.next;
      stop = next.next;

      continue;
    }

    ear = next;

    // if we looped through the whole remaining polygon and can't find any more ears
    if (ear == stop) {
      // try filtering points and slicing again
      if (pass == null || pass == 0) {
        earcutLinked(
            filterPoints(ear, null), triangles, dim, minX, minY, invSize, 1);

        // if this didn't work, try curing all small self-intersections locally

      } else if (pass == 1) {
        ear = cureLocalIntersections(filterPoints(ear, null), triangles, dim);
        earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);

        // as a last resort, try splitting the remaining polygon into two

      } else if (pass == 2) {
        splitEarcut(ear, triangles, dim, minX, minY, invSize);
      }

      break;
    }
  }
}