azulejeria 2.2.0 → 2.4.0

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.
package/README.md CHANGED
@@ -19,6 +19,8 @@ rosettes and Mudéjar strapwork), [Jerez](https://stepinto.dev/art/jerez.html)
19
19
  viewer, a canvas you paint any world into, with the drag, the pinch, the
20
20
  wheel and the page chrome; [Alicatado](https://stepinto.dev/art/alicatado.html)
21
21
  (cut tile set in lime) is the viewer with the cutter below,
22
+ [Papel Pintado](https://stepinto.dev/art/papelpintado.html) is the wall laid
23
+ by any of the seventeen wallpaper groups,
22
24
  [Trencadís](https://stepinto.dev/art/trencadis.html) is the viewer breaking
23
25
  Azulejería's tiles with it, and
24
26
  [Knotwork](https://stepinto.dev/art/knotwork.html) is the viewer with the knots. The design is
@@ -83,9 +85,12 @@ tap. Drive it from your own controls through `wall.state` and
83
85
 
84
86
  ## The parts
85
87
 
86
- **`createWall(canvas, { palettes, motifs, painters, presets, material })`** returns
88
+ **`createWall(canvas, { palettes, motifs, painters, presets, material, variants })`** returns
87
89
  the wall. `wall.state` holds `seed`, `repeat` (`module`, `rotate`,
88
- `single` or `checker`), `pal`, `motif`, `density` (1 to 5), `hand` (0 to
90
+ `single`, `checker`, `whole` for a tile that is a picture, every one
91
+ laid the same way up, or any of the seventeen wallpaper groups by name,
92
+ `p1` to `p6m`, see Groups below; `variants` is how many paintings of the design
93
+ such a wall draws from, four by default), `pal`, `motif`, `density` (1 to 5), `hand` (0 to
89
94
  1, how far the brush drifts), `brush` (line weight), `scale` (pixels per
90
95
  tile), `panX`, `panY`, `bands`, `grout` and `age`. After changing any of
91
96
  them call `wall.rebuild(true)` to repaint the tiles, or `wall.rebuild(false)`
@@ -181,7 +186,7 @@ diamond), `ring` (a scalloped ring around the main corner), `lattice` (a
181
186
  line edge to edge that mirrors into a lozenge net) and `dots`.
182
187
 
183
188
  **The lower level** is there if you want it: `makeDesign(seed, density,
184
- bands, bold, motif)`, `paintDesign(g, els, pal, hand, lw, seed, painters)`,
189
+ bands, bold, motif)`, `paintDesign(g, els, pal, hand, lw, seed, painters, material, brush)`,
185
190
  `makeTile(opts, size, pixelRatio, seed)`, `tileOpts(cfg, state)` and
186
191
  `paintThumb(cfg, preset, g, size, pixelRatio)`.
187
192
 
@@ -223,6 +228,39 @@ a small speckle tile for mortar. `centroid`, `signedArea`,
223
228
  `ensureCCW`, `regularPoly` and `pathPoly` are the small polygon maths
224
229
  every page wanted.
225
230
 
231
+ ## Groups
232
+
233
+ `azulejeria/groups` is the seventeen ways a wall can repeat: the
234
+ wallpaper groups, for [Papel Pintado](https://stepinto.dev/art/papelpintado.html)
235
+ and for any wall that sets `state.repeat` to one of their names. Every
236
+ repeating pattern in the plane has one of these symmetries, and each
237
+ is given here as a lattice (two vectors, in tiles), a domain (the part
238
+ of the tile that is drawn: the whole square, half of it cut on a
239
+ diagonal, or the 60° rhombus corner to corner and its thirds, sixths
240
+ and twelfths) and the copies, the isometries that lay the domain out
241
+ inside one cell. Every domain keeps the design's main corner, so what
242
+ gets repeated is the heart of the design.
243
+
244
+ ```js
245
+ import { GROUPS, GROUP_NAMES, layGroup, drawTile } from 'azulejeria/groups';
246
+
247
+ GROUP_NAMES; // ['p1', 'p2', 'pm', 'pg', 'cm', 'pmm', 'pmg', 'pgg', 'cmm', 'p4', 'p4m', 'p4g', 'p3', 'p3m1', 'p31m', 'p6', 'p6m']
248
+ const g = GROUPS.p6m; // { cell, domain, copies, lattice, note }
249
+ layGroup(ctx, g, S, gap, W, H, ox, oy, (c, t) => drawTile(c, img, S, gap));
250
+ ```
251
+
252
+ **`layGroup(ctx, group, S, gap, W, H, ox, oy, draw)`** walks every
253
+ copy in every cell a window touches, with `S` the tile in pixels,
254
+ `gap` the grout and the world's origin at `(ox, oy)`, and calls
255
+ `draw(ctx, { i, j, k, grp, M })` with the context in design
256
+ coordinates (the unit square, main corner at the origin) and clipped
257
+ to the domain less half the gap. **`drawTile(ctx, img, S, gap)`** puts
258
+ a tile image there. The matrices are plain `[a, b, c, d, e, f]`
259
+ arrays with `mul`, `rotate(th, cx, cy)`, `reflect(ang, px, py)`,
260
+ `translate` and `apply` beside them, and `inset(poly, d)` moves a
261
+ convex polygon in by `d`. The wall's `createWall` uses all of this
262
+ when `repeat` names a group, and `paintThumb` lays a card with it.
263
+
226
264
  ## Knots
227
265
 
228
266
  `azulejeria/knots` is interlace by rule, for the
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "azulejeria",
3
- "version": "2.2.0",
3
+ "version": "2.4.0",
4
4
  "description": "Tin-glazed tiles painted by rule. A seeded canvas engine for Spanish azulejo walls: a diagonal-symmetric design, brushwork that drifts, washes that pool, and four ways to repeat the tile.",
5
- "keywords": ["canvas", "generative", "tiles", "azulejo", "mudejar", "sevilla", "pattern", "procedural", "art", "wallpaper", "interlace", "knotwork", "celtic", "mosaic", "voronoi", "trencadis"],
5
+ "keywords": ["canvas", "generative", "wallpaper-groups", "symmetry", "tiles", "azulejo", "mudejar", "sevilla", "pattern", "procedural", "art", "wallpaper", "interlace", "knotwork", "celtic", "mosaic", "voronoi", "trencadis"],
6
6
  "license": "MIT",
7
7
  "author": "Andy Cross",
8
8
  "homepage": "https://stepinto.dev/art/azulejeria.html",
@@ -13,11 +13,12 @@
13
13
  ".": "./src/azulejeria.js",
14
14
  "./knots": "./src/knots.js",
15
15
  "./pieces": "./src/pieces.js",
16
+ "./groups": "./src/groups.js",
16
17
  "./azulejeria.css": "./src/azulejeria.css"
17
18
  },
18
- "files": ["src", "README.md", "LICENSE"],
19
+ "files": ["src", "test", "README.md", "LICENSE"],
19
20
  "sideEffects": false,
20
21
  "scripts": {
21
- "test": "node --check src/azulejeria.js && node --check src/knots.js && node --check src/head.js && node --check src/pieces.js"
22
+ "test": "node --check src/azulejeria.js && node --check src/knots.js && node --check src/head.js && node --check src/pieces.js && node --check src/groups.js && node test/groups.mjs"
22
23
  }
23
24
  }
package/src/azulejeria.js CHANGED
@@ -20,6 +20,13 @@
20
20
  // touching what is drawn on it: how the ground is laid, how a shape
21
21
  // is filled, how far a piece strays from the drawing, what the grout
22
22
  // is, and what age does. Without one the tile is tin-glazed.
23
+ //
24
+ // The wall's repeat is one of the four classic layings (module, rotate,
25
+ // single, checker), the whole tile laid the same way up, or any of the
26
+ // seventeen wallpaper groups, which live in groups.js.
27
+
28
+ import { GROUPS, layGroup, drawTile } from './groups.js';
29
+ export { GROUPS, GROUP_NAMES, layGroup, drawTile, inset } from './groups.js';
23
30
 
24
31
  // ------------------------------------------------------------- random ---
25
32
  export function mulberry32(a) {
@@ -219,14 +226,16 @@ export const PAINTERS = {
219
226
 
220
227
  // paints one design into the unit square of g (already scaled). The
221
228
  // material, if given, may supply `ampOf(hand)`, `shape(g, pts, col, b)`,
222
- // `line(g, pts, lw, close, b)` and `dot(g, x, y, r, col, a, b)`.
223
- export function paintDesign(g, els, pal, hand, lw, jseed, painters, material) {
229
+ // `line(g, pts, lw, close, b)` and `dot(g, x, y, r, col, a, b)`. The
230
+ // brush `b` also carries `hand` and `brush` as the state has them, for
231
+ // a painter whose line is not drawn with `lw` (a traced drawing, say).
232
+ export function paintDesign(g, els, pal, hand, lw, jseed, painters, material, brush) {
224
233
  const rng = mulberry32(jseed);
225
234
  const m = material || {};
226
235
  const amp = m.ampOf ? m.ampOf(hand) : 0.012 * hand;
227
236
  const Q = Math.PI / 2, D = Q / 2;
228
237
  const dc = Math.cos(D), ds = Math.sin(D); // the diagonal
229
- const b = { g, pal, lw, amp, rng, Q, D, dc, ds, col: pal.wash };
238
+ const b = { g, pal, lw, amp, rng, Q, D, dc, ds, col: pal.wash, hand, brush: brush === undefined ? 1 : brush };
230
239
  const shape = m.shape
231
240
  ? (pts, col) => m.shape(g, pts, col, b)
232
241
  : (pts, col) => { wash(g, pts, col, rng); ink(g, pts, lw, pal, rng, true); };
@@ -262,7 +271,9 @@ export function paintDesign(g, els, pal, hand, lw, jseed, painters, material) {
262
271
  // --------------------------------------------------------------- tile ---
263
272
  // One tile, painted. `o.quarter` lays the design four times mirrored
264
273
  // about the centre; otherwise the design fills the tile with its main
265
- // corner at bottom right, for the module to complete. `o.plain` paints
274
+ // corner at bottom right, for the module to complete (or, with the
275
+ // 'whole' repeat, every tile laid the same way up: a tile that is a
276
+ // picture). `o.plain` paints
266
277
  // the small tile a checker alternates with.
267
278
  export function makeTile(o, S, k, jseed) {
268
279
  const c = document.createElement('canvas');
@@ -305,18 +316,18 @@ export function makeTile(o, S, k, jseed) {
305
316
  let v = 0;
306
317
  for (const [fx, fy] of [[1, 1], [-1, 1], [1, -1], [-1, -1]]) {
307
318
  g.save(); g.translate(S / 2, S / 2); g.scale(fx * S / 2, fy * S / 2);
308
- paintDesign(g, o.design, pal, o.hand, 0.024 * o.brush, jseed + 17 + v++, o.painters, o.material);
319
+ paintDesign(g, o.design, pal, o.hand, 0.024 * o.brush, jseed + 17 + v++, o.painters, o.material, o.brush);
309
320
  g.restore();
310
321
  }
311
322
  } else {
312
323
  g.save(); g.translate(S, S); g.scale(-S, -S);
313
- paintDesign(g, o.design, pal, o.hand, 0.012 * o.brush, jseed + 17, o.painters, o.material);
324
+ paintDesign(g, o.design, pal, o.hand, 0.012 * o.brush, jseed + 17, o.painters, o.material, o.brush);
314
325
  g.restore();
315
326
  }
316
327
  return c;
317
328
  }
318
329
 
319
- export const REPEATS = ['module', 'rotate', 'single', 'checker'];
330
+ export const REPEATS = ['module', 'rotate', 'single', 'checker', 'whole'];
320
331
  const ROT = [0, Math.PI / 2, 3 * Math.PI / 2, Math.PI];
321
332
 
322
333
  // the tile options for a given state (preset or live)
@@ -327,10 +338,64 @@ export function tileOpts(cfg, s) {
327
338
  return { pal, design, quarter, hand: s.hand, brush: s.brush, painters: cfg.painters, material: cfg.material };
328
339
  }
329
340
 
341
+ // what age does to a glazed tile, drawn in the tile's own frame: a
342
+ // warm cast on some, a craze on a few, a chipped corner on fewer
343
+ export function ageTile(ctx, rng, s, pal) {
344
+ if (rng() < 0.5) { ctx.fillStyle = `rgba(120, 85, 30, ${0.04 + rng() * 0.1})`; ctx.fillRect(-s / 2, -s / 2, s, s); }
345
+ if (rng() < 0.18) {
346
+ ctx.strokeStyle = 'rgba(60, 45, 30, 0.35)';
347
+ ctx.lineWidth = 0.7;
348
+ const pts = linePts(-s / 2, (rng() - 0.5) * s, s / 2, (rng() - 0.5) * s, s * 0.08, rng, 14);
349
+ trace(ctx, pts, false); ctx.stroke();
350
+ if (rng() < 0.5) {
351
+ const q = linePts((rng() - 0.5) * s, -s / 2, (rng() - 0.5) * s, s / 2, s * 0.08, rng, 14);
352
+ trace(ctx, q, false); ctx.stroke();
353
+ }
354
+ }
355
+ if (rng() < 0.05) {
356
+ const cxs = rng() < 0.5 ? -1 : 1, cys = rng() < 0.5 ? -1 : 1;
357
+ ctx.fillStyle = pal.grout;
358
+ ctx.beginPath();
359
+ ctx.moveTo(cxs * s / 2, cys * s / 2);
360
+ ctx.lineTo(cxs * (s / 2 - s * (0.08 + rng() * 0.1)), cys * s / 2);
361
+ ctx.lineTo(cxs * (s / 2 - s * 0.03), cys * (s / 2 - s * 0.05));
362
+ ctx.lineTo(cxs * s / 2, cys * (s / 2 - s * (0.08 + rng() * 0.1)));
363
+ ctx.closePath(); ctx.fill();
364
+ }
365
+ }
366
+
367
+ // a wall laid by a wallpaper group: every copy of the domain in every
368
+ // cell the window touches, each straying from its place as the hand
369
+ // allows, aged if asked. `pick(rng)` chooses the tile image.
370
+ function layWall(ctx, grp, S, gap, W, H, ox, oy, state, pal, m, lay, pick) {
371
+ layGroup(ctx, grp, S, gap, W, H, ox, oy, (g, t) => {
372
+ const rng = mulberry32(hash2(t.i * 32 + t.k, t.j) + state.seed);
373
+ const img = pick(rng);
374
+ g.translate(0.5 + (rng() - 0.5) * lay.shift * state.hand, 0.5 + (rng() - 0.5) * lay.shift * state.hand);
375
+ g.rotate((rng() - 0.5) * lay.turn * state.hand);
376
+ g.translate(-0.5, -0.5);
377
+ drawTile(g, img, S, gap);
378
+ if (state.age) {
379
+ // age is drawn in pixels about the tile's centre
380
+ g.translate(0.5, 0.5); g.scale(1 / S, 1 / S);
381
+ const s = S - gap;
382
+ if (m.age) m.age(g, { i: t.i, j: t.j, k: t.k, rng, s, S, pal, state, cx: (t.i + 0.5) * S, cy: (t.j + 0.5) * S, fx: 1, fy: 1, rot: 0 });
383
+ else ageTile(g, rng, s, pal);
384
+ }
385
+ });
386
+ }
387
+
330
388
  // a small wall of four tiles, for a preset card
331
389
  export function paintThumb(cfg, p, g, T, k) {
332
390
  const o = tileOpts(cfg, p);
333
391
  const A = makeTile(o, T, k, p.seed * 31);
392
+ const grp = GROUPS[p.repeat];
393
+ if (grp) {
394
+ g.fillStyle = o.pal.grout; g.fillRect(0, 0, 2 * T, 2 * T);
395
+ const m = cfg.material || {}, gap = p.grout === false ? 0 : (m.gapOf ? m.gapOf(T, p) : Math.max(1.2, T * 0.022));
396
+ layWall(g, grp, T, gap, 2 * T, 2 * T, 0, 0, { ...p, age: false }, o.pal, m, { shift: 0, turn: 0 }, () => A);
397
+ return;
398
+ }
334
399
  const B = p.repeat === 'checker' ? makeTile({ ...o, plain: true }, T, k, p.seed * 37) : null;
335
400
  g.fillStyle = o.pal.grout; g.fillRect(0, 0, 2 * T, 2 * T);
336
401
  for (let j = 0; j < 2; j++) for (let i = 0; i < 2; i++) {
@@ -468,8 +533,10 @@ export function createViewer(canvas, opts) {
468
533
  }
469
534
 
470
535
  // --------------------------------------------------------------- wall ---
471
- // createWall(canvas, { palettes, motifs, painters, presets, material })
472
- // is a viewer that paints a wall of tiles. It returns the wall, which
536
+ // createWall(canvas, { palettes, motifs, painters, presets, material,
537
+ // variants }) is a viewer that paints a wall of tiles. `variants` is how
538
+ // many paintings of the design the wall draws from (four unless told
539
+ // otherwise; a wall of whole tiles, each its own picture, wants more). It returns the wall, which
473
540
  // is the viewer with more on it: `state` to read or change, then
474
541
  // `rebuild(true)` to repaint the tiles or `rebuild(false)` to redraw
475
542
  // the wall with the tiles it has. `applyPreset(name)`, `newDesign()`
@@ -485,13 +552,14 @@ export function createWall(canvas, cfg) {
485
552
  const painters = cfg.painters || PAINTERS;
486
553
  const material = cfg.material || null, m = material || {};
487
554
  const lay = m.lay || { shift: 0.014, turn: 0.016 };
555
+ const variants = cfg.variants || 4;
488
556
  const firstMotif = Object.keys(motifs)[0];
489
557
  for (const p of Object.values(presets)) if (!p.motif) p.motif = firstMotif;
490
558
 
491
559
  const wall = createViewer(canvas, { paint, scale: 120, minZoom: 40, maxZoom: 260 });
492
560
  const state = Object.assign(wall.state, {
493
561
  seed: 1,
494
- repeat: 'module', // module | rotate | single | checker
562
+ repeat: 'module', // module | rotate | single | checker | whole | a wallpaper group
495
563
  pal: Object.keys(palettes)[0],
496
564
  motif: firstMotif,
497
565
  density: 3, // how much of the design is drawn, 1..5
@@ -511,7 +579,7 @@ export function createWall(canvas, cfg) {
511
579
  const o = tileOpts(tcfg, state);
512
580
  tiles.A = [];
513
581
  tiles.B = [];
514
- for (let v = 0; v < 4; v++) tiles.A.push(makeTile(o, state.scale, DPR, state.seed * 31 + v));
582
+ for (let v = 0; v < variants; v++) tiles.A.push(makeTile(o, state.scale, DPR, state.seed * 31 + v));
515
583
  if (state.repeat === 'checker')
516
584
  for (let v = 0; v < 3; v++) tiles.B.push(makeTile({ ...o, plain: true }, state.scale, DPR, state.seed * 37 + v));
517
585
  needTiles = false;
@@ -526,6 +594,13 @@ export function createWall(canvas, cfg) {
526
594
 
527
595
  const S = state.scale;
528
596
  const gap = state.grout ? (m.gapOf ? m.gapOf(S, state) : Math.max(1.2, S * 0.022)) : 0;
597
+ const grp = GROUPS[state.repeat];
598
+ if (grp) {
599
+ layWall(ctx, grp, S, gap, W, H, ox, oy, state, pal, m, lay, rng => tiles.A[Math.floor(rng() * tiles.A.length)]);
600
+ if (m.finish) m.finish(ctx, W, H, state, pal);
601
+ else if (state.age) { ctx.fillStyle = 'rgba(90, 60, 20, 0.05)'; ctx.fillRect(0, 0, W, H); }
602
+ return;
603
+ }
529
604
  const i0 = Math.floor(-ox / S) - 1, i1 = Math.ceil((W - ox) / S) + 1;
530
605
  const j0 = Math.floor(-oy / S) - 1, j1 = Math.ceil((H - oy) / S) + 1;
531
606
  const s = S - gap;
@@ -546,28 +621,7 @@ export function createWall(canvas, cfg) {
546
621
  if (state.age && m.age) {
547
622
  m.age(ctx, { i, j, rng, s, S, pal, state, cx: (i + 0.5) * S, cy: (j + 0.5) * S, fx, fy, rot });
548
623
  } else if (state.age) {
549
- // a warm cast on some, a craze on a few, a chipped corner on fewer
550
- if (rng() < 0.5) { ctx.fillStyle = `rgba(120, 85, 30, ${0.04 + rng() * 0.1})`; ctx.fillRect(-s / 2, -s / 2, s, s); }
551
- if (rng() < 0.18) {
552
- ctx.strokeStyle = 'rgba(60, 45, 30, 0.35)';
553
- ctx.lineWidth = 0.7;
554
- const pts = linePts(-s / 2, (rng() - 0.5) * s, s / 2, (rng() - 0.5) * s, s * 0.08, rng, 14);
555
- trace(ctx, pts, false); ctx.stroke();
556
- if (rng() < 0.5) {
557
- const q = linePts((rng() - 0.5) * s, -s / 2, (rng() - 0.5) * s, s / 2, s * 0.08, rng, 14);
558
- trace(ctx, q, false); ctx.stroke();
559
- }
560
- }
561
- if (rng() < 0.05) {
562
- const cxs = rng() < 0.5 ? -1 : 1, cys = rng() < 0.5 ? -1 : 1;
563
- ctx.fillStyle = pal.grout;
564
- ctx.beginPath();
565
- ctx.moveTo(cxs * s / 2, cys * s / 2);
566
- ctx.lineTo(cxs * (s / 2 - s * (0.08 + rng() * 0.1)), cys * s / 2);
567
- ctx.lineTo(cxs * (s / 2 - s * 0.03), cys * (s / 2 - s * 0.05));
568
- ctx.lineTo(cxs * s / 2, cys * (s / 2 - s * (0.08 + rng() * 0.1)));
569
- ctx.closePath(); ctx.fill();
570
- }
624
+ ageTile(ctx, rng, s, pal);
571
625
  }
572
626
  ctx.restore();
573
627
  }
package/src/groups.js ADDED
@@ -0,0 +1,196 @@
1
+ // groups: the seventeen ways a wall can repeat.
2
+ //
3
+ // Every repeating pattern in the plane has one of seventeen symmetry
4
+ // groups, the wallpaper groups. Each one here is a lattice (two
5
+ // vectors, in tiles), a domain (the part of the tile that is drawn,
6
+ // a polygon in design coordinates, or null for the whole square) and
7
+ // the copies: the isometries that lay the domain out inside one cell
8
+ // of the lattice. Lay the cell at every lattice point and the plane is
9
+ // covered exactly once.
10
+ //
11
+ // Design coordinates are the unit square with the design's main corner
12
+ // at (0, 0) and its far corner at (1, 1). The square groups use the
13
+ // whole tile, or half of it cut along a diagonal; the hexagonal groups
14
+ // use the 60° rhombus whose long diagonal runs corner to corner, or a
15
+ // third, a sixth or a twelfth of it. Every domain keeps the main
16
+ // corner, so the heart of the design is what gets repeated.
17
+ //
18
+ // A matrix is [a, b, c, d, e, f] as the canvas has it: x' = ax + cy + e,
19
+ // y' = bx + dy + f. `mul(M, N)` applies N first.
20
+
21
+ export const I = [1, 0, 0, 1, 0, 0];
22
+ export function mul(M, N) {
23
+ return [
24
+ M[0] * N[0] + M[2] * N[1], M[1] * N[0] + M[3] * N[1],
25
+ M[0] * N[2] + M[2] * N[3], M[1] * N[2] + M[3] * N[3],
26
+ M[0] * N[4] + M[2] * N[5] + M[4], M[1] * N[4] + M[3] * N[5] + M[5],
27
+ ];
28
+ }
29
+ export const translate = (x, y) => [1, 0, 0, 1, x, y];
30
+ // a turn of `th` about (cx, cy)
31
+ export function rotate(th, cx, cy) {
32
+ const c = Math.cos(th), s = Math.sin(th);
33
+ return [c, s, -s, c, cx - c * cx + s * cy, cy - s * cx - c * cy];
34
+ }
35
+ // a reflection in the line through (px, py) at angle `ph`
36
+ export function reflect(ph, px, py) {
37
+ const c = Math.cos(2 * ph), s = Math.sin(2 * ph);
38
+ return [c, s, s, -c, px - c * px - s * py, py - s * px + c * py];
39
+ }
40
+ export const apply = (M, [x, y]) => [M[0] * x + M[2] * y + M[4], M[1] * x + M[3] * y + M[5]];
41
+
42
+ // the orbit of a list of matrices under another: every product
43
+ const times = (A, B) => A.flatMap(a => B.map(b => mul(b, a)));
44
+ const turns = (n, cx, cy) => Array.from({ length: n }, (_, k) => rotate(2 * Math.PI * k / n, cx, cy));
45
+
46
+ const PI = Math.PI, Q = PI / 2;
47
+ const Mx1 = reflect(Q, 1, 0); // the line x = 1
48
+ const My1 = reflect(0, 0, 1); // the line y = 1
49
+ const DIAG = reflect(PI / 4, 0, 0); // the line y = x
50
+ const ANTI = reflect(-PI / 4, 1, 0); // the line x + y = 1
51
+
52
+ // the 60° rhombus corner to corner: short diagonal s = sqrt(2/3)
53
+ const R3 = 1 / (2 * Math.sqrt(3));
54
+ const C = [0.5 + R3, 0.5 - R3], C2 = [0.5 - R3, 0.5 + R3];
55
+ const HEX_A = [1, 1], HEX_B = [0.5 - Math.sqrt(3) / 2, 0.5 + Math.sqrt(3) / 2];
56
+ const hex = k => turns(3, C[0], C[1]).flatMap(r => k.map(m => mul(r, m)));
57
+
58
+ const SQ = [[1, 0], [0, 1]], R21 = [[2, 0], [0, 1]], R22 = [[2, 0], [0, 2]];
59
+ const HALF_LR = [[0, 0], [1, 0], [1, 1]];
60
+ const HALF_LL = [[0, 0], [1, 0], [0, 1]];
61
+ const RHOMB = [[0, 0], C, [1, 1], C2];
62
+ const THIRD = [[0, 0], C, C2];
63
+ const THIRD_LONG = [[0, 0], C, [1, 1]];
64
+ const TWELFTH = [[0, 0], C, [0.5, 0.5]];
65
+
66
+ export const GROUPS = {
67
+ // translations only
68
+ p1: { cell: SQ, domain: null, copies: [I],
69
+ lattice: 'oblique', note: 'translations only: the tile, over and over' },
70
+ // a half turn
71
+ p2: { cell: R21, domain: null, copies: [I, rotate(PI, 1, 0.5)],
72
+ lattice: 'oblique', note: 'every other tile upside down' },
73
+ // a mirror
74
+ pm: { cell: R21, domain: null, copies: [I, Mx1],
75
+ lattice: 'rectangular', note: 'mirrors in one direction' },
76
+ // a glide: reflect, then slide half a tile along the mirror
77
+ pg: { cell: R21, domain: null, copies: [I, mul(translate(0, 0.5), Mx1)],
78
+ lattice: 'rectangular', note: 'a reflection that slides as it goes' },
79
+ // a mirror on a centred lattice: the rows step by half
80
+ cm: { cell: [[2, 0], [1, 1]], domain: null, copies: [I, Mx1],
81
+ lattice: 'rhombic', note: 'mirrors, with every row stepped by half' },
82
+ // mirrors both ways
83
+ pmm: { cell: R22, domain: null, copies: [I, Mx1, My1, rotate(PI, 1, 1)],
84
+ lattice: 'rectangular', note: 'mirrors both ways: the four-tile module' },
85
+ // mirrors one way, half turns between them
86
+ pmg: { cell: R22, domain: null, copies: [I, Mx1, rotate(PI, 0.5, 1), mul(Mx1, rotate(PI, 0.5, 1))],
87
+ lattice: 'rectangular', note: 'mirrors one way, half turns between them' },
88
+ // glides both ways, no mirrors at all
89
+ pgg: { cell: R22, domain: null,
90
+ copies: [I, mul(translate(0, 1), reflect(Q, 0.5, 0)), rotate(PI, 1, 1), mul(translate(2, -2), mul(mul(translate(0, 1), reflect(Q, 0.5, 0)), rotate(PI, 1, 1)))],
91
+ lattice: 'rectangular', note: 'glides both ways and never a mirror' },
92
+ // mirrors both ways with a half turn off them: the tile cut in two
93
+ cmm: { cell: R22, domain: HALF_LR, copies: times([I, Mx1, My1, rotate(PI, 1, 1)], [I, rotate(PI, 0.5, 0.5)]),
94
+ lattice: 'rhombic', note: 'mirrors both ways, with a half turn between them' },
95
+ // quarter turns
96
+ p4: { cell: R22, domain: null, copies: turns(4, 1, 1),
97
+ lattice: 'square', note: 'quarter turns about the far corner' },
98
+ // quarter turns and mirrors: the kaleidoscope
99
+ p4m: { cell: R22, domain: HALF_LR, copies: times([I, DIAG], turns(4, 1, 1)),
100
+ lattice: 'square', note: 'quarter turns and mirrors: the kaleidoscope' },
101
+ // quarter turns with the mirrors off the centres
102
+ p4g: { cell: R22, domain: HALF_LL, copies: times([I, ANTI], turns(4, 1, 1)),
103
+ lattice: 'square', note: 'quarter turns, the mirrors missing the centres' },
104
+ // thirds of a turn
105
+ p3: { cell: [HEX_A, HEX_B], domain: RHOMB, copies: hex([I]),
106
+ lattice: 'hexagonal', note: 'thirds of a turn: three rhombi make a hexagon' },
107
+ // thirds with every centre on a mirror
108
+ p3m1: { cell: [HEX_A, HEX_B], domain: THIRD, copies: hex([I, ANTI]),
109
+ lattice: 'hexagonal', note: 'thirds of a turn, every centre on a mirror' },
110
+ // thirds with one kind of centre off the mirrors
111
+ p31m: { cell: [HEX_A, HEX_B], domain: THIRD_LONG, copies: hex([I, DIAG]),
112
+ lattice: 'hexagonal', note: 'thirds of a turn, one kind of centre off the mirrors' },
113
+ // sixths of a turn
114
+ p6: { cell: [HEX_A, HEX_B], domain: THIRD, copies: hex([I, rotate(PI, 0.5, 0.5)]),
115
+ lattice: 'hexagonal', note: 'sixths of a turn' },
116
+ // sixths and mirrors: the snowflake
117
+ p6m: { cell: [HEX_A, HEX_B], domain: TWELFTH, copies: hex(times([I, rotate(PI, 0.5, 0.5)], [I, DIAG])),
118
+ lattice: 'hexagonal', note: 'sixths of a turn and mirrors: the snowflake' },
119
+ };
120
+ export const GROUP_NAMES = Object.keys(GROUPS);
121
+
122
+ // a convex polygon moved in by d on every side
123
+ export function inset(poly, d) {
124
+ const n = poly.length, out = [];
125
+ // the polygon's turn, so "inward" is the same side on every edge
126
+ let area = 0;
127
+ for (let i = 0; i < n; i++) { const p = poly[i], q = poly[(i + 1) % n]; area += p[0] * q[1] - q[0] * p[1]; }
128
+ const sgn = area > 0 ? 1 : -1;
129
+ const lines = poly.map((p, i) => {
130
+ const q = poly[(i + 1) % n], dx = q[0] - p[0], dy = q[1] - p[1], L = Math.hypot(dx, dy) || 1;
131
+ const nx = sgn * dy / L, ny = -sgn * dx / L; // the inward normal
132
+ return [nx, ny, nx * p[0] + ny * p[1] + d]; // the line n·x = c, moved in
133
+ });
134
+ for (let i = 0; i < n; i++) {
135
+ const [a1, b1, c1] = lines[(i + n - 1) % n], [a2, b2, c2] = lines[i];
136
+ const det = a1 * b2 - a2 * b1;
137
+ if (Math.abs(det) < 1e-9) { out.push(poly[i]); continue; }
138
+ out.push([(c1 * b2 - c2 * b1) / det, (a1 * c2 - a2 * c1) / det]);
139
+ }
140
+ return out;
141
+ }
142
+
143
+ // Lays a group over a window. `S` is the tile in pixels, `gap` the
144
+ // grout in pixels, the window `W` by `H` with the world's origin at
145
+ // (ox, oy); `ctx` is already scaled to css pixels. For every copy in
146
+ // every cell the window touches, `draw(ctx, t)` is called with the
147
+ // context in design coordinates (the unit square, main corner at the
148
+ // origin), clipped to the domain less half the gap, and `t` carrying
149
+ // the cell `i`, `j`, the copy `k` and the group. Draw the tile image
150
+ // there with `drawTile(ctx, img, S, gap)`.
151
+ export function layGroup(ctx, grp, S, gap, W, H, ox, oy, draw) {
152
+ const [A, B] = grp.cell;
153
+ const det = A[0] * B[1] - A[1] * B[0];
154
+ // the window's corners, in lattice coordinates
155
+ const corners = [[-ox, -oy], [W - ox, -oy], [-ox, H - oy], [W - ox, H - oy]];
156
+ let i0 = Infinity, i1 = -Infinity, j0 = Infinity, j1 = -Infinity;
157
+ for (const [px, py] of corners) {
158
+ const x = px / S, y = py / S;
159
+ const i = (x * B[1] - y * B[0]) / det, j = (y * A[0] - x * A[1]) / det;
160
+ i0 = Math.min(i0, i); i1 = Math.max(i1, i); j0 = Math.min(j0, j); j1 = Math.max(j1, j);
161
+ }
162
+ // copies may spill out of their cell by up to a tile and a half
163
+ i0 = Math.floor(i0) - 2; i1 = Math.ceil(i1) + 1; j0 = Math.floor(j0) - 2; j1 = Math.ceil(j1) + 1;
164
+ const dom = grp.domain ? (gap > 0 ? inset(grp.domain, gap / (2 * S)) : grp.domain) : null;
165
+ for (let j = j0; j <= j1; j++) {
166
+ for (let i = i0; i <= i1; i++) {
167
+ const cx = i * A[0] + j * B[0], cy = i * A[1] + j * B[1];
168
+ for (let k = 0; k < grp.copies.length; k++) {
169
+ const M = grp.copies[k];
170
+ ctx.save();
171
+ ctx.translate(ox + cx * S, oy + cy * S);
172
+ ctx.scale(S, S);
173
+ ctx.transform(M[0], M[1], M[2], M[3], M[4], M[5]);
174
+ if (dom) {
175
+ ctx.beginPath();
176
+ ctx.moveTo(dom[0][0], dom[0][1]);
177
+ for (let n = 1; n < dom.length; n++) ctx.lineTo(dom[n][0], dom[n][1]);
178
+ ctx.closePath();
179
+ ctx.clip();
180
+ }
181
+ draw(ctx, { i, j, k, grp, M });
182
+ ctx.restore();
183
+ }
184
+ }
185
+ }
186
+ }
187
+
188
+ // the tile image into the design's unit square (the image has the
189
+ // main corner at its bottom right), less half the gap on every side
190
+ export function drawTile(ctx, img, S, gap) {
191
+ const g = gap / (2 * S);
192
+ ctx.save();
193
+ ctx.scale(-1, -1);
194
+ ctx.drawImage(img, -1 + g, -1 + g, 1 - 2 * g, 1 - 2 * g);
195
+ ctx.restore();
196
+ }
@@ -0,0 +1,75 @@
1
+ // The seventeen groups, checked: every group's copies cover the plane
2
+ // exactly once, and each pattern has the rotation orders, mirror
3
+ // directions and centre placement its name says. node test/groups.mjs
4
+ import { GROUPS, apply, rotate, reflect } from '../src/groups.js';
5
+ const SQ = [[0,0],[1,0],[1,1],[0,1]];
6
+ function inside(poly, [x, y]) {
7
+ let c = false;
8
+ for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
9
+ const [xi, yi] = poly[i], [xj, yj] = poly[j];
10
+ if ((yi > y) !== (yj > y) && x < (xj - xi) * (y - yi) / (yj - yi) + xi) c = !c;
11
+ }
12
+ return c;
13
+ }
14
+ let bad = 0;
15
+ console.log("--- coverage");
16
+ for (const [name, g] of Object.entries(GROUPS)) {
17
+ const dom = g.domain || SQ;
18
+ const [A, B] = g.cell;
19
+ // images of the domain for cells in a range
20
+ const polys = [];
21
+ for (let i = -4; i <= 4; i++) for (let j = -4; j <= 4; j++) for (const M of g.copies)
22
+ polys.push(dom.map(p => { const q = apply(M, p); return [q[0] + i * A[0] + j * B[0], q[1] + i * A[1] + j * B[1]]; }));
23
+ const counts = {};
24
+ let n = 0;
25
+ for (let t = 0; t < 4000; t++) {
26
+ const p = [Math.random() * 2 - 0.5, Math.random() * 2 - 0.5];
27
+ let c = 0;
28
+ for (const poly of polys) if (inside(poly, p)) c++;
29
+ counts[c] = (counts[c] || 0) + 1; n++;
30
+ }
31
+ const ok = (counts[1] || 0) / n > 0.995;
32
+ if (!ok) bad++;
33
+ console.log(name.padEnd(5), g.copies.length, 'copies', JSON.stringify(counts), ok ? 'ok' : 'BAD');
34
+ }
35
+
36
+
37
+ const seedPts = [[0.07, 0.03], [0.21, 0.11], [0.13, 0.19]]; // asymmetric, inside every domain
38
+ const key = ([x, y]) => Math.round(x * 200) + ',' + Math.round(y * 200);
39
+ const expect = {
40
+ p1: 'r1 m0', p2: 'r2 m0', pm: "r1 m1 someOff", pg: 'r1 m0', cm: "r1 m1 someOff", pmm: 'r2 m2 allOn', pmg: 'r2 m1 someOff',
41
+ pgg: 'r2 m0', cmm: 'r2 m2 someOff', p4: 'r4 m0', p4m: 'r4 m4 allOn', p4g: "r4 m2 someOff",
42
+ p3: 'r3 m0', p3m1: 'r3 m3 allOn', p31m: 'r3 m3 someOff', p6: 'r6 m0', p6m: 'r6 m6 allOn',
43
+ };
44
+ console.log("--- symmetry");
45
+ for (const [name, g] of Object.entries(GROUPS)) {
46
+ const [A, B] = g.cell, P = new Set(), near = [];
47
+ for (let i = -4; i <= 4; i++) for (let j = -4; j <= 4; j++) for (const M of g.copies) for (const p of seedPts) {
48
+ const q = apply(M, p), r = [q[0] + i * A[0] + j * B[0], q[1] + i * A[1] + j * B[1]];
49
+ P.add(key(r));
50
+ if (Math.hypot(r[0] - 1, r[1] - 1) < 1.6) near.push(r);
51
+ }
52
+ const holds = M => near.every(p => P.has(key(apply(M, p))));
53
+ const step = 1 / 12, centres = [];
54
+ for (let x = 0; x <= 2; x += step) for (let y = 0; y <= 2; y += step) centres.push([x, y]);
55
+ const R3 = 1 / (2 * Math.sqrt(3)); for (const c of [[0.5 + R3, 0.5 - R3], [0.5 - R3, 0.5 + R3]]) for (let i = -1; i <= 1; i++) for (let j = -1; j <= 1; j++) centres.push([c[0] + i * A[0] + j * B[0], c[1] + i * A[1] + j * B[1]]);
56
+ let rmax = 1;
57
+ const rotC = {};
58
+ for (const n of [6, 4, 3, 2]) for (const c of centres) if (holds(rotate(2 * Math.PI / n, c[0], c[1]))) { rmax = Math.max(rmax, n); (rotC[n] = rotC[n] || []).push(c); }
59
+ const mirrors = [];
60
+ for (const ang of [0,15,30,45,60,75,90,105,120,135,150,165]) for (const c of centres) {
61
+ const M = reflect(ang * Math.PI / 180, c[0], c[1]);
62
+ if (holds(M)) mirrors.push([ang, M]);
63
+ }
64
+ const dirs = new Set(mirrors.map(m => m[0]));
65
+ const onMirror = c => mirrors.some(([, M]) => { const q = apply(M, c); return Math.hypot(q[0] - c[0], q[1] - c[1]) < 1e-6; });
66
+ const top = rotC[rmax] || [];
67
+ // the centres of the highest order, and 2-folds for the r2 groups
68
+ const cs = rmax === 2 ? (rotC[2] || []) : top;
69
+ const all = cs.length && cs.every(onMirror), some = cs.some(c => !onMirror(c));
70
+ const got = `r${rmax} m${dirs.size}` + (dirs.size ? (all ? ' allOn' : ' someOff') : '');
71
+ const ok = got === expect[name];
72
+ if (!ok) bad++;
73
+ console.log(name.padEnd(5), got.padEnd(14), ok ? 'ok' : 'EXPECTED ' + expect[name]);
74
+ }
75
+ process.exit(bad ? 1 : 0);