Skip to content

An agent skill for creating detailed, interactive isometric illustrations of real-world machines and complex systems. Generates polished SVGs with optional WebGL shaders and 3D effects.

turn-build: ignore repeated vertices when rounding a prism polygon

Rudy Celeklifd788d75e6bd
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8c9e657..37794a0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@ All notable changes to Anatomy are recorded here. The format follows [Keep a Cha
### Fixed
+- A repeated vertex in a `T.prism` polygon no longer pushes a rounded corner outside the polygon: `roundedPolygon` drops consecutive duplicate points before it rounds.
- `capture.mjs` waits for the page to load before it touches it, even with `--wait 0`, and gives up with an error when Chrome leaves a request unanswered for 60 s instead of hanging.
- `drive.mjs` `until` times a condition inside the page, so one that is already true passes however short the wait, even on a slow machine; a promise that never settles still times out.
- Start headless Chrome on Windows from an elevated terminal: `capture.mjs`, `drive.mjs` and `turn-check.mjs` pass `--do-not-de-elevate` there. Chrome otherwise relaunches itself without the elevation, the process the script started exits with code 0, and the check stops with `Chrome exited before startup: 0` while the relaunched browser stays running.
diff --git a/kit/turn-build.mjs b/kit/turn-build.mjs
index 3bfe31b..ea05502 100644
--- a/kit/turn-build.mjs
+++ b/kit/turn-build.mjs
@@ -73,8 +73,12 @@ function convexOrThrow(points, name) {
}
export function roundedPolygon(polygon, radius = 0, steps = 4) {
- const poly = signedArea(polygon) < 0 ? polygon.slice().reverse() : polygon.slice();
- if (!(radius > 0)) return poly.map(([x, y]) => [x, y]);
+ const ordered = signedArea(polygon) < 0 ? polygon.slice().reverse() : polygon.slice();
+ if (!(radius > 0)) return ordered.map(([x, y]) => [x, y]);
+ const same = (p, q) => Math.hypot(p[0] - q[0], p[1] - q[1]) <= 1e-9;
+ const poly = ordered.filter((p, i) => i === 0 || !same(p, ordered[i - 1]));
+ while (poly.length > 1 && same(poly[0], poly[poly.length - 1])) poly.pop();
+ if (poly.length < 3) return poly.map(([x, y]) => [x, y]);
const n = poly.length;
const normals = poly.map((a, i) => {
const b = poly[(i + 1) % n];
diff --git a/scripts/turn-rounded-polygon.test.mjs b/scripts/turn-rounded-polygon.test.mjs
new file mode 100644
index 0000000..66ea360
--- /dev/null
+++ b/scripts/turn-rounded-polygon.test.mjs
@@ -0,0 +1,25 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import * as G from "../kit/lathe.mjs";
+import { roundedPolygon, turning } from "../kit/turn-build.mjs";
+const square = [[0, 0], [10, 0], [10, 10], [0, 10]];
+const extent = (ring, axis) => [Math.min(...ring.map((p) => p[axis])), Math.max(...ring.map((p) => p[axis]))];
+test("a repeated vertex does not change the rounded outline", () => {
+ const expected = roundedPolygon(square, 1, 2);
+ assert.equal(expected.length, 12);
+ assert.deepEqual(roundedPolygon([[0, 0], [10, 0], [10, 0], [10, 10], [0, 10]], 1, 2), expected);
+ assert.deepEqual(roundedPolygon([[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], 1, 2), expected);
+ assert.deepEqual(roundedPolygon([[0, 0], [0, 0], [0, 0], [10, 0], [10, 10], [0, 10]], 1, 2), expected);
+});
+test("a repeated vertex keeps the rounded outline inside the polygon", () => {
+ const ring = roundedPolygon([[0, 0], [10, 0], [10, 0], [10, 10], [0, 10]], 1, 2);
+ assert.deepEqual(extent(ring, 0), [0, 10]);
+ assert.deepEqual(extent(ring, 1), [0, 10]);
+});
+test("a prism with a repeated vertex and a corner radius stays inside its polygon", () => {
+ const T = turning({ origin: [0, 0], scale: 1, azimuth: 45, elevation: 30 }); T.layer("all");
+ const F = G.frameAlong([0, 0, 0], [0, 0, 1]);
+ const part = T.prism({ name: "block", F, s0: 0, s1: 2, polygon: [[0, 0], [4, 0], [4, 0], [4, 4], [0, 4]], round: 1 });
+ assert.deepEqual(extent(part.world.ring, 0), [0, 4]);
+ assert.deepEqual(extent(part.world.ring, 1), [0, 4]);
+});