@logomarks/generate 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,126 +0,0 @@
1
- /**
2
- * Tri: a grid of equilateral triangles, the cells inside a big triangle switched on by a numbered layout
3
- * and made mirror-symmetric if asked (a layout that comes out as three like pieces round the centre, a known game's
4
- * emblem or the radiation trefoil, or a triangle with a triangle hole at its middle, is drawn again from its next
5
- * sub-seed), parted by the house gap or merged into one figure. Never a full
6
- * hexagram (two crossed triangles, a religious symbol), and at least three rows: two rows with the middle cell out is a
7
- * known game's triangle emblem.
8
- */
9
- import { U, poly } from '@logomarks/core/geometry';
10
- import { BOX, GAP, tidy } from '../draw.js';
11
- import { rng } from '../random.js';
12
-
13
- /** @type {import('../index.js').Control[]} */
14
- export const controls = [
15
- { key: 'size', label: 'Rows (triangle)', type: 'range', min: 3, max: 4, step: 1, value: 3 },
16
- { key: 'layout', label: 'Layout no.', type: 'range', min: 1, max: 999, step: 1, value: 2 },
17
- { key: 'density', label: 'Density', type: 'range', min: 0.35, max: 0.9, step: 0.01, value: 0.65 },
18
- { key: 'symmetry', label: 'Symmetry', type: 'choice', options: ['mirror', 'none'], value: 'mirror' },
19
- { key: 'parted', label: 'Parted', type: 'toggle', value: false },
20
- ];
21
-
22
- /** The triangle cells (a, b, c corners in lattice coords) inside a big triangle of `n` rows, apex up. */
23
- function triangleCells(n) {
24
- const out = [];
25
- for (let row = 0; row < n; row++) for (let i = 0; i <= 2 * row; i++) out.push({ row, i, up: i % 2 === 0 });
26
- return out;
27
- }
28
-
29
- /** A ring's area and centre (vertex mean; fine for the convex cells here). */
30
- function measure(ring) {
31
- let a = 0;
32
- for (let i = 0; i < ring.length; i++) { const [x0, y0] = ring[i], [x1, y1] = ring[(i + 1) % ring.length]; a += x0 * y1 - x1 * y0; }
33
- const c = ring.reduce((s, [x, y]) => [s[0] + x / ring.length, s[1] + y / ring.length], [0, 0]);
34
- return { a: Math.abs(a / 2), c };
35
- }
36
-
37
- /**
38
- * Whether a drawn grid is a banned read: three like pieces spaced round the centre about 120° apart (the three-triangle
39
- * emblem of a known game, or the radiation trefoil), or one piece whose only hole is a triangle at its middle (the same
40
- * emblem merged).
41
- */
42
- function emblem(shape) {
43
- if (!shape.length) return false;
44
- if (shape.length === 3) {
45
- const m = shape.map((poly) => measure(poly[0])), areas = m.map((x) => x.a);
46
- if (Math.max(...areas) / Math.min(...areas) > 1.4) return false;
47
- const c = m.reduce((s, x) => [s[0] + x.c[0] / 3, s[1] + x.c[1] / 3], [0, 0]);
48
- const r = m.map((x) => Math.hypot(x.c[0] - c[0], x.c[1] - c[1])), ang = m.map((x) => Math.atan2(x.c[1] - c[1], x.c[0] - c[0])).sort((a, b) => a - b);
49
- const gaps = [ang[1] - ang[0], ang[2] - ang[1], 2 * Math.PI + ang[0] - ang[2]].map((g) => (g * 180) / Math.PI);
50
- return Math.max(...r) / Math.max(1e-9, Math.min(...r)) < 1.35 && gaps.every((g) => Math.abs(g - 120) < 25);
51
- }
52
- if (shape.length === 1 && shape[0].length === 2) {
53
- const outer = measure(shape[0][0]), hole = measure(shape[0][1]);
54
- return hole.a > 0.08 * outer.a && Math.hypot(hole.c[0] - outer.c[0], hole.c[1] - outer.c[1]) < 0.12 * Math.sqrt(outer.a) && shape[0][1].length <= 6;
55
- }
56
- return false;
57
- }
58
-
59
- /** The mark for resolved params: a layout that draws a banned read is drawn again from its next sub-seed. */
60
- export function build(p) {
61
- for (let attempt = 0; attempt < 8; attempt++) {
62
- const s = draw(p, attempt);
63
- if (s && !emblem(s)) return s;
64
- }
65
- return [];
66
- }
67
-
68
- /**
69
- * Whether a big triangle's cells (on/off, row by row) make the three-triangle emblem: an apex-up pair of rows with its
70
- * three up cells on and its middle down cell off (merged: anywhere; parted: holding all but at most two of the cells
71
- * that are on); or every up
72
- * cell on and every down cell off (the Sierpiński look, the same emblem grown).
73
- */
74
- function triforce(on, n, parted) {
75
- const idx = (row, i) => row * row + i, count = on.filter(Boolean).length;
76
- let allUp = true;
77
- for (let row = 0; row < n; row++) for (let i = 0; i <= 2 * row; i++) if (on[idx(row, i)] !== (i % 2 === 0)) allUp = false;
78
- if (allUp) return true;
79
- for (let row = 0; row + 1 < n; row++) for (let i = 0; i <= 2 * row; i += 2) {
80
- const ups = [idx(row, i), idx(row + 1, i), idx(row + 1, i + 2)], down = idx(row + 1, i + 1);
81
- // merged, any such triangular hole ringed by three triangles is the emblem; parted, only when it is nearly all there is
82
- if (ups.every((k) => on[k]) && !on[down] && (!parted || count - 3 <= 2)) return true;
83
- }
84
- return false;
85
- }
86
-
87
- /** One drawing of the grid; attempt 0 is the layout's own seed (so layouts that pass draw as they always have). */
88
- function draw(p, attempt) {
89
- // a parted triangle under about 110 units a side is needle-thin once the gap is taken: the big triangle takes 3–4 rows
90
- const n = p.size, seedKey = '0', rand = rng(attempt ? `tri:${p.layout}:${n}:${seedKey}:${attempt}` : `tri:${p.layout}:${n}:${seedKey}`), g = p.parted ? GAP : 0;
91
- const side = BOX.size, h = (side * Math.sqrt(3)) / 2;
92
- const cellW = side / n, cellH = h / n;
93
- // the big triangle's cells, as points, apex at the top centre
94
- const triCells = triangleCells(n).map(({ row, i, up }) => {
95
- const xL = -row * cellW / 2 + (i / 2) * cellW, y0 = row * cellH, y1 = y0 + cellH;
96
- return up ? [[xL, y1], [xL + cellW / 2, y0], [xL + cellW, y1]] : [[xL, y0], [xL + cellW, y0], [xL + cellW / 2, y1]];
97
- });
98
- const groups = 1; // a hexagon of six triangles makes a hexagram or a nut: the big triangle only
99
- const cells = [];
100
- for (let gIdx = 0; gIdx < groups; gIdx++) {
101
- const a = (gIdx * Math.PI) / 3;
102
- for (const [ci, pts] of triCells.entries()) {
103
- const placed = pts.map(([x, y]) => groups === 1 ? [BOX.cx + x, BOX.cy - h / 2 + y + (BOX.size - h) / 2 - (BOX.size - h) / 2] : [x * Math.cos(a) - (y - 0) * Math.sin(a), x * Math.sin(a) + y * Math.cos(a)]);
104
- cells.push({ g: gIdx, ci, pts: groups === 1 ? placed : placed.map(([x, y]) => [BOX.cx + x, BOX.cy + y]) });
105
- }
106
- }
107
- // on or off: drawn per cell of one group, then copied by the symmetry
108
- const base = triCells.map(() => rand() < p.density);
109
- const mirrorOf = (ci) => { // the cell mirrored left-right within the big triangle
110
- let k = 0; for (let row = 0; row < n; row++) for (let i = 0; i <= 2 * row; i++, k++) if (k === ci) { const mi = 2 * row - i; return row * row + mi; }
111
- return ci;
112
- };
113
- const on = cells.map(({ g: gi, ci }) => {
114
- let v = base[ci];
115
- if (p.symmetry === 'mirror') v = base[Math.min(ci, mirrorOf(ci))];
116
- if (groups === 6 && p.symmetry !== 'none') v = base[Math.min(ci, mirrorOf(ci))];
117
- return v;
118
- });
119
- if (triforce(on, n, !!p.parted)) return null;
120
- const shrink = (pts) => { if (!g) return pts; const c = pts.reduce((s, q) => [s[0] + q[0] / 3, s[1] + q[1] / 3], [0, 0]); const k = 1 - (g * Math.sqrt(3)) / cellW; return pts.map(([x, y]) => [c[0] + (x - c[0]) * k, c[1] + (y - c[1]) * k]); };
121
- const parts = cells.filter((_, k) => on[k]).map(({ pts }) => poly(shrink(pts)));
122
- return parts.length ? tidy(U(...parts), { share: 0.003, width: 10 }) : [];
123
- }
124
-
125
- /** A grid of triangles is many pieces. */
126
- export const guard = { maxPieces: 60, minPieceShare: 0.003, maxThinShare: 0.1 }; // 60° corners by design