From ceb29cc42fb126cac294d3d8be32c123c1d03248 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Fri, 26 Jan 2024 19:01:53 +0300 Subject: [PATCH] feat: Shape.CreateNgon --- Engine.Physics2D/Primitives/Shape.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Engine.Physics2D/Primitives/Shape.cs b/Engine.Physics2D/Primitives/Shape.cs index 5dbc804..5aa4c10 100644 --- a/Engine.Physics2D/Primitives/Shape.cs +++ b/Engine.Physics2D/Primitives/Shape.cs @@ -12,6 +12,22 @@ public record Shape(IList Vertices) : IEnumerable public static Shape CreateCopy(Shape shape) => new(new List(shape.Vertices)); + public static Shape CreateNgon(int vertexCount) => CreateNgon(vertexCount, Vector2D.Up); + public static Shape CreateNgon(int vertexCount, Vector2D positionToRotate) + { + if (vertexCount < 3) + throw new System.ArgumentException($"{nameof(vertexCount)} must have a value of more than 2."); + + List vertices = new(vertexCount); + + float radiansPerVertex = 360f / vertexCount * Math.DegreeToRadian; + + for (int i = 0; i < vertexCount; i++) + vertices.Add(positionToRotate.Rotate(i * radiansPerVertex)); + + return new(vertices); + } + public static Triangle GetSuperTriangle(Shape shape) { float minX = float.MaxValue, minY = float.MaxValue;