/** * Efficiently combines two Disjoint Polygons. Returns false if not disjoint. * * @param polygon1 ConvexPolygon2d * @param polygon2 ConvexPolygon2d * @param combinedPolygonToPack ConvexPolygon2d polygon in which we put the convex hull containing * polygon1 and polygon2. * @param connectingEdge1ToPack LineSegment2d first connecting edge between polygon1 and polygon2. * @param connectingEdge2Topack LineSegment2d second connecting edge between polygon1 and * polygon2. * @return true if succeeded, false if failed */ public static boolean combineDisjointPolygons( ConvexPolygon2d polygon1, ConvexPolygon2d polygon2, ConvexPolygon2d combinedPolygonToPack, LineSegment2d connectingEdge1ToPack, LineSegment2d connectingEdge2Topack) { int[][] verticesIndices = new int[2][2]; boolean success = findConnectingEdgesVerticesIndexes(polygon1, polygon2, verticesIndices); if (!success) return false; combinedPolygonToPack.clear(); polygon1.addVerticesInClockwiseOrderInPolygon( verticesIndices[0][1], verticesIndices[0][0], combinedPolygonToPack); polygon2.addVerticesInClockwiseOrderInPolygon( verticesIndices[1][0], verticesIndices[1][1], combinedPolygonToPack); combinedPolygonToPack.update(); packConnectingEdges( polygon1, polygon2, connectingEdge1ToPack, connectingEdge2Topack, verticesIndices); return true; }
private static boolean constructPolygonForIntersection( ArrayList<Boolean> decrementP, int[][][] crossingIndices, ConvexPolygon2d polygonP, ConvexPolygon2d polygonQ, ConvexPolygon2d intersectingPolygonToPack) { int startIndexP1 = crossingIndices[0][0][0]; int endIndexP1 = crossingIndices[0][0][1]; int startIndexQ1 = crossingIndices[0][1][0]; int endIndexQ1 = crossingIndices[0][1][1]; // Want to do things in clockwise order so that making the new polygon is fast. Should only be // one of two cases. // a) P1/Q2 start->end increments, P2/Q1 start->end decrement; OR // b) P2/Q1 start->end increments, P1/Q2 start->end decrement; OR boolean incrementPNext; if ((startIndexP1 + 1) % polygonP.getNumberOfVertices() == endIndexP1) { incrementPNext = true; } else if ((startIndexQ1 + 1) % polygonQ.getNumberOfVertices() == endIndexQ1) { incrementPNext = false; } else { throw new RuntimeException("Neither P1, nor P2 increment!!!"); } intersectingPolygonToPack.clear(); // Start at the first intersection. Add it. Then march to the next intersection. Then continue. for (int i = 0; i < crossingIndices.length; i++) { int startIndexP = crossingIndices[i][0][0]; int endIndexP = crossingIndices[i][0][1]; int startIndexQ = crossingIndices[i][1][0]; int endIndexQ = crossingIndices[i][1][1]; Point2d startP = polygonP.getVertex(startIndexP); Point2d endP = polygonP.getVertex(endIndexP); Point2d startQ = polygonQ.getVertex(startIndexQ); Point2d endQ = polygonQ.getVertex(endIndexQ); Point2d intersection = GeometryTools.getIntersectionBetweenTwoLines(startP, endP, startQ, endQ); intersectingPolygonToPack.addVertex(intersection); if (incrementPNext) { int indexP = crossingIndices[i][0][1]; // endIndexP; int indexPNext = crossingIndices[(i + 1) % crossingIndices.length][0][0]; // System.out.println("indexP = " + indexP + ", indexPNext = " + indexPNext); while (indexP != indexPNext) { intersectingPolygonToPack.addVertex(polygonP.getVertex(indexP)); indexP = polygonP.getNextVertexIndex(indexP); } } else { int indexQ = crossingIndices[i][1][1]; // endIndexQ; int indexQNext = crossingIndices[(i + 1) % crossingIndices.length][1][0]; // System.out.println("indexQ = " + indexQ + ", indexQNext = " + indexQNext); while (indexQ != indexQNext) { intersectingPolygonToPack.addVertex(polygonQ.getVertex(indexQ)); indexQ = polygonQ.getNextVertexIndex(indexQ); } } incrementPNext = !incrementPNext; } intersectingPolygonToPack.update(); if (intersectingPolygonToPack.isEmpty()) return false; return true; }