convertAlignmentToPosition function

Vec convertAlignmentToPosition({
  1. required double parentWidth,
  2. required double parentHeight,
  3. required double childWidth,
  4. required double childHeight,
  5. required double alignmentX,
  6. required double alignmentY,
})

Implementation

Vec convertAlignmentToPosition({
  required double parentWidth,
  required double parentHeight,
  required double childWidth,
  required double childHeight,
  required double alignmentX,
  required double alignmentY,
}) {
  final double widthDelta = parentWidth - childWidth;
  final double heightDelta = parentHeight - childHeight;

  final bool isSameWidth = widthDelta.closeTo(0);
  final bool isSameHeight = heightDelta.closeTo(0);

  // When the size is the same as that of the [rect] size, we account for it
  // by using the [size] directly instead of a zero-delta value.
  // It does not matter whether we use [rect] or [size] since they
  // are the same.
  final double deltaX = (isSameWidth ? childWidth : widthDelta) / 2;
  final double deltaY = (isSameHeight ? childHeight : heightDelta) / 2;

  final double adjustedAlignmentX = alignmentX - (isSameWidth ? 1 : 0);
  final double adjustedAlignmentY = alignmentY - (isSameHeight ? 1 : 0);

  final double x = deltaX + adjustedAlignmentX * deltaX;
  final double y = deltaY + adjustedAlignmentY * deltaY;

  return Vec(
    double.parse(x.toStringAsFixed(3)),
    double.parse(y.toStringAsFixed(3)),
  );
}