/** * Packs the index of the closest edge to the given point. The index corresponds to the index * of the vertex at the start of the edge. */ public static int getClosestEdgeIndex(Point2d point, ConvexPolygon2d polygon) { int index = -1; if (!polygon.hasAtLeastTwoVertices()) return index; double minDistance = Double.POSITIVE_INFINITY; for (int i = 0; i < polygon.getNumberOfVertices(); i++) { Point2d start = polygon.getVertex(i); Point2d end = polygon.getNextVertex(i); double distance = GeometryTools.distanceFromPointToLineSegment(point, start, end); if (distance < minDistance) { index = i; minDistance = distance; } } return index; }
/** * Checks if a line intersects the edge with the given index. */ public static boolean doesLineIntersectEdge(Line2d line, int edgeIndex, ConvexPolygon2d polygon) { if (!polygon.hasAtLeastTwoVertices()) return false; Point2d edgePointOne = polygon.getVertex(edgeIndex); Point2d edgePointTwo = polygon.getNextVertex(edgeIndex); double edgeVectorX = edgePointTwo.x - edgePointOne.x; double edgeVectorY = edgePointTwo.y - edgePointOne.y; double lambdaOne = getIntersectionLambda(edgePointOne.x, edgePointOne.y, edgeVectorX, edgeVectorY, line.getPoint().x, line.getPoint().y, line.getNormalizedVector().x, line.getNormalizedVector().y); if (Double.isNaN(lambdaOne) || lambdaOne < 0.0 || lambdaOne > 1.0) return false; return true; }
if (polygon.hasAtLeastTwoVertices())
return false; if (polygonP.hasExactlyTwoVertices() && polygonQ.hasAtLeastTwoVertices())