From 095823efc4b5f07a1d3181f0e6d7ae51aa1846fa Mon Sep 17 00:00:00 2001 From: Richiedu38 <115776110+Richiedu38@users.noreply.github.com> Date: Tue, 30 Apr 2024 16:21:39 -0500 Subject: [PATCH] Feature/convex polygon geometrically equals testing (#63) This PR adds a test to validate that issue #51 has been resolved. --------- Co-authored-by: vroy --- .../euclid/geometry/ConvexPolygon2DTest.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/test/java/us/ihmc/euclid/geometry/ConvexPolygon2DTest.java b/src/test/java/us/ihmc/euclid/geometry/ConvexPolygon2DTest.java index c177575d6..489a0598a 100644 --- a/src/test/java/us/ihmc/euclid/geometry/ConvexPolygon2DTest.java +++ b/src/test/java/us/ihmc/euclid/geometry/ConvexPolygon2DTest.java @@ -131,4 +131,49 @@ public void testConstructors() assertEquals(4.0, polygonPolygon.getNumberOfVertices(), EPSILON, "Number of vertices should be four"); assertTrue(polygonPolygon.isUpToDate()); } + + @Test + + + /* If two polygons built in different ways (vertices in different order), they are still equivalent + + */ + public void geometricallyEquals() + { + + int numberOfVertices = 4; + ArrayList verticesList = new ArrayList<>(); + verticesList.add(new Point2D(0.0, 0.0)); + verticesList.add(new Point2D(0.0, 1.0)); + verticesList.add(new Point2D(1.0, 0.0)); + verticesList.add(new Point2D(1.0, 1.0)); + + int numberOfVertices2 = 4; + ArrayList verticesList2 = new ArrayList<>(); + + verticesList2.add(new Point2D(1.0, 0.0)); + verticesList2.add(new Point2D(1.0, 1.0)); + verticesList2.add(new Point2D(0.0, 1.0)); + verticesList2.add(new Point2D(0.0, 0.0)); + + ConvexPolygon2D polygonA = new ConvexPolygon2D(Vertex2DSupplier.asVertex2DSupplier(verticesList, numberOfVertices)); + ConvexPolygon2D polygonB = new ConvexPolygon2D(Vertex2DSupplier.asVertex2DSupplier(verticesList2, numberOfVertices2)); + + assertEquals(polygonA.getNumberOfVertices(), polygonB.getNumberOfVertices()); + + for (int i = 0; i < numberOfVertices; i++) + { + boolean foundMatchingVertex = false; + for (int j = 0; j < numberOfVertices2; j++) + { + if (polygonA.getVertex(i).equals(polygonB.getVertex(j))) + { + assertEquals(polygonA.getVertex(i), polygonB.getVertex(j)); + foundMatchingVertex = true; + break; + } // Once we find the corresponding vertex, we stop + } + assertTrue(foundMatchingVertex, "No matching vertex found in polygonB for vertex" + polygonA.getVertex(i)); + } + } }