/** * Packs the closest vertex of the polygon to the given point */ public static boolean getClosestVertex(Point2d point, ConvexPolygon2d polygon, Point2d pointToPack) { int index = getClosestVertexIndex(point, polygon); if (index < 0) return false; pointToPack.set(polygon.getVertex(index)); return true; }
/** * Packs the closest vertex of the polygon to the given line. */ public static boolean getClosestVertex(Line2d line, ConvexPolygon2d polygon, Point2d pointToPack) { int index = getClosestVertexIndex(line, polygon); if (index < 0) return false; pointToPack.set(polygon.getVertex(index)); return true; }
return; int closestVertexIndex = getClosestVertexIndex(pointToProject, polygon);
/** * Packs the point on the polygon that is closest to the given ray. If the ray is parallel to the * closest edge this will return the point on that edge closest to the ray origin. If the ray * intersects the polygon the result of this method will be wrong. If unsure check first using the * intersectionWithRay method. */ public static boolean getClosestPointToRay(Line2d ray, Point2d pointToPack, ConvexPolygon2d polygon) { if (!polygon.hasAtLeastOneVertex()) return false; Point2d rayStart = ray.getPoint(); Vector2d rayDirection = ray.getNormalizedVector(); int closestVertexIndex = getClosestVertexIndex(ray, polygon); Point2d closestVertexToLine = polygon.getVertex(closestVertexIndex); // validate the closest vertex is in front of the ray: boolean vertexValid = isPointInFrontOfRay(rayStart, rayDirection, closestVertexToLine); // check edges adjacent to the closest vertex to determine if they are parallel to the ray: boolean edge1Parallel = isEdgeParallel(closestVertexIndex, rayDirection, polygon); boolean edge2Parallel = isEdgeParallel(polygon.getNextVertexIndex(closestVertexIndex), rayDirection, polygon); boolean rayParallelToEdge = edge1Parallel || edge2Parallel; if (vertexValid && !rayParallelToEdge) pointToPack.set(closestVertexToLine); else { pointToPack.set(rayStart); orthogonalProjection(pointToPack, polygon); } return true; }