Files
OpenNest/OpenNest.Core/Shapes/NgonShape.cs
T
aj 9b84508ff4 refactor(shapes): generalize OctagonShape to NgonShape
Parameterize side count so users can generate any regular n-gon
(n>=3). Width remains the inscribed-circle diameter, preserving n=8
behavior; circumradius derives as Width / (2*cos(pi/n)).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 13:42:02 -04:00

45 lines
1.2 KiB
C#

using OpenNest.Geometry;
using System.Collections.Generic;
namespace OpenNest.Shapes
{
public class NgonShape : ShapeDefinition
{
public int Sides { get; set; }
public double Width { get; set; }
public override void SetPreviewDefaults()
{
Sides = 8;
Width = 8;
}
public override Drawing GetDrawing()
{
var n = Sides < 3 ? 3 : Sides;
var center = Width / 2.0;
var circumRadius = Width / (2.0 * System.Math.Cos(System.Math.PI / n));
var step = 2.0 * System.Math.PI / n;
var start = System.Math.PI / n;
var vertices = new Vector[n];
for (var i = 0; i < n; i++)
{
var angle = start + i * step;
vertices[i] = new Vector(
center + circumRadius * System.Math.Cos(angle),
center + circumRadius * System.Math.Sin(angle));
}
var entities = new List<Entity>();
for (var i = 0; i < n; i++)
{
var next = (i + 1) % n;
entities.Add(new Line(vertices[i], vertices[next]));
}
return CreateDrawing(entities);
}
}
}