Files
OpenNest/OpenNest.Core/Shapes/NgonShape.cs
T
aj 49603eddf9 fix(geometry): nest the same on Windows and Linux
Geometry, nesting and import code now call PortableMath instead of
System.Math for Sin, Cos, Tan, Atan, Atan2, Asin and Acos (Core, Engine,
IO, Data; 269 calls in 64 files). A test fails if those projects call the
runtime's trig again.

With the runtime's trig, Windows and Linux disagreed in the last bit at
non-quarter angles and the same job could nest differently: the
learned-angle nests (a cross fixture) placed 23 parts in Strip on Windows
and 28 on Linux. Now every Engine.Tests pin gives the same layout on both
(3600 passing on Linux and on DesktopPC), Linux layouts are unchanged, and
the learned-angle rows are re-pinned to the shared layouts, which pass the
overlap and work-area checks, and run on every platform again.

Fixes Project Memory 518f3b58.
2026-11-09 19:18:00 -05:00

56 lines
1.7 KiB
C#

using System.Collections.Generic;
using OpenNest.Geometry;
using OpenNest.Math;
namespace OpenNest.Shapes
{
public class NgonShape : ShapeDefinition
{
public int Sides { get; set; }
public double Width { get; set; }
public override string GenerateName() => $"{Sides}-Sided Polygon {Dim(Width)}";
public override void SetPreviewDefaults()
{
Sides = 8;
Width = 8;
}
public override IReadOnlyList<ShapeDimension> GetDimensions() =>
new[]
{
// Across the flats: the flat at angle 0 and the one opposite it.
ShapeDimension.Linear(nameof(Width), new Vector(0, Width / 2.0), new Vector(Width, Width / 2.0), ShapeDimension.Down),
};
public override Drawing GetDrawing()
{
var n = Sides < 3 ? 3 : Sides;
var center = Width / 2.0;
var circumRadius = Width / (2.0 * PortableMath.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 * PortableMath.Cos(angle),
center + circumRadius * PortableMath.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);
}
}
}