resolve static method

DiagramRect resolve({
  1. required DiagramRect bounds,
  2. required DiagramPoint pointer,
  3. required double axisX,
  4. required double axisY,
  5. required DiagramSize minimumSize,
  6. bool preserveAspectRatio = false,
  7. bool fromCenter = false,
})

Implementation

static DiagramRect resolve({
  required DiagramRect bounds,
  required DiagramPoint pointer,
  required double axisX,
  required double axisY,
  required DiagramSize minimumSize,
  bool preserveAspectRatio = false,
  bool fromCenter = false,
}) {
  if (![
        bounds.left,
        bounds.top,
        bounds.right,
        bounds.bottom,
        bounds.width,
        bounds.height,
        pointer.x,
        pointer.y,
        minimumSize.width,
        minimumSize.height,
      ].every((v) => v.isFinite) ||
      bounds.width <= 0 ||
      bounds.height <= 0 ||
      minimumSize.width <= 0 ||
      minimumSize.height <= 0 ||
      !const [-1.0, 0.0, 1.0].contains(axisX) ||
      !const [-1.0, 0.0, 1.0].contains(axisY) ||
      (axisX == 0 && axisY == 0)) {
    throw ArgumentError(
      'Resize requires finite positive bounds and a resize axis',
    );
  }
  final anchor = DiagramPoint(
    fromCenter || axisX == 0
        ? bounds.center.x
        : axisX < 0
        ? bounds.right
        : bounds.left,
    fromCenter || axisY == 0
        ? bounds.center.y
        : axisY < 0
        ? bounds.bottom
        : bounds.top,
  );
  final multiplier = fromCenter ? 2.0 : 1.0;
  var width = axisX == 0
      ? bounds.width
      : (pointer.x - anchor.x) * axisX * multiplier;
  var height = axisY == 0
      ? bounds.height
      : (pointer.y - anchor.y) * axisY * multiplier;
  if (preserveAspectRatio) {
    final scale = math.max(
      axisX == 0
          ? height / bounds.height
          : axisY == 0
          ? width / bounds.width
          : math.max(width / bounds.width, height / bounds.height),
      math.max(
        minimumSize.width / bounds.width,
        minimumSize.height / bounds.height,
      ),
    );
    width = bounds.width * scale;
    height = bounds.height * scale;
  } else {
    if (axisX != 0) width = math.max(width, minimumSize.width);
    if (axisY != 0) height = math.max(height, minimumSize.height);
  }
  final left = fromCenter || axisX == 0
      ? anchor.x - width / 2
      : axisX < 0
      ? anchor.x - width
      : anchor.x;
  final top = fromCenter || axisY == 0
      ? anchor.y - height / 2
      : axisY < 0
      ? anchor.y - height
      : anchor.y;
  final right = left + width;
  final bottom = top + height;
  if (![left, right, top, bottom, width, height].every((v) => v.isFinite)) {
    throw ArgumentError('Resize exceeds finite geometry');
  }
  return DiagramRect.fromLTWH(left, top, width, height);
}