simplifySection method

void simplifySection(
  1. int i,
  2. int j,
  3. int depth
)

Implementation

void simplifySection(int i, int j, int depth) {
  depth += 1;
  List<int> sectionIndex = List.filled(2, 0);
  if ((i + 1) == j) {
    LineSegment newSeg = line.getSegment(i);
    line.addToResult(newSeg);
    // leave this segment in the input index, for efficiency
    return;
  }

  bool isValidToSimplify = true;

  ///
  /// Following logic ensures that there is enough points in the output line.
  /// If there is already more points than the minimum, there's nothing to check.
  /// Otherwise, if in the worst case there wouldn't be enough points,
  /// don't flatten this segment (which avoids the worst case scenario)
  ////
  if (line.getResultSize() < line.getMinimumSize()) {
    int worstCaseSize = depth + 1;
    if (worstCaseSize < line.getMinimumSize()) isValidToSimplify = false;
  }

  List<double> distance = List.empty(growable: true);
  int furthestPtIndex = findFurthestPoint(linePts, i, j, distance);
  // flattening must be less than distanceTolerance
  if (distance[0] > distanceTolerance) isValidToSimplify = false;
  // test if flattened section would cause intersection
  LineSegment candidateSeg = LineSegment.empty();
  candidateSeg.p0 = linePts[i];
  candidateSeg.p1 = linePts[j];
  sectionIndex[0]=i;
  sectionIndex[1]=j;
  if (hasBadIntersection(line, sectionIndex, candidateSeg)) {
    isValidToSimplify = false;
  }

  if (isValidToSimplify) {
    LineSegment newSeg = flatten(i, j);
    line.addToResult(newSeg);
    return;
  }
  simplifySection(i, furthestPtIndex, depth);
  simplifySection(furthestPtIndex, j, depth);
}