feat: Shape2D convex triangulation methods added

This commit is contained in:
Syntriax 2025-04-05 14:56:06 +03:00
parent 5756b96002
commit 7f93d95f6b

View File

@ -99,6 +99,31 @@ public readonly struct Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
return new Triangle(p1, p2, p3);
}
/// <summary>
/// Triangulates the given convex shape.
/// </summary>
/// <param name="shape">The shape to triangulate.</param>
/// <param name="triangles">The list to populate with triangles.</param>
public static void TriangulateConvex(Shape2D shape, IList<Triangle> triangles)
{
triangles.Clear();
for (int i = 2; i < shape.Vertices.Count; i++)
triangles.Add(new Triangle(shape[0], shape[i - 1], shape[i]));
}
/// <summary>
/// Triangulates the given convex shape.
/// </summary>
/// <param name="shape">The shape to triangulate.</param>
/// <returns>A list of <see cref="Triangle"/>s that makes up the given convex <see cref="Shape2D"/>.</returns>
public static List<Triangle> TriangulateConvex(Shape2D shape)
{
List<Triangle> triangles = new(shape.Vertices.Count - 2);
TriangulateConvex(shape, triangles);
return triangles;
}
/// <summary>
/// Gets the lines that form the edges of the shape.
/// </summary>
@ -229,6 +254,12 @@ public static class Shape2DExtensions
/// <inheritdoc cref="Shape2D.GetSuperTriangle(Shape2D)" />
public static Triangle ToSuperTriangle(this Shape2D shape) => Shape2D.GetSuperTriangle(shape);
/// <inheritdoc cref="Shape2D.TriangulateConvex(Shape2D, IList{Triangle})" />
public static void ToTrianglesConvex(this Shape2D shape, IList<Triangle> lines) => Shape2D.TriangulateConvex(shape, lines);
/// <inheritdoc cref="Shape2D.TriangulateConvex(Shape2D)" />
public static List<Triangle> ToTrianglesConvex(this Shape2D shape) => Shape2D.TriangulateConvex(shape);
/// <inheritdoc cref="Shape2D.GetLines(Shape2D, IList{Line2D})" />
public static void ToLines(this Shape2D shape, IList<Line2D> lines) => Shape2D.GetLines(shape, lines);