invert method

GMatrix invert()

Inverts this matrix, i.e. applies a matrix transformation that is the opposite of the original one.

If the matrix is not invertible (i.e. its determinant is 0), this method sets the values of the matrix to a non-invertible state and returns itself.

Returns this matrix instance after inverting it for easy chaining.

Implementation

GMatrix invert() {
  var n = a * d - b * c;
  if (n == 0) {
    a = b = c = d = 0;
    tx = -tx;
    ty = -ty;
  } else {
    n = 1 / n;
    var a1 = d * n;
    d = a * n;
    a = a1;
    b *= -n;
    c *= -n;
    var tx1 = -a * tx - c * ty;
    ty = -b * tx - d * ty;
    tx = tx1;
  }
  return this;
}