project method

ResolvedPathSample project(
  1. DiagramPoint point
)

Closest arc-length anchor for direct label placement. Ties resolve toward the start, giving crossings and retraced segments a deterministic owner.

Implementation

ResolvedPathSample project(DiagramPoint point) {
  if (!point.x.isFinite || !point.y.isFinite) {
    throw ArgumentError.value(point);
  }
  if (length == 0) return sample(0);
  var bestDistance = double.infinity;
  var fraction = 0.0;
  for (var index = 1; index < points.length; index++) {
    final delta = points[index] - points[index - 1];
    final offset = point - points[index - 1];
    final squaredLength = delta.x * delta.x + delta.y * delta.y;
    final unit = ((offset.x * delta.x + offset.y * delta.y) / squaredLength)
        .clamp(0.0, 1.0);
    final candidate = points[index - 1] + delta * unit;
    final distance = (point - candidate).magnitude;
    if (distance < bestDistance) {
      bestDistance = distance;
      fraction =
          (_distances[index - 1] +
              (_distances[index] - _distances[index - 1]) * unit) /
          length;
    }
  }
  return sample(fraction.clamp(0.0, 1.0));
}