getStringListOfOverlappedGrids static method

List<String> getStringListOfOverlappedGrids(
  1. Matrix grid1,
  2. Matrix grid2
)

Creates a string representation of two overlaid matrices.

This static method compares two matrices cell by cell and generates a new representation where each cell is represented by a character based on the values in both input matrices.

Parameters:

  • grid1: The first Matrix to be overlaid.
  • grid2: The second Matrix to be overlaid.

Returns: A List

Throws: An Exception if the input matrices have different dimensions.

Note: This method is useful for visualizing the differences and similarities between two matrices, which can be helpful in debugging or analysis tasks.

Implementation

static List<String> getStringListOfOverlappedGrids(
  final Matrix grid1,
  final Matrix grid2,
) {
  final int height = grid1.rows;
  final int width = grid1.cols;

  if (height != grid2.rows || width != grid2.cols) {
    throw Exception('Grids must have the same dimensions');
  }

  final List<String> overlappedGrid = [];

  for (int row = 0; row < height; row++) {
    String overlappedRow = '';

    for (int col = 0; col < width; col++) {
      final bool cell1 = grid1._data[row][col];
      final bool cell2 = grid2._data[row][col];

      if (cell1 && cell2) {
        overlappedRow += '=';
      } else if (cell1) {
        overlappedRow += '*';
      } else if (cell2) {
        overlappedRow += '#';
      } else {
        overlappedRow += '.';
      }
    }

    overlappedGrid.add(overlappedRow);
  }

  return overlappedGrid;
}