reframe static method

ShapeElement reframe(
  1. ShapeElement source,
  2. String key,
  3. DiagramVectorPath path
)

Recompute a positive frame while preserving every local/world point, including the pivot shift of a rotated path and its owner-relative frame. The path is stored in source's value key.

Implementation

static ShapeElement reframe(
  ShapeElement source,
  String key,
  DiagramVectorPath path,
) {
  path.validate();
  final f = source.frame;
  DiagramPoint position(DiagramPoint p) =>
      DiagramPoint(f.left + f.width * p.x, f.top + f.height * p.y);
  final hull = [
    for (final n in path.nodes) ...[
      position(n.position),
      position(n.position + n.incoming),
      position(n.position + n.outgoing),
    ],
  ];
  final extent = DiagramRect.fromPoints(hull);
  final left = extent.left, top = extent.top;
  final width = math.max(4.0, extent.width);
  final height = math.max(4.0, extent.height);
  final local = DiagramRect.fromLTWH(left, top, width, height);
  final center = local.center.rotateAround(f.center, source.rotation);
  final frame = DiagramRect.fromLTWH(
    center.x - width / 2,
    center.y - height / 2,
    width,
    height,
  );
  final reframed = path.copyWith(
    nodes: [
      for (final n in path.nodes)
        n.copyWith(
          position: DiagramPoint(
            (position(n.position).x - left) / width,
            (position(n.position).y - top) / height,
          ),
          incoming: DiagramPoint(
            n.incoming.x * f.width / width,
            n.incoming.y * f.height / height,
          ),
          outgoing: DiagramPoint(
            n.outgoing.x * f.width / width,
            n.outgoing.y * f.height / height,
          ),
        ),
    ],
  );
  return source.copyWith(
    frame: frame,
    values: {...source.values, key: reframed.toValue()},
  );
}