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: reject star polygons as non-convex prisms

Rudy Celeklid2b1646983c1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8c9e657..7e715c6 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
+- `T.prism` rejects a star polygon such as a pentagram, which turns the same way at every vertex but winds twice, with the same "split it into convex pieces" error as any other non-convex part.
- `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..fdb2bf2 100644
--- a/kit/turn-build.mjs
+++ b/kit/turn-build.mjs
@@ -58,9 +58,11 @@ function signedArea(points) {
return s / 2;
}
-function convexOrThrow(points, name) {
+function convexOrThrow(ring, name) {
+ const points = ring.filter((p, i) => Math.hypot(p[0] - ring[(i + ring.length - 1) % ring.length][0], p[1] - ring[(i + ring.length - 1) % ring.length][1]) > 1e-9);
const n = points.length;
let sign = 0;
+ let turned = 0;
for (let i = 0; i < n; i++) {
const a = points[i];
const b = points[(i + 1) % n];
@@ -69,7 +71,9 @@ function convexOrThrow(points, name) {
if (Math.abs(cross) < 1e-9) continue;
if (!sign) sign = Math.sign(cross);
else if (Math.sign(cross) !== sign) throw new Error(`turning: part ${name} is not convex; split it into convex pieces`);
+ turned += Math.atan2(cross, (b[0] - a[0]) * (c[0] - b[0]) + (b[1] - a[1]) * (c[1] - b[1]));
}
+ if (sign && Math.abs(Math.abs(turned) - TAU) > 1e-6) throw new Error(`turning: part ${name} is not convex; split it into convex pieces`);
}
export function roundedPolygon(polygon, radius = 0, steps = 4) {
diff --git a/scripts/turn-convex-check.test.mjs b/scripts/turn-convex-check.test.mjs
new file mode 100644
index 0000000..c2329ba
--- /dev/null
+++ b/scripts/turn-convex-check.test.mjs
@@ -0,0 +1,30 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import * as G from "../kit/lathe.mjs";
+import { turning } from "../kit/turn-build.mjs";
+const prism = (polygon, options = {}) => {
+ 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]);
+ return T.prism({ name: "part", F, s0: 0, s1: 2, polygon, ...options });
+};
+const regular = (count, step, radius = 3) => Array.from({ length: count }, (_, i) => [radius * Math.cos(Math.PI / 2 + i * step), radius * Math.sin(Math.PI / 2 + i * step)]);
+test("a pentagram is not a convex prism", () => {
+ assert.throws(() => prism(regular(5, (4 * Math.PI) / 5)), /part is not convex/);
+});
+test("a heptagram is not a convex prism, in either winding", () => {
+ assert.throws(() => prism(regular(7, (4 * Math.PI) / 7)), /part is not convex/);
+ assert.throws(() => prism(regular(7, (4 * Math.PI) / 7).reverse()), /part is not convex/);
+});
+test("a bowtie and an L shape are still rejected", () => {
+ assert.throws(() => prism([[0, 0], [2, 2], [2, 0], [0, 2]]), /part is not convex/);
+ assert.throws(() => prism([[0, 0], [4, 0], [4, 1], [1, 1], [1, 4], [0, 4]]), /part is not convex/);
+});
+test("convex polygons are still accepted, whichever way they wind", () => {
+ for (const polygon of [regular(3, (2 * Math.PI) / 3), regular(5, (2 * Math.PI) / 5), regular(12, (2 * Math.PI) / 12), regular(6, (2 * Math.PI) / 6).reverse(), [[0, 0], [5, 0], [10, 0], [10, 10], [0, 10]]]) {
+ assert.equal(prism(polygon).world.ring.length, polygon.length);
+ }
+ assert.ok(prism([[0, 0], [10, 0], [10, 10], [0, 10]], { round: 2, steps: 6 }).world.ring.length > 4);
+});
+test("a repeated vertex does not make a convex polygon look concave", () => {
+ assert.equal(prism([[0, 0], [4, 0], [4, 0], [4, 4], [0, 4], [0, 0]]).world.ring.length, 6);
+});