compose2DMatrix static method

Matrix4 compose2DMatrix(
  1. List decomposed
)

Implementation

static Matrix4 compose2DMatrix(List decomposed) {
  // a 4x4 matrix initialized to identity matrix
  List<List<double>> matrix = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];

  List<double> translate = decomposed[0];
  List<double> scale = decomposed[1];
  double angle = decomposed[2];
  double m11 = decomposed[3];
  double m12 = decomposed[4];
  double m21 = decomposed[5];
  double m22 = decomposed[6];

  matrix[0][0] = m11;
  matrix[0][1] = m12;
  matrix[1][0] = m21;
  matrix[1][1] = m22;

  // Translate matrix.
  matrix[3][0] = translate[0] * m11 + translate[1] * m21;
  matrix[3][1] = translate[0] * m12 + translate[1] * m22;

  // Rotate matrix.
  angle = _deg2rad(angle)!;
  double cosAngle = cos(angle);
  double sinAngle = sin(angle);

  // New temporary, identity initialized, 4x4 matrix rotateMatrix
  List<List<double>> rotateMatrix = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];

  rotateMatrix[0][0] = cosAngle;
  rotateMatrix[0][1] = sinAngle;
  rotateMatrix[1][0] = -sinAngle;
  rotateMatrix[1][1] = cosAngle;

  matrix = _multiply(matrix, rotateMatrix);

  // Scale matrix.
  matrix[0][0] *= scale[0];
  matrix[0][1] *= scale[0];
  matrix[1][0] *= scale[1];
  matrix[1][1] *= scale[1];

  return Matrix4.columns(
    Vector4.array(matrix[0]),
    Vector4.array(matrix[1]),
    Vector4.array(matrix[2]),
    Vector4.array(matrix[3])
  );
}