Skip to content

Instantly share code, notes, and snippets.

@daybrush
Created July 6, 2019 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daybrush/37b0cfd3fd621332def52757f6df8781 to your computer and use it in GitHub Desktop.
Save daybrush/37b0cfd3fd621332def52757f6df8781 to your computer and use it in GitHub Desktop.
invert matrix
export function invert3x2(a: number[]) {
// 00 01 02
// 10 11 12
// 20 21 22
const [
a00,
a10,
a01,
a11,
a02,
a12,
] = a;
const a20 = 0;
const a21 = 0;
const a22 = 1;
const det
= a00 * a11 * a22
+ a01 * a12 * a20
+ a02 * a10 * a21
- a02 * a11 * a20
- a01 * a10 * a22
- a00 * a12 * a21;
const b00 = a11 * a22 - a12 * a21;
const b01 = a02 * a21 - a01 * a22;
const b02 = a01 * a12 - a02 * a11;
const b10 = a12 * a20 - a10 * a22;
const b11 = a22 * a00 - a20 * a02;
const b12 = a02 * a10 - a00 * a12;
// const b20 = a11 * a21 - a11 * a20;
// const b21 = a20 * a01 - a21 * a00;
// const b22 = a00 * a11 - a01 * a10;
a[0] = b00 / det;
a[1] = b10 / det;
a[2] = b01 / det;
a[3] = b11 / det;
a[4] = b02 / det;
a[5] = b12 / det;
return a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment