Skip to content

Commit d99c903

Browse files
authored
Added a toString, operator == and hashCode to the Quad class. (#311)
* 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.
1 parent 38a00c3 commit d99c903

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

lib/src/vector_math/quad.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,21 @@ class Quad {
8888
_point2.add(offset);
8989
_point3.add(offset);
9090
}
91+
92+
/// Returns a printable string
93+
@override
94+
String toString() => '[0] $_point0\n[1] $_point1\n'
95+
'[2] $_point2\n[3] $_point3\n';
96+
97+
/// Check if two quad are the same.
98+
@override
99+
bool operator ==(Object other) =>
100+
(other is Quad) &&
101+
(_point3 == other._point3) &&
102+
(_point2 == other._point2) &&
103+
(_point1 == other._point1) &&
104+
(_point0 == other._point0);
105+
106+
@override
107+
int get hashCode => Object.hash(_point0, _point1, _point2, _point3);
91108
}

lib/src/vector_math/vector4.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ class Vector4 implements Vector {
119119

120120
/// Returns a printable string
121121
@override
122-
String toString() => '${_v4storage[0]},${_v4storage[1]},'
123-
'${_v4storage[2]},${_v4storage[3]}';
122+
String toString() => '[${_v4storage[0]},${_v4storage[1]},'
123+
'${_v4storage[2]},${_v4storage[3]}]';
124124

125125
/// Check if two vectors are the same.
126126
@override

lib/src/vector_math_64/quad.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,21 @@ class Quad {
8888
_point2.add(offset);
8989
_point3.add(offset);
9090
}
91+
92+
/// Returns a printable string
93+
@override
94+
String toString() => '[0] $_point0\n[1] $_point1\n'
95+
'[2] $_point2\n[3] $_point3\n';
96+
97+
/// Check if two quad are the same.
98+
@override
99+
bool operator ==(Object other) =>
100+
(other is Quad) &&
101+
(_point3 == other._point3) &&
102+
(_point2 == other._point2) &&
103+
(_point1 == other._point1) &&
104+
(_point0 == other._point0);
105+
106+
@override
107+
int get hashCode => Object.hash(_point0, _point1, _point2, _point3);
91108
}

lib/src/vector_math_64/vector4.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ class Vector4 implements Vector {
119119

120120
/// Returns a printable string
121121
@override
122-
String toString() => '${_v4storage[0]},${_v4storage[1]},'
123-
'${_v4storage[2]},${_v4storage[3]}';
122+
String toString() => '[${_v4storage[0]},${_v4storage[1]},'
123+
'${_v4storage[2]},${_v4storage[3]}]';
124124

125125
/// Check if two vectors are the same.
126126
@override

test/quad_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,29 @@ void testQuadCopyTriangles() {
4848
relativeTest(t2Normal, normal);
4949
}
5050

51+
void testQuadEquals() {
52+
final v1 = Vector3(1.0, 0.0, 1.0);
53+
final v2 = Vector3(0.0, 2.0, 1.0);
54+
final v3 = Vector3(1.0, 0.0, 0.0);
55+
final v4 = Vector3(0.0, 2.0, 0.0);
56+
final quad = Quad.points(v1, v2, v3, v4);
57+
58+
expect(quad, Quad.points(v1, v2, v3, v4));
59+
60+
expect(quad, isNot(Quad.points(Vector3.zero(), v2, v3, v4)));
61+
expect(quad, isNot(Quad.points(v1, Vector3.zero(), v3, v4)));
62+
expect(quad, isNot(Quad.points(v1, v2, Vector3.zero(), v4)));
63+
expect(quad, isNot(Quad.points(v1, v2, v3, Vector3.zero())));
64+
65+
expect(Quad.points(v1, v2, v3, v4).hashCode,
66+
equals(Quad.points(v1, v2, v3, v4).hashCode));
67+
}
68+
5169
void main() {
5270
group('Quad', () {
5371
test('Copy', testQuadCopy);
5472
test('CopyNormalInto', testQuadCopyNormalInto);
5573
test('CopyTriangles', testQuadCopyTriangles);
74+
test('equals', testQuadEquals);
5675
});
5776
}

0 commit comments

Comments
 (0)