chaikin method

SPath chaikin({
  1. int n = 1,
  2. bool looped = false,
})

A way to smooth out a path of lines; after just a few applications it will look like a nice curve. NB this converts the SPath to lines i.e. detail in any existing curves will be lost If the path forms a look you probably want to opt in to the looped option.

Implementation

SPath chaikin({int n = 1, bool looped = false}) {
  var pts = points;
  if (pts.length < 3) {
    throw Exception("Must have at least 3 points to perform this");
  }
  List<Point<double>> newPts = [];
  for (var i = 0; i < n; i++) {
    if (!looped) {
      newPts.add(pts[0]);
    }
    final m = pts.length - 2;
    for (var j = 0; j < m; j++) {
      final a = pts[j];
      final b = pts[j + 1];
      final c = pts[j + 2];

      newPts.add(b.pointTowards(a, proportion: 0.25));
      newPts.add(b.pointTowards(c, proportion: 0.25));
    }

    if (!looped) {
      newPts.add(pts.last);
    }

    pts = newPts;
    newPts = [];
  }
  // In the JS version I did some extra slice thing at end... this was very fiddly, not doing yet
  return SPath.fromPoints(pts);
}