Skip to content

Commit

Permalink
Added a toString, operator == and hashCode to the Quad class. (#311)
Browse files Browse the repository at this point in the history
* Added a toString, operator == and hashCode to the Quad class.

* Changed vector4 toString to be consistent with vector{2,3}.

* Fixed a couple of typos.
  • Loading branch information
bramp authored Jan 25, 2024
1 parent 38a00c3 commit d99c903
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
17 changes: 17 additions & 0 deletions lib/src/vector_math/quad.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,21 @@ class Quad {
_point2.add(offset);
_point3.add(offset);
}

/// Returns a printable string
@override
String toString() => '[0] $_point0\n[1] $_point1\n'
'[2] $_point2\n[3] $_point3\n';

/// Check if two quad are the same.
@override
bool operator ==(Object other) =>
(other is Quad) &&
(_point3 == other._point3) &&
(_point2 == other._point2) &&
(_point1 == other._point1) &&
(_point0 == other._point0);

@override
int get hashCode => Object.hash(_point0, _point1, _point2, _point3);
}
4 changes: 2 additions & 2 deletions lib/src/vector_math/vector4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ class Vector4 implements Vector {

/// Returns a printable string
@override
String toString() => '${_v4storage[0]},${_v4storage[1]},'
'${_v4storage[2]},${_v4storage[3]}';
String toString() => '[${_v4storage[0]},${_v4storage[1]},'
'${_v4storage[2]},${_v4storage[3]}]';

/// Check if two vectors are the same.
@override
Expand Down
17 changes: 17 additions & 0 deletions lib/src/vector_math_64/quad.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,21 @@ class Quad {
_point2.add(offset);
_point3.add(offset);
}

/// Returns a printable string
@override
String toString() => '[0] $_point0\n[1] $_point1\n'
'[2] $_point2\n[3] $_point3\n';

/// Check if two quad are the same.
@override
bool operator ==(Object other) =>
(other is Quad) &&
(_point3 == other._point3) &&
(_point2 == other._point2) &&
(_point1 == other._point1) &&
(_point0 == other._point0);

@override
int get hashCode => Object.hash(_point0, _point1, _point2, _point3);
}
4 changes: 2 additions & 2 deletions lib/src/vector_math_64/vector4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ class Vector4 implements Vector {

/// Returns a printable string
@override
String toString() => '${_v4storage[0]},${_v4storage[1]},'
'${_v4storage[2]},${_v4storage[3]}';
String toString() => '[${_v4storage[0]},${_v4storage[1]},'
'${_v4storage[2]},${_v4storage[3]}]';

/// Check if two vectors are the same.
@override
Expand Down
19 changes: 19 additions & 0 deletions test/quad_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,29 @@ void testQuadCopyTriangles() {
relativeTest(t2Normal, normal);
}

void testQuadEquals() {
final v1 = Vector3(1.0, 0.0, 1.0);
final v2 = Vector3(0.0, 2.0, 1.0);
final v3 = Vector3(1.0, 0.0, 0.0);
final v4 = Vector3(0.0, 2.0, 0.0);
final quad = Quad.points(v1, v2, v3, v4);

expect(quad, Quad.points(v1, v2, v3, v4));

expect(quad, isNot(Quad.points(Vector3.zero(), v2, v3, v4)));
expect(quad, isNot(Quad.points(v1, Vector3.zero(), v3, v4)));
expect(quad, isNot(Quad.points(v1, v2, Vector3.zero(), v4)));
expect(quad, isNot(Quad.points(v1, v2, v3, Vector3.zero())));

expect(Quad.points(v1, v2, v3, v4).hashCode,
equals(Quad.points(v1, v2, v3, v4).hashCode));
}

void main() {
group('Quad', () {
test('Copy', testQuadCopy);
test('CopyNormalInto', testQuadCopyNormalInto);
test('CopyTriangles', testQuadCopyTriangles);
test('equals', testQuadEquals);
});
}

0 comments on commit d99c903

Please sign in to comment.