Skip to content

Instantly share code, notes, and snippets.

@nurpax
Created February 9, 2019 23:32
Show Gist options
  • Save nurpax/f3bae64a3577485fec62f8f3c94e2644 to your computer and use it in GitHub Desktop.
Save nurpax/f3bae64a3577485fec62f8f3c94e2644 to your computer and use it in GitHub Desktop.
interface Matrix3x3 {
v: number[];
}
function c(a: Matrix3x3, col: number): number[] {
return [a.v[col], a.v[col + 3], a.v[col + 6]];
}
function r(a: Matrix3x3, row: number): number[] {
return [a.v[row*3+0], a.v[row*3+1], a.v[row*3+2]];
}
function dot(a: number[], b: number[]): number {
return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
}
function ident(): Matrix3x3 {
return {
v: [
1, 0, 0,
0, 1, 0,
0, 0, 1
]
}
}
function mult(a: Matrix3x3, b: Matrix3x3) {
return {
v: [
dot(r(a,0), c(b,0)), dot(r(a,0), c(b,1)), dot(r(a,0), c(b,2)),
dot(r(a,1), c(b,0)), dot(r(a,1), c(b,1)), dot(r(a,1), c(b,2)),
dot(r(a,2), c(b,0)), dot(r(a,2), c(b,1)), dot(r(a,2), c(b,2))
]
}
}
// a c tx
// b d ty
// 0 0 1
// ->
// [a b c d tx ty]
function toCss(a: Matrix3x3) {
const v = a.v;
return `matrix(${v[0]}, ${v[3]}, ${v[1]}, ${v[4]}, ${v[2]}, ${v[5]})`;
}
function mkScale(s: number): Matrix3x3 {
const m = ident();
m.v[0] = s;
m.v[4] = s;
return m;
}
function mkTranslate(x: number, y: number): Matrix3x3 {
const m = ident();
m.v[2] = x;
m.v[5] = y;
return m;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment