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>
This commit is contained in:
2026-04-15 13:42:02 -04:00
parent 6fdf0ad3c5
commit 9b84508ff4
3 changed files with 63 additions and 41 deletions
@@ -3,33 +3,38 @@ using System.Collections.Generic;
namespace OpenNest.Shapes namespace OpenNest.Shapes
{ {
public class OctagonShape : ShapeDefinition public class NgonShape : ShapeDefinition
{ {
public int Sides { get; set; }
public double Width { get; set; } public double Width { get; set; }
public override void SetPreviewDefaults() public override void SetPreviewDefaults()
{ {
Sides = 8;
Width = 8; Width = 8;
} }
public override Drawing GetDrawing() public override Drawing GetDrawing()
{ {
var n = Sides < 3 ? 3 : Sides;
var center = Width / 2.0; var center = Width / 2.0;
var circumRadius = Width / (2.0 * System.Math.Cos(System.Math.PI / 8.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[8]; var vertices = new Vector[n];
for (var i = 0; i < 8; i++) for (var i = 0; i < n; i++)
{ {
var angle = System.Math.PI / 8.0 + i * System.Math.PI / 4.0; var angle = start + i * step;
vertices[i] = new Vector( vertices[i] = new Vector(
center + circumRadius * System.Math.Cos(angle), center + circumRadius * System.Math.Cos(angle),
center + circumRadius * System.Math.Sin(angle)); center + circumRadius * System.Math.Sin(angle));
} }
var entities = new List<Entity>(); var entities = new List<Entity>();
for (var i = 0; i < 8; i++) for (var i = 0; i < n; i++)
{ {
var next = (i + 1) % 8; var next = (i + 1) % n;
entities.Add(new Line(vertices[i], vertices[next])); entities.Add(new Line(vertices[i], vertices[next]));
} }
+51
View File
@@ -0,0 +1,51 @@
using OpenNest.Shapes;
namespace OpenNest.Tests.Shapes;
public class NgonShapeTests
{
[Fact]
public void GetDrawing_Octagon_BoundingBoxFitsWithinExpectedSize()
{
var shape = new NgonShape { Sides = 8, Width = 20 };
var drawing = shape.GetDrawing();
var bbox = drawing.Program.BoundingBox();
// Corner-to-corner is larger than flat-to-flat
Assert.True(bbox.Width >= 20 - 0.01);
Assert.True(bbox.Length >= 20 - 0.01);
// But should not be wildly larger (corner-to-corner ~ width / cos(22.5deg) ~ width * 1.0824)
Assert.True(bbox.Width < 22);
Assert.True(bbox.Length < 22);
}
[Theory]
[InlineData(3)]
[InlineData(4)]
[InlineData(5)]
[InlineData(6)]
[InlineData(8)]
[InlineData(12)]
public void GetDrawing_HasOneLinearMovePerSide(int sides)
{
var shape = new NgonShape { Sides = sides, Width = 20 };
var drawing = shape.GetDrawing();
var moves = drawing.Program.Codes
.OfType<OpenNest.CNC.LinearMove>()
.Count();
Assert.Equal(sides, moves);
}
[Fact]
public void GetDrawing_ClampsSidesBelowThreeToTriangle()
{
var shape = new NgonShape { Sides = 2, Width = 20 };
var drawing = shape.GetDrawing();
var moves = drawing.Program.Codes
.OfType<OpenNest.CNC.LinearMove>()
.Count();
Assert.Equal(3, moves);
}
}
@@ -1,34 +0,0 @@
using OpenNest.Shapes;
namespace OpenNest.Tests.Shapes;
public class OctagonShapeTests
{
[Fact]
public void GetDrawing_BoundingBoxFitsWithinExpectedSize()
{
var shape = new OctagonShape { Width = 20 };
var drawing = shape.GetDrawing();
var bbox = drawing.Program.BoundingBox();
// Corner-to-corner is larger than flat-to-flat
Assert.True(bbox.Width >= 20 - 0.01);
Assert.True(bbox.Length >= 20 - 0.01);
// But should not be wildly larger (corner-to-corner ~ width / cos(22.5deg) ~ width * 1.0824)
Assert.True(bbox.Width < 22);
Assert.True(bbox.Length < 22);
}
[Fact]
public void GetDrawing_HasEightEdges()
{
var shape = new OctagonShape { Width = 20 };
var drawing = shape.GetDrawing();
// An octagon program should have 8 linear moves (one per edge)
var moves = drawing.Program.Codes
.OfType<OpenNest.CNC.LinearMove>()
.Count();
Assert.Equal(8, moves);
}
}