arrangeInGrid method

void arrangeInGrid({
  1. int? numRows,
  2. int? numCols,
  3. bool center = true,
  4. double buffer = DEFAULT_MOBJECT_TO_MOBJECT_BUFFER,
  5. Vector3 alignedEdge = ORIGIN,
  6. Vector3 coordinateMask = const Vector3(1, 1, 1),
})

Implementation

void arrangeInGrid(
    {int? numRows,
    int? numCols,
    bool center = true,
    double buffer = DEFAULT_MOBJECT_TO_MOBJECT_BUFFER,
    Vector3 alignedEdge = ORIGIN,
    // TODO: add submobjectToAlign
    // Mobject? submobjectToAlign,
    // int? indexOfSubmobjectToAlign,
    Vector3 coordinateMask = const Vector3(1, 1, 1)}) {
  // Only to make the code more readable
  var length = this.length;

  if (numRows == null && numCols == null) {
    numCols = sqrt(length).toInt();
  }

  late Vector3 v1, v2;
  late int n;

  if (numRows != null) {
    v1 = RIGHT;
    v2 = DOWN;
    //* "~/" is equivalent to "//" in python
    n = length ~/ numRows;
  } else if (numCols != null) {
    v1 = DOWN;
    v2 = RIGHT;
    //* "~/" is equivalent to "//" in python
    n = length ~/ numCols;
  }

  var arrangedGroup = (List<Mobject> mobs, Vector3 direction) {
    var group = Group(mobs);
    group.arrange(
        direction: direction,
        center: center,
        buffer: buffer,
        alignedEdge: alignedEdge,
        // TODO: add submobjectToAlign
        // submobjectToAlign: submobjectToAlign,
        // indexOfSubmobjectToAlign: indexOfSubmobjectToAlign
        coordinateMask: coordinateMask);
    return group;
  };

  arrangedGroup([
    for (var i in range(start: 0, end: length, step: n))
      arrangedGroup(
          submobjects
              .whereIndexed((index, element) => index >= i && index < i + n)
              .toList(),
          v1)
  ], v2);
}