From 7f93d95f6bc680fa804f7f973c3248d08708875a Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sat, 5 Apr 2025 14:56:06 +0300 Subject: [PATCH] feat: Shape2D convex triangulation methods added --- Engine.Core/Primitives/Shape2D.cs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Engine.Core/Primitives/Shape2D.cs b/Engine.Core/Primitives/Shape2D.cs index 59c894c..19866c7 100644 --- a/Engine.Core/Primitives/Shape2D.cs +++ b/Engine.Core/Primitives/Shape2D.cs @@ -99,6 +99,31 @@ public readonly struct Shape2D(List vertices) : IEnumerable return new Triangle(p1, p2, p3); } + /// + /// Triangulates the given convex shape. + /// + /// The shape to triangulate. + /// The list to populate with triangles. + public static void TriangulateConvex(Shape2D shape, IList triangles) + { + triangles.Clear(); + + for (int i = 2; i < shape.Vertices.Count; i++) + triangles.Add(new Triangle(shape[0], shape[i - 1], shape[i])); + } + + /// + /// Triangulates the given convex shape. + /// + /// The shape to triangulate. + /// A list of s that makes up the given convex . + public static List TriangulateConvex(Shape2D shape) + { + List triangles = new(shape.Vertices.Count - 2); + TriangulateConvex(shape, triangles); + return triangles; + } + /// /// Gets the lines that form the edges of the shape. /// @@ -229,6 +254,12 @@ public static class Shape2DExtensions /// public static Triangle ToSuperTriangle(this Shape2D shape) => Shape2D.GetSuperTriangle(shape); + /// + public static void ToTrianglesConvex(this Shape2D shape, IList lines) => Shape2D.TriangulateConvex(shape, lines); + + /// + public static List ToTrianglesConvex(this Shape2D shape) => Shape2D.TriangulateConvex(shape); + /// public static void ToLines(this Shape2D shape, IList lines) => Shape2D.GetLines(shape, lines);