softCP static method
Offset
softCP(
- OffsetPoint current, {
- OffsetPoint? previous,
- OffsetPoint? next,
- bool reverse = false,
- double smoothing = 0.65,
Calculates a 'soft' control point for a current
point based on its previous
and next
neighbors.
This is used to create smooth curves.
current
The current OffsetPoint for which to calculate the control point.
previous
The preceding OffsetPoint in the path.
next
The succeeding OffsetPoint in the path.
reverse
If true, calculates the control point in reverse direction.
smoothing
A factor (0.0 to 1.0) controlling the smoothness of the curve.
Returns the calculated soft control point as an Offset.
Implementation
static Offset softCP(OffsetPoint current,
{OffsetPoint? previous,
OffsetPoint? next,
bool reverse = false,
double smoothing = 0.65}) {
assert(smoothing >= 0.0 && smoothing <= 1.0);
previous ??= current;
next ??= current;
final sharpness = 1.0 - smoothing;
final dist1 = previous.distanceTo(current);
final dist2 = current.distanceTo(next);
final dist = dist1 + dist2;
final dir1 = current.directionTo(next);
final dir2 = current.directionTo(previous);
final dir3 =
reverse ? next.directionTo(previous) : previous.directionTo(next);
final velocity =
(dist * 0.3 / (next.timestamp - previous.timestamp)).clamp(0.5, 3.0);
final ratio = (dist * velocity * smoothing)
.clamp(0.0, (reverse ? dist2 : dist1) * 0.5);
final dir =
((reverse ? dir2 : dir1) * sharpness) + (dir3 * smoothing) * ratio;
final x = current.dx + dir.dx;
final y = current.dy + dir.dy;
return Offset(x, y);
}