cutAtProgress method

(ExpressiveMeasuredCubic, ExpressiveMeasuredCubic) cutAtProgress(
  1. double cutOutlineProgress
)

Cut this ExpressiveMeasuredCubic into two at the given outline progress value.

Implementation

(ExpressiveMeasuredCubic, ExpressiveMeasuredCubic) cutAtProgress(
  double cutOutlineProgress,
) {
  // Floating point errors further up can cause cutOutlineProgress to land
  // just slightly outside of the start/end progress for this cubic, so we
  // limit it to those bounds to avoid further errors later
  final boundedCutOutlineProgress = cutOutlineProgress.coerceIn(
    _startOutlineProgress,
    _endOutlineProgress,
  );
  final outlineProgressSize = _endOutlineProgress - _startOutlineProgress;
  final progressFromStart = boundedCutOutlineProgress - _startOutlineProgress;

  // Note that in earlier parts of the computation, we have empty
  // MeasuredCubics (cubics with progressSize == 0), but those cubics are
  // filtered out before this method is called.
  final relativeProgress = progressFromStart / outlineProgressSize;
  final t = measurer.findCubicCutPoint(
    cubic,
    relativeProgress * measuredSize,
  );

  if (t < 0 || t > 1) {
    throw ArgumentError(
      'ShapeCubic cut point is expected to be between 0 and 1.',
    );
  }

  // c1/c2 are the two new cubics, then we return MeasuredCubics created
  // from them.
  final (c1, c2) = cubic.split(t);
  return (
    ExpressiveMeasuredCubic(
      measurer: measurer,
      cubic: c1,
      startOutlineProgress: _startOutlineProgress,
      endOutlineProgress: boundedCutOutlineProgress,
    ),
    ExpressiveMeasuredCubic(
      measurer: measurer,
      cubic: c2,
      startOutlineProgress: boundedCutOutlineProgress,
      endOutlineProgress: _endOutlineProgress,
    ),
  );
}