From 61cec78d118a64253e5c7e1b907a4cb955c77c01 Mon Sep 17 00:00:00 2001 From: Jose Puente Date: Thu, 21 Nov 2024 12:58:43 +0100 Subject: [PATCH] fix duplicate points returned in polygon intersection (#7519) * fix duplicate points returned in polygon intersection * apply spotless * improve comments --- gdx/src/com/badlogic/gdx/math/Intersector.java | 7 +++++-- .../com/badlogic/gdx/math/IntersectorTest.java | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/gdx/src/com/badlogic/gdx/math/Intersector.java b/gdx/src/com/badlogic/gdx/math/Intersector.java index add592884e0..5f128f0455b 100644 --- a/gdx/src/com/badlogic/gdx/math/Intersector.java +++ b/gdx/src/com/badlogic/gdx/math/Intersector.java @@ -197,6 +197,9 @@ public static boolean intersectPolygons (Polygon p1, Polygon p2, Polygon overlap floatArray2.addAll(floatArray); floatArray.clear(); } + // Ensure first and last point are different + if (floatArray2.size >= 6 && floatArray2.get(0) == floatArray2.get(floatArray2.size - 2) + && floatArray2.get(1) == floatArray2.get(floatArray2.size - 1)) floatArray2.setSize(floatArray2.size - 2); // Check for 3 or more vertices needed due to floating point precision errors if (floatArray2.size >= 6) { if (overlap != null) { @@ -210,14 +213,14 @@ public static boolean intersectPolygons (Polygon p1, Polygon p2, Polygon overlap return false; } - /** Returns true if the specified poygons intersect. */ + /** Returns true if the specified polygons intersect. */ static public boolean intersectPolygons (FloatArray polygon1, FloatArray polygon2) { if (Intersector.isPointInPolygon(polygon1.items, 0, polygon1.size, polygon2.items[0], polygon2.items[1])) return true; if (Intersector.isPointInPolygon(polygon2.items, 0, polygon2.size, polygon1.items[0], polygon1.items[1])) return true; return intersectPolygonEdges(polygon1, polygon2); } - /** Returns true if the lines of the specified poygons intersect. */ + /** Returns true if the lines of the specified polygons intersect. */ static public boolean intersectPolygonEdges (FloatArray polygon1, FloatArray polygon2) { int last1 = polygon1.size - 2, last2 = polygon2.size - 2; float[] p1 = polygon1.items, p2 = polygon2.items; diff --git a/gdx/test/com/badlogic/gdx/math/IntersectorTest.java b/gdx/test/com/badlogic/gdx/math/IntersectorTest.java index c33b7aa3388..c29994c407d 100644 --- a/gdx/test/com/badlogic/gdx/math/IntersectorTest.java +++ b/gdx/test/com/badlogic/gdx/math/IntersectorTest.java @@ -231,4 +231,20 @@ public void testIntersectPolygons () { new Polygon(new float[] {3213.0f, 131.0f, 3214.0f, 131.0f, 3214.0f, 130.0f, 3213.0f, 130.0f}), intersectionPolygon)); assertEquals(0, intersectionPolygon.getVertexCount()); } + + @Test + public void testIntersectPolygonsWithVertexLyingOnEdge () { + Polygon p1 = new Polygon(new float[] {1, -1, 2, -1, 2, -2, 1, -2}); + Polygon p2 = new Polygon(new float[] {0.5f, -1.5f, 1.5f, -1.5f, 1.5f, -2.5f}); + + Polygon intersectionPolygon = new Polygon(); + boolean checkResult = Intersector.intersectPolygons(p1, p2, intersectionPolygon); + + assertTrue(checkResult); + assertEquals(4, intersectionPolygon.getVertexCount()); + assertEquals(new Vector2(1.0f, -2.0f), intersectionPolygon.getVertex(0, new Vector2())); + assertEquals(new Vector2(1.0f, -1.5f), intersectionPolygon.getVertex(1, new Vector2())); + assertEquals(new Vector2(1.5f, -1.5f), intersectionPolygon.getVertex(2, new Vector2())); + assertEquals(new Vector2(1.5f, -2.0f), intersectionPolygon.getVertex(3, new Vector2())); + } }