inverted property

Float64List get inverted

Implementation

Float64List get inverted {
  double a = this[0];
  double b = this[1];
  double c = this[4];
  double d = this[5];
  double tx = this[12];
  double ty = this[13];

  double det = a * d - b * c;
  if (det == 0) {
    throw Exception('Matrix cannot be inverted because it is singular');
  }

  double invertedA = d / det;
  double invertedB = -b / det;
  double invertedC = -c / det;
  double invertedD = a / det;
  double invertedTx = (c * ty - d * tx) / det;
  double invertedTy = (b * tx - a * ty) / det;

  final Float64List invertedMatrix = Float64List(16);

  invertedMatrix[0] = invertedA;
  invertedMatrix[1] = invertedB;
  invertedMatrix[4] = invertedC;
  invertedMatrix[5] = invertedD;
  invertedMatrix[12] = invertedTx;
  invertedMatrix[13] = invertedTy;

  invertedMatrix[10] = 1;
  invertedMatrix[15] = 1;

  return invertedMatrix;
}