Skip to content

Commit

Permalink
Add equality and matrix multiplication methods to Matrix3
Browse files Browse the repository at this point in the history
  • Loading branch information
yellcorp committed Mar 28, 2017
1 parent ac8c3e6 commit 1b963b9
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/math/Matrix3.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,62 @@ Object.assign( Matrix3.prototype, {

}(),

multiply: function ( m ) {

return this.multiplyMatrices( this, m );

},

premultiply: function ( m ) {

return this.multiplyMatrices( m, this );

},

multiplyMatrices: function ( a, b ) {

var ae = a.elements;
var be = b.elements;
var te = this.elements;

var a11 = ae[ 0 ], a12 = ae[ 3 ], a13 = ae[ 6 ];
var a21 = ae[ 1 ], a22 = ae[ 4 ], a23 = ae[ 7 ];
var a31 = ae[ 2 ], a32 = ae[ 5 ], a33 = ae[ 8 ];

var b11 = be[ 0 ], b12 = be[ 3 ], b13 = be[ 6 ];
var b21 = be[ 1 ], b22 = be[ 4 ], b23 = be[ 7 ];
var b31 = be[ 2 ], b32 = be[ 5 ], b33 = be[ 8 ];

te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31;
te[ 3 ] = a11 * b12 + a12 * b22 + a13 * b32;
te[ 6 ] = a11 * b13 + a12 * b23 + a13 * b33;

te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31;
te[ 4 ] = a21 * b12 + a22 * b22 + a23 * b32;
te[ 7 ] = a21 * b13 + a22 * b23 + a23 * b33;

te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31;
te[ 5 ] = a31 * b12 + a32 * b22 + a33 * b32;
te[ 8 ] = a31 * b13 + a32 * b23 + a33 * b33;

return this;

},

multiplyToArray: function ( a, b, r ) {

var te = this.elements;

this.multiplyMatrices( a, b );

r[ 0 ] = te[ 0 ]; r[ 1 ] = te[ 1 ]; r[ 2 ] = te[ 2 ];
r[ 3 ] = te[ 3 ]; r[ 4 ] = te[ 4 ]; r[ 5 ] = te[ 5 ];
r[ 6 ] = te[ 6 ]; r[ 7 ] = te[ 7 ]; r[ 8 ] = te[ 8 ];

return this;

},

multiplyScalar: function ( s ) {

var te = this.elements;
Expand Down Expand Up @@ -231,6 +287,21 @@ Object.assign( Matrix3.prototype, {

},

equals: function ( matrix ) {

var te = this.elements;
var me = matrix.elements;

for ( var i = 0; i < 9; i ++ ) {

if ( te[ i ] !== me[ i ] ) return false;

}

return true;

},

fromArray: function ( array, offset ) {

if ( offset === undefined ) offset = 0;
Expand Down

0 comments on commit 1b963b9

Please sign in to comment.