From 980627d08414564d06cbc53e10c1dce5845e3667 Mon Sep 17 00:00:00 2001 From: Andrew Brampton Date: Thu, 25 Jan 2024 11:18:52 -0800 Subject: [PATCH 1/3] Added a toString, operator == and hashCode to the Quad class. --- lib/src/vector_math/quad.dart | 17 +++++++++++++++++ lib/src/vector_math_64/quad.dart | 17 +++++++++++++++++ test/quad_test.dart | 19 +++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/lib/src/vector_math/quad.dart b/lib/src/vector_math/quad.dart index 39064b91..4437c004 100644 --- a/lib/src/vector_math/quad.dart +++ b/lib/src/vector_math/quad.dart @@ -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 vectors 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); } diff --git a/lib/src/vector_math_64/quad.dart b/lib/src/vector_math_64/quad.dart index 5a5b3aa4..e1a10070 100644 --- a/lib/src/vector_math_64/quad.dart +++ b/lib/src/vector_math_64/quad.dart @@ -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 vectors 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); } diff --git a/test/quad_test.dart b/test/quad_test.dart index 051a48ff..b97e0dbe 100644 --- a/test/quad_test.dart +++ b/test/quad_test.dart @@ -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); }); } From 8061e7ab9d8354662ccf394b3885e0d68a1b3e65 Mon Sep 17 00:00:00 2001 From: Andrew Brampton Date: Thu, 25 Jan 2024 11:19:22 -0800 Subject: [PATCH 2/3] Changed vector4 toString to be consistent with vector{2,3}. --- lib/src/vector_math/vector4.dart | 4 ++-- lib/src/vector_math_64/vector4.dart | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/src/vector_math/vector4.dart b/lib/src/vector_math/vector4.dart index 37780958..e9dc95f9 100644 --- a/lib/src/vector_math/vector4.dart +++ b/lib/src/vector_math/vector4.dart @@ -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 diff --git a/lib/src/vector_math_64/vector4.dart b/lib/src/vector_math_64/vector4.dart index d017f4a9..5157b03b 100644 --- a/lib/src/vector_math_64/vector4.dart +++ b/lib/src/vector_math_64/vector4.dart @@ -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 From 7252b463e54084c9ffed347f22e12dd29003fe2d Mon Sep 17 00:00:00 2001 From: Andrew Brampton Date: Thu, 25 Jan 2024 12:01:41 -0800 Subject: [PATCH 3/3] Fixed a couple of typos. --- lib/src/vector_math/quad.dart | 2 +- lib/src/vector_math_64/quad.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/vector_math/quad.dart b/lib/src/vector_math/quad.dart index 4437c004..0e46e1d2 100644 --- a/lib/src/vector_math/quad.dart +++ b/lib/src/vector_math/quad.dart @@ -94,7 +94,7 @@ class Quad { String toString() => '[0] $_point0\n[1] $_point1\n' '[2] $_point2\n[3] $_point3\n'; - /// Check if two vectors are the same. + /// Check if two quad are the same. @override bool operator ==(Object other) => (other is Quad) && diff --git a/lib/src/vector_math_64/quad.dart b/lib/src/vector_math_64/quad.dart index e1a10070..04eb6255 100644 --- a/lib/src/vector_math_64/quad.dart +++ b/lib/src/vector_math_64/quad.dart @@ -94,7 +94,7 @@ class Quad { String toString() => '[0] $_point0\n[1] $_point1\n' '[2] $_point2\n[3] $_point3\n'; - /// Check if two vectors are the same. + /// Check if two quad are the same. @override bool operator ==(Object other) => (other is Quad) &&