split static method

DiagramVectorPath split(
  1. DiagramVectorPath p,
  2. String nodeId, {
  3. required String newNodeId,
  4. double t = .5,
})

Implementation

static DiagramVectorPath split(
  DiagramVectorPath p,
  String nodeId, {
  required String newNodeId,
  double t = .5,
}) {
  if (!t.isFinite || t <= 0 || t >= 1) {
    throw ArgumentError('Split position must be in (0, 1)');
  }
  final index = _index(p, nodeId);
  if (!p.closed && index == p.nodes.length - 1) {
    throw ArgumentError('No outgoing segment');
  }
  final next = (index + 1) % p.nodes.length;
  final a = p.nodes[index];
  final b = p.nodes[next];
  final nodes = [...p.nodes];
  DiagramPoint mix(DiagramPoint x, DiagramPoint y) => x * (1 - t) + y * t;
  DiagramPathNode added;
  if (a.segment == DiagramPathSegmentKind.line) {
    added = DiagramPathNode(
      id: newNodeId,
      position: mix(a.position, b.position),
    );
  } else {
    final ab = mix(a.position, a.position + a.outgoing);
    final bc = mix(a.position + a.outgoing, b.position + b.incoming);
    final cd = mix(b.position + b.incoming, b.position);
    final abc = mix(ab, bc);
    final bcd = mix(bc, cd);
    final point = mix(abc, bcd);
    nodes[index] = a.copyWith(
      outgoing: ab - a.position,
      mode: DiagramPathHandleMode.corner,
    );
    nodes[next] = b.copyWith(
      incoming: cd - b.position,
      mode: DiagramPathHandleMode.corner,
    );
    added = DiagramPathNode(
      id: newNodeId,
      position: point,
      incoming: abc - point,
      outgoing: bcd - point,
      mode: DiagramPathHandleMode.smooth,
      segment: DiagramPathSegmentKind.cubic,
    );
  }
  nodes.insert(index + 1, added);
  return p.copyWith(nodes: nodes);
}