Skip to content

Commit

Permalink
Add equals operator to matrixX
Browse files Browse the repository at this point in the history
Includes tests
  • Loading branch information
Fox32 committed Jul 23, 2015
1 parent 3faf71a commit de2aa31
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/src/vector_math/matrix2.dart
Expand Up @@ -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);

Expand Down
14 changes: 14 additions & 0 deletions lib/src/vector_math/matrix3.dart
Expand Up @@ -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);

Expand Down
23 changes: 23 additions & 0 deletions lib/src/vector_math/matrix4.dart
Expand Up @@ -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);

Expand Down
7 changes: 7 additions & 0 deletions test/matrix2_test.dart
Expand Up @@ -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);
Expand All @@ -121,5 +127,6 @@ void main() {
test('dot product', testMatrix2Dot);
test('Scale', testMatrix2Scale);
test('solving', testMatrix2Solving);
test('equals', testMatrix2Equals);
});
}
7 changes: 7 additions & 0 deletions test/matrix3_test.dart
Expand Up @@ -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);
Expand All @@ -325,5 +331,6 @@ void main() {
test('dot product', testMatrix3Dot);
test('Scale', testMatrix3Scale);
test('solving', testMatrix3Solving);
test('equals', testMatrix3Equals);
});
}
7 changes: 7 additions & 0 deletions test/matrix4_test.dart
Expand Up @@ -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);
Expand All @@ -611,5 +617,6 @@ void main() {
test('perspective transform', testMatrix4PerspectiveTransform);
test('solving', testMatrix4Solving);
test('compose/decompose', testMatrix4Compose);
test('equals', testMatrix4Equals);
});
}

0 comments on commit de2aa31

Please sign in to comment.