Skip to content

Commit

Permalink
fix duplicate points returned in polygon intersection (libgdx#7519)
Browse files Browse the repository at this point in the history
* fix duplicate points returned in polygon intersection

* apply spotless

* improve comments
  • Loading branch information
jose-puente authored Nov 21, 2024
1 parent 9886f3b commit 61cec78
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 5 additions & 2 deletions gdx/src/com/badlogic/gdx/math/Intersector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions gdx/test/com/badlogic/gdx/math/IntersectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}

0 comments on commit 61cec78

Please sign in to comment.