scaleToFit static method

List<GeoCoordinate2D> scaleToFit(
  1. Size size,
  2. BoxFit boxFit,
  3. List<GeoCoordinate2D> coords, [
  4. bool bRecenter = false,
  5. bool bInPlace = false,
])

Adjust coordinates coords within rectangle defined by dimensions size such that it meets boxFit criteria. If bInPlace is true, the coords array values will be updated, otherwise a new coordinate array will be returned.

Implementation

static List<GeoCoordinate2D> scaleToFit(
    Size size, BoxFit boxFit, List<GeoCoordinate2D> coords,
    [bool bRecenter = false, bool bInPlace = false]) {
  double sizeX = size.width / 2;
  double sizeY = size.height / 2;
  double minX = double.infinity,
      maxX = double.negativeInfinity,
      minY = double.infinity,
      maxY = double.negativeInfinity;
  for (int i = 0; i < coords.length; i++) {
    GeoCoordinate2D coord = coords[i];
    if (coord.x < minX) minX = coord.x;
    if (coord.x > maxX) maxX = coord.x;
    if (coord.y < minY) minY = coord.y;
    if (coord.y > maxY) maxY = coord.y;
  }
  if (bRecenter) {
    GeoCoordinate2D newOrigin =
        GeoCoordinate2D((maxX + minX) / 2, (maxY + minY) / 2);
    coords = neworigin(newOrigin, coords, bInPlace);
    // readjust minX/Y, an maxX/Y
    maxX = maxX - newOrigin.x;
    maxY = maxY - newOrigin.y;
    minX = minX - newOrigin.x;
    minY = minY - newOrigin.y;
  }

  double scaleX = 1.0, scaleY = 1.0;
  if (boxFit == BoxFit.fitWidth) {
    scaleX = sizeX / max(maxX.abs(), minX.abs());
  } else if (boxFit == BoxFit.fitHeight) {
    scaleY = sizeY / max(maxY.abs(), minY.abs());
  } else {
    scaleX = sizeX / max(maxX.abs(), minX.abs());
    scaleY = sizeY / max(maxY.abs(), minY.abs());
  }

  List<GeoCoordinate2D> retcoords = bInPlace ? coords : <GeoCoordinate2D>[];
  if (bInPlace) {
    for (GeoCoordinate2D coord in coords) {
      coord.x *= scaleX;
      coord.y *= scaleY;
    }
  } else {
    for (GeoCoordinate2D coord in coords) {
      retcoords.add(GeoCoordinate2D(coord.x * scaleX, coord.y * scaleY));
    }
  }
  return retcoords;
}