removeDuplicatedPoints method

List removeDuplicatedPoints(
  1. List points
)

Implementation

List removeDuplicatedPoints(List points) {
  // Creates a new array if necessary with duplicated points removed.
  // This does not remove duplicated initial and ending points of a closed path.

  bool dupPoints = false;
  for (int i = 1, n = points.length - 1; i < n; i++) {
    if (points[i].distanceTo(points[i + 1]) < minDistance) {
      dupPoints = true;
      break;
    }
  }

  if (!dupPoints) return points;

  List newPoints = [];
  newPoints.add(points[0]);

  for (int i = 1, n = points.length - 1; i < n; i++) {
    if (points[i].distanceTo(points[i + 1]) >= minDistance) {
      newPoints.add(points[i]);
    }
  }

  newPoints.add(points[points.length - 1]);

  return newPoints;
}