in commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/internal/EuclideanUtils.java [52:82]
public static <T> List<T> convexPolygonToTriangleFan(final List<Vector3D> vertices,
final Function<List<Vector3D>, T> fn) {
final int size = vertices.size();
if (size < TRIANGLE_VERTEX_COUNT) {
throw new IllegalArgumentException("Cannot create triangle fan: " + TRIANGLE_VERTEX_COUNT +
" or more vertices are required but found only " + vertices.size());
} else if (size == TRIANGLE_VERTEX_COUNT) {
return Collections.singletonList(fn.apply(vertices));
}
final List<T> triangles = new ArrayList<>(size - 2);
final int fanIdx = findBestTriangleFanIndex(vertices);
int vertexIdx = (fanIdx + 1) % size;
final Vector3D fanBase = vertices.get(fanIdx);
Vector3D vertexA = vertices.get(vertexIdx);
Vector3D vertexB;
vertexIdx = (vertexIdx + 1) % size;
while (vertexIdx != fanIdx) {
vertexB = vertices.get(vertexIdx);
triangles.add(fn.apply(Arrays.asList(fanBase, vertexA, vertexB)));
vertexA = vertexB;
vertexIdx = (vertexIdx + 1) % size;
}
return triangles;
}