honzabrecka/gama

Why are Matrices represented by columns?

Closed this issue · 2 comments

Hi, gama is really cool :) I'm not terribly experienced with using matrix-math libraries so please excuse me if the answer to my question is very obvious.

I was wondering, why are matrix arrays laid out by columns? I mean, if they used rows it would be nice to be able to write a matrix like this:

[1, 0, 0,
 0, 1, 0,
 0, 0, 1]

But the way gama works this would be really confusing since the rows would actually represent columns.

btw, invertMatrix would be nice to have...

var invertMatrix = m => {
  var det = m[0]*m[4]*m[8]
            + m[1]*m[5]*m[6]
            + m[2]*m[3]*m[7]
            - m[0]*m[5]*m[7]
            - m[1]*m[3]*m[8]
            - m[2]*m[4]*m[6];

  return [(m[4] * m[8] - m[5] * m[7]) / det,
          (m[2] * m[7] - m[1] * m[8]) / det,
          (m[1] * m[5] - m[2] * m[4]) / det,
          (m[5] * m[6] - m[3] * m[8]) / det,
          (m[0] * m[8] - m[2] * m[6]) / det,
          (m[2] * m[3] - m[0] * m[5]) / det,
          (m[3] * m[7] - m[4] * m[6]) / det,
          (m[1] * m[6] - m[0] * m[7]) / det,
          (m[0] * m[4] - m[1] * m[3]) / det];
};

Hey Gil, you are absolutely right! I can't remember why I did it this way. I changed it to the most common representation, which is:

[1, 0, tx,
 0, 1, ty,
 0, 0, 1]

Sure, gama should provide invertMatrix function. Do you want to create PR?

Thanks for looking into this. 👍 Yes I'll put in a PR for invertMatrix.