Skip to content

Added a toString, operator == and hashCode to the Quad class. #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]},'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one in _64 also needs this fix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was also fixed in my original commit. Please double check it looks good.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, completely missed it when reviewing on the phone. All good then! :)

'${_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);
});
}