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; }