smooth method

List? smooth(
  1. int lookAhead,
  2. double slide
)

Implementation

List<dynamic>? smooth(int lookAhead, double slide) {
  double sc;
  List<dynamic> res = [];

  int n = xyList.length; // Points->n_points;
  int half = lookAhead ~/ 2;

  for (int j = 0; j < n; j++) {
    List<double> tmp = List.filled(2, 0.0);
    res.add(tmp);
  }

  if (lookAhead % 2 == 0) {
    throw ArgumentError(
        "Look ahead parameter must be odd, but you supplied: $lookAhead");
  }

  if (lookAhead >= n || lookAhead == 1) {
    return null;
  }

  sc = 1.0 / lookAhead;

  List<double> pCoord = List.filled(2, 0.0);
  List<double> sCoord = List.filled(2, 0.0);
  pointAssign(xyList, 0, pCoord);
  for (int i = 1; i < lookAhead; i++) {
    List<double> tmpCoord = List.filled(2, 0.0);
    pointAssign(xyList, i, tmpCoord);
    pointAdd(pCoord, tmpCoord, pCoord);
  }

  /* and calculate the average of remaining points */
  for (int i = half; i + half < n; i++) {
    List<double> tmpCoord = List.filled(2, 0.0);
    pointAssign(xyList, i, sCoord);
    pointScalar(sCoord, 1.0 - slide, sCoord);
    pointScalar(pCoord, sc * slide, tmpCoord);
    pointAdd(tmpCoord, sCoord, res[i]);
    if (i + half + 1 < n) {
      pointAssign(xyList, i - half, tmpCoord);
      pointSubtract(pCoord, tmpCoord, pCoord);
      pointAssign(xyList, i + half + 1, tmpCoord);
      pointAdd(pCoord, tmpCoord, pCoord);
    }
  }

  for (int i = 0; i < half; i++) {
    res[i][0] = xyList[i][0];
    res[i][1] = xyList[i][1];
  }
  for (int i = n - half - 1; i < n; i++) {
    res[i][0] = xyList[i][0];
    res[i][1] = xyList[i][1];
  }
  // for( Coordinate coordinate : res ) {
  //     if (coordinate.x == 0) {
  //         System.out.println();
  //     }
  // }
  // for( i = half; i + half < n; i++ ) {
  // coordinates[i].x = res.get(i).x;
  // coordinates[i].y = res.get(i).y;
  // coordinates[i].z = res.get(i).z;
  // }

  // return Points->n_points;

  return res;
}