calculatePointAtPosition method

Offset? calculatePointAtPosition(
  1. Path path,
  2. double position
)

Calculates the position along a path for inserting new control points.

This helper method finds the point on the connection path at the given position (0.0 to 1.0), which is useful for adding control points at specific locations along the path.

Returns null if the position cannot be calculated.

Implementation

Offset? calculatePointAtPosition(Path path, double position) {
  final metrics = path.computeMetrics().toList();
  if (metrics.isEmpty) return null;

  // Get total length across all contours
  final totalLength = metrics.fold<double>(
    0,
    (sum, metric) => sum + metric.length,
  );

  if (totalLength == 0) return null;

  // Find the position along the total length
  final targetDistance = totalLength * position;

  // Find which contour contains this distance
  var currentDistance = 0.0;
  for (final metric in metrics) {
    if (currentDistance + metric.length >= targetDistance) {
      // Found the right contour
      final localDistance = targetDistance - currentDistance;
      final tangent = metric.getTangentForOffset(localDistance);
      return tangent?.position;
    }
    currentDistance += metric.length;
  }

  return null;
}