findChainEnd static method

int findChainEnd(
  1. List<Coordinate> pts,
  2. int start
)

Finds the index of the last point in a monotone chain starting at a given point. Repeated points (0-length segments) are included in the monotone chain returned.

@param pts the points to scan @param start the index of the start of this chain @return the index of the last point in the monotone chain starting at start.

Implementation

static int findChainEnd(List<Coordinate> pts, int start) {
  int safeStart = start;
  // skip any zero-length segments at the start of the sequence
  // (since they cannot be used to establish a quadrant)
  while (safeStart < pts.length - 1 &&
      pts[safeStart].equals2D(pts[safeStart + 1])) {
    safeStart++;
  }
  // check if there are NO non-zero-length segments
  if (safeStart >= pts.length - 1) {
    return pts.length - 1;
  }
  // determine overall quadrant for chain (which is the starting quadrant)
  int chainQuad =
      Quadrant.quadrantFromCoords(pts[safeStart], pts[safeStart + 1]);
  int last = start + 1;
  while (last < pts.length) {
    // skip zero-length segments, but include them in the chain
    if (!pts[last - 1].equals2D(pts[last])) {
      // compute quadrant for next possible segment in chain
      int quad = Quadrant.quadrantFromCoords(pts[last - 1], pts[last]);
      if (quad != chainQuad) break;
    }
    last++;
  }
  return last - 1;
}