toCubicBezier method

CubicBezier toCubicBezier()

Returns a CubicBezier instance with the same start and end points as this and control points positioned so it produces identical points along the curve as this.

Implementation

CubicBezier toCubicBezier() {
  final cubicCurvePoints = <Vector2>[];
  cubicCurvePoints.add(startPoint);

  final pointsCount = points.length;

  for (var index = 1; index < pointsCount; index++) {
    final currentPoint = points[index];
    final previousPoint = points[index - 1];
    final raisedPoint = Vector2.zero();
    raisedPoint.addScaled(currentPoint, (pointsCount - index) / pointsCount);
    raisedPoint.addScaled(previousPoint, index / pointsCount);

    cubicCurvePoints.add(raisedPoint);
  }

  cubicCurvePoints.add(endPoint);

  return CubicBezier(cubicCurvePoints);
}