calculateCoords function

List<Map<String, dynamic>> calculateCoords(
  1. int numCards,
  2. double arcRadius,
  3. double cardWidth,
  4. double cardHeight,
  5. String direction,
  6. double cardSpacing,
  7. Map<String, dynamic> box,
)

Implementation

List<Map<String, dynamic>> calculateCoords(
    int numCards,
    double arcRadius,
    double cardWidth,
    double cardHeight,
    String direction,
    double cardSpacing,
    Map<String, dynamic> box) {
  // The separation between the cards, in terms of rotation around the circle's origin
  var anglePerCard =
      radiansToDegrees(atan((cardWidth * cardSpacing) / arcRadius));
  String directionOption = direction.toUpperCase();
  int angleOffset = {"N": 270, "S": 90, "E": 0, "W": 180}[directionOption]!;

  var startAngle = angleOffset - 0.5 * anglePerCard * (numCards - 1);

  List<Map<String, dynamic>> coords = [];
  int i = 0;
  double minX = 99999.0;
  double minY = 99999.0;
  double maxX = -minX;
  double maxY = -minY;
  for (i = 0; i < numCards; i++) {
    double degrees = startAngle + anglePerCard * i;

    double radians = degreesToRadians(degrees);
    var x = cardWidth / 2 + cos(radians) * arcRadius;
    var y = cardHeight / 2 + sin(radians) * arcRadius;

    minX = min(minX, x);
    minY = min(minY, y);
    maxX = max(maxX, x);
    maxY = max(maxY, y);

    coords.add({"x": x, "y": y, "angle": degrees});
  }

  List<double> rotatedDimensions =
      getRotatedDimensions(coords[0]["angle"], cardWidth, cardHeight);

  double offsetX = 0.0;
  double offsetY = 0.0;

  if (directionOption == "N") {
    offsetX = minX * -1;
    offsetX += (rotatedDimensions[0] - cardWidth) / 2;

    offsetY = minY * -1;
  } else if (directionOption == "S") {
    offsetX = minX * -1;
    offsetX += (rotatedDimensions[0] - cardWidth) / 2;

    offsetY = (minY + (maxY - minY)) * -1;
  } else if (directionOption == "W") {
    offsetY = minY * -1;
    offsetY += (rotatedDimensions[1] - cardHeight) / 2;

    offsetX = minX * -1;
    offsetX +=
        cardHeight - rotatePointInBox(0, 0, 270, cardWidth, cardHeight)[1];
  } else if (directionOption == "E") {
    offsetY = minY * -1;
    offsetY += (rotatedDimensions[1] - cardHeight) / 2;

    offsetX = arcRadius * -1;
    offsetX -=
        cardHeight - rotatePointInBox(0, 0, 270, cardWidth, cardHeight)[1];
    //offsetX -= ?????;    // HELP! Needs to line up with yellow line!
  }

  for (int i = 0; i < coords.length; i++) {
    coords[i]["x"] += offsetX;
    coords[i]["x"] = (coords[i]["x"]);

    coords[i]["y"] += offsetY;
    coords[i]["y"] = (coords[i]["y"]);

    coords[i]["angle"] = coords[i]["angle"];
  }

  box["width"] = coords[numCards - 1]["x"] + cardWidth;
  box["height"] = coords[numCards - 1]["y"] + cardHeight;

  return coords;
}