diff --git a/lib/src/vector_math/matrix2.dart b/lib/src/vector_math/matrix2.dart index 816a46ce..7f07a8e7 100644 --- a/lib/src/vector_math/matrix2.dart +++ b/lib/src/vector_math/matrix2.dart @@ -136,12 +136,23 @@ class Matrix2 { /// Dimension of the matrix. int get dimension => 2; + /// Access the element of the matrix at the index [i]. double operator [](int i) => _m2storage[i]; + /// Set the element of the matrix at the index [i]. void operator []=(int i, double v) { _m2storage[i] = v; } + /// Check if two matrices are the same. + bool operator ==(other) { + return (other is Matrix2) && + (_m2storage[0] == other._m2storage[0]) && + (_m2storage[1] == other._m2storage[1]) && + (_m2storage[2] == other._m2storage[2]) && + (_m2storage[3] == other._m2storage[3]); + } + /// Returns row 0 Vector2 get row0 => getRow(0); diff --git a/lib/src/vector_math/matrix3.dart b/lib/src/vector_math/matrix3.dart index f0c11851..daac0e3e 100644 --- a/lib/src/vector_math/matrix3.dart +++ b/lib/src/vector_math/matrix3.dart @@ -234,6 +234,20 @@ class Matrix3 { _m3storage[i] = v; } + /// Check if two matrices are the same. + bool operator ==(other) { + return (other is Matrix3) && + (_m3storage[0] == other._m3storage[0]) && + (_m3storage[1] == other._m3storage[1]) && + (_m3storage[2] == other._m3storage[2]) && + (_m3storage[3] == other._m3storage[3]) && + (_m3storage[4] == other._m3storage[4]) && + (_m3storage[5] == other._m3storage[5]) && + (_m3storage[6] == other._m3storage[6]) && + (_m3storage[7] == other._m3storage[7]) && + (_m3storage[8] == other._m3storage[8]); + } + /// Returns row 0 Vector3 get row0 => getRow(0); diff --git a/lib/src/vector_math/matrix4.dart b/lib/src/vector_math/matrix4.dart index 42cc7219..8ccf053d 100644 --- a/lib/src/vector_math/matrix4.dart +++ b/lib/src/vector_math/matrix4.dart @@ -429,12 +429,35 @@ class Matrix4 { /// Dimension of the matrix. int get dimension => 4; + /// Access the element of the matrix at the index [i]. double operator [](int i) => _m4storage[i]; + /// Set the element of the matrix at the index [i]. void operator []=(int i, double v) { _m4storage[i] = v; } + /// Check if two matrices are the same. + bool operator ==(other) { + return (other is Matrix4) && + (_m4storage[0] == other._m4storage[0]) && + (_m4storage[1] == other._m4storage[1]) && + (_m4storage[2] == other._m4storage[2]) && + (_m4storage[3] == other._m4storage[3]) && + (_m4storage[4] == other._m4storage[4]) && + (_m4storage[5] == other._m4storage[5]) && + (_m4storage[6] == other._m4storage[6]) && + (_m4storage[7] == other._m4storage[7]) && + (_m4storage[8] == other._m4storage[8]) && + (_m4storage[9] == other._m4storage[9]) && + (_m4storage[10] == other._m4storage[10]) && + (_m4storage[11] == other._m4storage[11]) && + (_m4storage[12] == other._m4storage[12]) && + (_m4storage[13] == other._m4storage[13]) && + (_m4storage[14] == other._m4storage[14]) && + (_m4storage[15] == other._m4storage[15]); + } + /// Returns row 0 Vector4 get row0 => getRow(0); diff --git a/test/matrix2_test.dart b/test/matrix2_test.dart index 1954347a..18dc8871 100644 --- a/test/matrix2_test.dart +++ b/test/matrix2_test.dart @@ -112,6 +112,12 @@ void testMatrix2Solving() { expect(backwards.y, equals(b.y)); } +void testMatrix2Equals() { + expect(new Matrix2.identity(), equals(new Matrix2.identity())); + expect(new Matrix2.zero(), isNot(equals(new Matrix2.identity()))); + expect(new Matrix2.zero(), isNot(equals(5))); +} + void main() { group('Matrix2', () { test('Determinant', testMatrix2Determinant); @@ -121,5 +127,6 @@ void main() { test('dot product', testMatrix2Dot); test('Scale', testMatrix2Scale); test('solving', testMatrix2Solving); + test('equals', testMatrix2Equals); }); } diff --git a/test/matrix3_test.dart b/test/matrix3_test.dart index 21c6e798..05a3fd2a 100644 --- a/test/matrix3_test.dart +++ b/test/matrix3_test.dart @@ -310,6 +310,12 @@ void testMatrix3Solving() { expect(backwards2.y, equals(b2.y)); } +void testMatrix3Equals() { + expect(new Matrix3.identity(), equals(new Matrix3.identity())); + expect(new Matrix3.zero(), isNot(equals(new Matrix3.identity()))); + expect(new Matrix3.zero(), isNot(equals(5))); +} + void main() { group('Matrix3', () { test('Determinant', testMatrix3Determinant); @@ -325,5 +331,6 @@ void main() { test('dot product', testMatrix3Dot); test('Scale', testMatrix3Scale); test('solving', testMatrix3Solving); + test('equals', testMatrix3Equals); }); } diff --git a/test/matrix4_test.dart b/test/matrix4_test.dart index 4c1c30cf..de6faf35 100644 --- a/test/matrix4_test.dart +++ b/test/matrix4_test.dart @@ -589,6 +589,12 @@ void testMatrix4Compose() { } } +void testMatrix4Equals() { + expect(new Matrix4.identity(), equals(new Matrix4.identity())); + expect(new Matrix4.zero(), isNot(equals(new Matrix4.identity()))); + expect(new Matrix4.zero(), isNot(equals(5))); +} + void main() { group('Matrix4', () { test('instancing from Float32List', testMatrix4InstacingFromFloat32List); @@ -611,5 +617,6 @@ void main() { test('perspective transform', testMatrix4PerspectiveTransform); test('solving', testMatrix4Solving); test('compose/decompose', testMatrix4Compose); + test('equals', testMatrix4Equals); }); }