resolveShapePath function

ResolvedOutline resolveShapePath(
  1. DiagramRect frame,
  2. DiagramVectorPath localPath, {
  3. double cornerRadius = 0,
})

Resolves localPath, in shape-local units from the frame origin, at frame: a closed, straight-edged path is a PolygonOutline, so connectors meet its true edges; any other path is a VectorPathOutline that keeps its nodes.

Implementation

ResolvedOutline resolveShapePath(
  DiagramRect frame,
  DiagramVectorPath localPath, {
  double cornerRadius = 0,
}) {
  final path = localPath;
  if (!path.closed ||
      path.nodes.any((node) => node.segment != DiagramPathSegmentKind.line)) {
    return VectorPathOutline.local(frame, path, cornerRadius: cornerRadius);
  }
  path.validate();
  final corners = [
    for (final node in path.nodes)
      DiagramPoint(frame.left + node.position.x, frame.top + node.position.y),
  ];
  if (corners.any((point) => !point.x.isFinite || !point.y.isFinite)) {
    throw StateError('Resolved path coordinates must remain finite');
  }
  final contour = _roundPathCorners(
    [
      for (var i = 0; i < corners.length; i++)
        [corners[i], corners[(i + 1) % corners.length]],
    ],
    closed: true,
    radius: cornerRadius,
    corners: [
      for (final node in path.nodes) node.mode == DiagramPathHandleMode.corner,
    ],
  );
  final points = contour.sublist(0, contour.length - 1);
  final inside = points.every(
    (p) =>
        p.x >= frame.left &&
        p.x <= frame.right &&
        p.y >= frame.top &&
        p.y <= frame.bottom,
  );
  return PolygonOutline(
    inside ? frame : DiagramRect.fromPoints([...frame.corners, ...points]),
    points,
  );
}