azulejeria 2.3.1 → 2.5.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 +49 -2
- package/package.json +5 -4
- package/src/azulejeria.js +129 -23
- package/src/groups.js +196 -0
- package/test/groups.mjs +75 -0
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
|
|
@@ -85,8 +87,9 @@ tap. Drive it from your own controls through `wall.state` and
|
|
|
85
87
|
|
|
86
88
|
**`createWall(canvas, { palettes, motifs, painters, presets, material, variants })`** returns
|
|
87
89
|
the wall. `wall.state` holds `seed`, `repeat` (`module`, `rotate`,
|
|
88
|
-
`single`, `checker`,
|
|
89
|
-
laid the same way up
|
|
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
|
|
90
93
|
such a wall draws from, four by default), `pal`, `motif`, `density` (1 to 5), `hand` (0 to
|
|
91
94
|
1, how far the brush drifts), `brush` (line weight), `scale` (pixels per
|
|
92
95
|
tile), `panX`, `panY`, `bands`, `grout` and `age`. After changing any of
|
|
@@ -182,6 +185,17 @@ edge across the far corner, which four tiles close into a ring or a
|
|
|
182
185
|
diamond), `ring` (a scalloped ring around the main corner), `lattice` (a
|
|
183
186
|
line edge to edge that mirrors into a lozenge net) and `dots`.
|
|
184
187
|
|
|
188
|
+
**A border round a corner**: `bendCorner(strip, fill)` takes a square
|
|
189
|
+
tile painted as a run of border (outside along the top, inside along
|
|
190
|
+
the bottom, the design meeting itself left and right) and returns the
|
|
191
|
+
corner tile: the strip bent through a quarter turn about its inner
|
|
192
|
+
corner, so the vine or the scroll turns the corner and meets the strip
|
|
193
|
+
on both sides. `fill(g, size)`, if given, paints the outer corner the
|
|
194
|
+
bend does not reach. [Azulejos de letras](https://stepinto.dev/art/letras.html)
|
|
195
|
+
uses it for the frame of a street plaque. `mitreCorner(strip)` is the
|
|
196
|
+
square alternative: the strip cut on the diagonal and laid both ways,
|
|
197
|
+
as a run of border tiles is mitred at forty-five degrees.
|
|
198
|
+
|
|
185
199
|
**The lower level** is there if you want it: `makeDesign(seed, density,
|
|
186
200
|
bands, bold, motif)`, `paintDesign(g, els, pal, hand, lw, seed, painters, material, brush)`,
|
|
187
201
|
`makeTile(opts, size, pixelRatio, seed)`, `tileOpts(cfg, state)` and
|
|
@@ -225,6 +239,39 @@ a small speckle tile for mortar. `centroid`, `signedArea`,
|
|
|
225
239
|
`ensureCCW`, `regularPoly` and `pathPoly` are the small polygon maths
|
|
226
240
|
every page wanted.
|
|
227
241
|
|
|
242
|
+
## Groups
|
|
243
|
+
|
|
244
|
+
`azulejeria/groups` is the seventeen ways a wall can repeat: the
|
|
245
|
+
wallpaper groups, for [Papel Pintado](https://stepinto.dev/art/papelpintado.html)
|
|
246
|
+
and for any wall that sets `state.repeat` to one of their names. Every
|
|
247
|
+
repeating pattern in the plane has one of these symmetries, and each
|
|
248
|
+
is given here as a lattice (two vectors, in tiles), a domain (the part
|
|
249
|
+
of the tile that is drawn: the whole square, half of it cut on a
|
|
250
|
+
diagonal, or the 60° rhombus corner to corner and its thirds, sixths
|
|
251
|
+
and twelfths) and the copies, the isometries that lay the domain out
|
|
252
|
+
inside one cell. Every domain keeps the design's main corner, so what
|
|
253
|
+
gets repeated is the heart of the design.
|
|
254
|
+
|
|
255
|
+
```js
|
|
256
|
+
import { GROUPS, GROUP_NAMES, layGroup, drawTile } from 'azulejeria/groups';
|
|
257
|
+
|
|
258
|
+
GROUP_NAMES; // ['p1', 'p2', 'pm', 'pg', 'cm', 'pmm', 'pmg', 'pgg', 'cmm', 'p4', 'p4m', 'p4g', 'p3', 'p3m1', 'p31m', 'p6', 'p6m']
|
|
259
|
+
const g = GROUPS.p6m; // { cell, domain, copies, lattice, note }
|
|
260
|
+
layGroup(ctx, g, S, gap, W, H, ox, oy, (c, t) => drawTile(c, img, S, gap));
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
**`layGroup(ctx, group, S, gap, W, H, ox, oy, draw)`** walks every
|
|
264
|
+
copy in every cell a window touches, with `S` the tile in pixels,
|
|
265
|
+
`gap` the grout and the world's origin at `(ox, oy)`, and calls
|
|
266
|
+
`draw(ctx, { i, j, k, grp, M })` with the context in design
|
|
267
|
+
coordinates (the unit square, main corner at the origin) and clipped
|
|
268
|
+
to the domain less half the gap. **`drawTile(ctx, img, S, gap)`** puts
|
|
269
|
+
a tile image there. The matrices are plain `[a, b, c, d, e, f]`
|
|
270
|
+
arrays with `mul`, `rotate(th, cx, cy)`, `reflect(ang, px, py)`,
|
|
271
|
+
`translate` and `apply` beside them, and `inset(poly, d)` moves a
|
|
272
|
+
convex polygon in by `d`. The wall's `createWall` uses all of this
|
|
273
|
+
when `repeat` names a group, and `paintThumb` lays a card with it.
|
|
274
|
+
|
|
228
275
|
## Knots
|
|
229
276
|
|
|
230
277
|
`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.
|
|
3
|
+
"version": "2.5.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) {
|
|
@@ -331,10 +338,123 @@ export function tileOpts(cfg, s) {
|
|
|
331
338
|
return { pal, design, quarter, hand: s.hand, brush: s.brush, painters: cfg.painters, material: cfg.material };
|
|
332
339
|
}
|
|
333
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 border tile bent round a corner. `strip` is a square tile painted
|
|
368
|
+
// as a run of border: the outside of the wall along its top edge, the
|
|
369
|
+
// inside along its bottom, the design meeting itself at left and
|
|
370
|
+
// right. The corner is the quarter-annulus about the tile's inner
|
|
371
|
+
// corner: distance from that corner becomes how far in the design
|
|
372
|
+
// sits, and the angle round it becomes how far along, so the strip's
|
|
373
|
+
// right edge becomes the corner's right edge, its left edge the
|
|
374
|
+
// corner's bottom edge, and the design turns through a quarter between
|
|
375
|
+
// them. Where the annulus does not reach, the outer corner, `fill(g)`
|
|
376
|
+
// paints the ground (the glaze by default, sampled from the strip's
|
|
377
|
+
// own corner). The result joins the strip laid to its right unturned
|
|
378
|
+
// and laid below it turned three quarters; the other three corners are
|
|
379
|
+
// this one turned.
|
|
380
|
+
export function bendCorner(strip, fill) {
|
|
381
|
+
const N = strip.width;
|
|
382
|
+
const c = document.createElement('canvas');
|
|
383
|
+
c.width = c.height = N;
|
|
384
|
+
const g = c.getContext('2d');
|
|
385
|
+
if (fill) fill(g, N);
|
|
386
|
+
else { g.drawImage(strip, 0, 0, 2, 2, 0, 0, N, N); }
|
|
387
|
+
const src = strip.getContext('2d').getImageData(0, 0, N, N).data;
|
|
388
|
+
const out = g.getImageData(0, 0, N, N), d = out.data;
|
|
389
|
+
const Q = Math.PI / 2;
|
|
390
|
+
for (let py = 0; py < N; py++) for (let px = 0; px < N; px++) {
|
|
391
|
+
const u = 1 - (px + 0.5) / N, v = 1 - (py + 0.5) / N;
|
|
392
|
+
const r = Math.hypot(u, v);
|
|
393
|
+
if (r > 1) continue;
|
|
394
|
+
const along = Math.atan2(u, v) / Q, inward = 1 - r;
|
|
395
|
+
// bilinear sample of the strip at (along, inward)
|
|
396
|
+
const sx = Math.min(N - 1.001, Math.max(0, along * N - 0.5)), sy = Math.min(N - 1.001, Math.max(0, inward * N - 0.5));
|
|
397
|
+
const x0 = Math.floor(sx), y0 = Math.floor(sy), fx = sx - x0, fy = sy - y0;
|
|
398
|
+
const i = (py * N + px) * 4;
|
|
399
|
+
for (let ch = 0; ch < 4; ch++) {
|
|
400
|
+
const a = src[(y0 * N + x0) * 4 + ch], b = src[(y0 * N + x0 + 1) * 4 + ch];
|
|
401
|
+
const cc = src[((y0 + 1) * N + x0) * 4 + ch], dd = src[((y0 + 1) * N + x0 + 1) * 4 + ch];
|
|
402
|
+
d[i + ch] = (a * (1 - fx) + b * fx) * (1 - fy) + (cc * (1 - fx) + dd * fx) * fy;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
g.putImageData(out, 0, 0);
|
|
406
|
+
return c;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// The same strip mitred instead: the corner is cut on the diagonal
|
|
410
|
+
// from the outer corner to the inner one, the strip laid unturned
|
|
411
|
+
// above the cut and turned three quarters below it, the way a picture
|
|
412
|
+
// frame or a run of border tiles is cut at forty-five degrees. It joins
|
|
413
|
+
// the same neighbours as bendCorner's.
|
|
414
|
+
export function mitreCorner(strip) {
|
|
415
|
+
const N = strip.width;
|
|
416
|
+
const c = document.createElement('canvas');
|
|
417
|
+
c.width = c.height = N;
|
|
418
|
+
const g = c.getContext('2d');
|
|
419
|
+
g.save(); g.beginPath(); g.moveTo(0, 0); g.lineTo(N, 0); g.lineTo(N, N); g.closePath(); g.clip();
|
|
420
|
+
g.drawImage(strip, 0, 0); g.restore();
|
|
421
|
+
g.save(); g.beginPath(); g.moveTo(0, 0); g.lineTo(0, N); g.lineTo(N, N); g.closePath(); g.clip();
|
|
422
|
+
g.translate(N / 2, N / 2); g.rotate(3 * Math.PI / 2); g.drawImage(strip, -N / 2, -N / 2); g.restore();
|
|
423
|
+
return c;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// a wall laid by a wallpaper group: every copy of the domain in every
|
|
427
|
+
// cell the window touches, each straying from its place as the hand
|
|
428
|
+
// allows, aged if asked. `pick(rng)` chooses the tile image.
|
|
429
|
+
function layWall(ctx, grp, S, gap, W, H, ox, oy, state, pal, m, lay, pick) {
|
|
430
|
+
layGroup(ctx, grp, S, gap, W, H, ox, oy, (g, t) => {
|
|
431
|
+
const rng = mulberry32(hash2(t.i * 32 + t.k, t.j) + state.seed);
|
|
432
|
+
const img = pick(rng);
|
|
433
|
+
g.translate(0.5 + (rng() - 0.5) * lay.shift * state.hand, 0.5 + (rng() - 0.5) * lay.shift * state.hand);
|
|
434
|
+
g.rotate((rng() - 0.5) * lay.turn * state.hand);
|
|
435
|
+
g.translate(-0.5, -0.5);
|
|
436
|
+
drawTile(g, img, S, gap);
|
|
437
|
+
if (state.age) {
|
|
438
|
+
// age is drawn in pixels about the tile's centre
|
|
439
|
+
g.translate(0.5, 0.5); g.scale(1 / S, 1 / S);
|
|
440
|
+
const s = S - gap;
|
|
441
|
+
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 });
|
|
442
|
+
else ageTile(g, rng, s, pal);
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
|
|
334
447
|
// a small wall of four tiles, for a preset card
|
|
335
448
|
export function paintThumb(cfg, p, g, T, k) {
|
|
336
449
|
const o = tileOpts(cfg, p);
|
|
337
450
|
const A = makeTile(o, T, k, p.seed * 31);
|
|
451
|
+
const grp = GROUPS[p.repeat];
|
|
452
|
+
if (grp) {
|
|
453
|
+
g.fillStyle = o.pal.grout; g.fillRect(0, 0, 2 * T, 2 * T);
|
|
454
|
+
const m = cfg.material || {}, gap = p.grout === false ? 0 : (m.gapOf ? m.gapOf(T, p) : Math.max(1.2, T * 0.022));
|
|
455
|
+
layWall(g, grp, T, gap, 2 * T, 2 * T, 0, 0, { ...p, age: false }, o.pal, m, { shift: 0, turn: 0 }, () => A);
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
338
458
|
const B = p.repeat === 'checker' ? makeTile({ ...o, plain: true }, T, k, p.seed * 37) : null;
|
|
339
459
|
g.fillStyle = o.pal.grout; g.fillRect(0, 0, 2 * T, 2 * T);
|
|
340
460
|
for (let j = 0; j < 2; j++) for (let i = 0; i < 2; i++) {
|
|
@@ -498,7 +618,7 @@ export function createWall(canvas, cfg) {
|
|
|
498
618
|
const wall = createViewer(canvas, { paint, scale: 120, minZoom: 40, maxZoom: 260 });
|
|
499
619
|
const state = Object.assign(wall.state, {
|
|
500
620
|
seed: 1,
|
|
501
|
-
repeat: 'module', // module | rotate | single | checker
|
|
621
|
+
repeat: 'module', // module | rotate | single | checker | whole | a wallpaper group
|
|
502
622
|
pal: Object.keys(palettes)[0],
|
|
503
623
|
motif: firstMotif,
|
|
504
624
|
density: 3, // how much of the design is drawn, 1..5
|
|
@@ -533,6 +653,13 @@ export function createWall(canvas, cfg) {
|
|
|
533
653
|
|
|
534
654
|
const S = state.scale;
|
|
535
655
|
const gap = state.grout ? (m.gapOf ? m.gapOf(S, state) : Math.max(1.2, S * 0.022)) : 0;
|
|
656
|
+
const grp = GROUPS[state.repeat];
|
|
657
|
+
if (grp) {
|
|
658
|
+
layWall(ctx, grp, S, gap, W, H, ox, oy, state, pal, m, lay, rng => tiles.A[Math.floor(rng() * tiles.A.length)]);
|
|
659
|
+
if (m.finish) m.finish(ctx, W, H, state, pal);
|
|
660
|
+
else if (state.age) { ctx.fillStyle = 'rgba(90, 60, 20, 0.05)'; ctx.fillRect(0, 0, W, H); }
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
536
663
|
const i0 = Math.floor(-ox / S) - 1, i1 = Math.ceil((W - ox) / S) + 1;
|
|
537
664
|
const j0 = Math.floor(-oy / S) - 1, j1 = Math.ceil((H - oy) / S) + 1;
|
|
538
665
|
const s = S - gap;
|
|
@@ -553,28 +680,7 @@ export function createWall(canvas, cfg) {
|
|
|
553
680
|
if (state.age && m.age) {
|
|
554
681
|
m.age(ctx, { i, j, rng, s, S, pal, state, cx: (i + 0.5) * S, cy: (j + 0.5) * S, fx, fy, rot });
|
|
555
682
|
} else if (state.age) {
|
|
556
|
-
|
|
557
|
-
if (rng() < 0.5) { ctx.fillStyle = `rgba(120, 85, 30, ${0.04 + rng() * 0.1})`; ctx.fillRect(-s / 2, -s / 2, s, s); }
|
|
558
|
-
if (rng() < 0.18) {
|
|
559
|
-
ctx.strokeStyle = 'rgba(60, 45, 30, 0.35)';
|
|
560
|
-
ctx.lineWidth = 0.7;
|
|
561
|
-
const pts = linePts(-s / 2, (rng() - 0.5) * s, s / 2, (rng() - 0.5) * s, s * 0.08, rng, 14);
|
|
562
|
-
trace(ctx, pts, false); ctx.stroke();
|
|
563
|
-
if (rng() < 0.5) {
|
|
564
|
-
const q = linePts((rng() - 0.5) * s, -s / 2, (rng() - 0.5) * s, s / 2, s * 0.08, rng, 14);
|
|
565
|
-
trace(ctx, q, false); ctx.stroke();
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
if (rng() < 0.05) {
|
|
569
|
-
const cxs = rng() < 0.5 ? -1 : 1, cys = rng() < 0.5 ? -1 : 1;
|
|
570
|
-
ctx.fillStyle = pal.grout;
|
|
571
|
-
ctx.beginPath();
|
|
572
|
-
ctx.moveTo(cxs * s / 2, cys * s / 2);
|
|
573
|
-
ctx.lineTo(cxs * (s / 2 - s * (0.08 + rng() * 0.1)), cys * s / 2);
|
|
574
|
-
ctx.lineTo(cxs * (s / 2 - s * 0.03), cys * (s / 2 - s * 0.05));
|
|
575
|
-
ctx.lineTo(cxs * s / 2, cys * (s / 2 - s * (0.08 + rng() * 0.1)));
|
|
576
|
-
ctx.closePath(); ctx.fill();
|
|
577
|
-
}
|
|
683
|
+
ageTile(ctx, rng, s, pal);
|
|
578
684
|
}
|
|
579
685
|
ctx.restore();
|
|
580
686
|
}
|
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
|
+
}
|
package/test/groups.mjs
ADDED
|
@@ -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);
|