@altnovatrail/picture-kit 0.1.1 → 0.2.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.
Files changed (130) hide show
  1. package/README.md +1 -1
  2. package/lib/index.d.ts +45 -1
  3. package/lib/index.js +34 -3
  4. package/lib/kinds/balance.js +2 -3
  5. package/lib/kinds/calendar.d.ts +5 -1
  6. package/lib/kinds/calendar.js +33 -11
  7. package/lib/kinds/chart.js +2 -4
  8. package/lib/kinds/clock.d.ts +5 -0
  9. package/lib/kinds/clock.js +45 -4
  10. package/lib/kinds/coins.js +3 -2
  11. package/lib/kinds/column.js +2 -3
  12. package/lib/kinds/commands.d.ts +8 -6
  13. package/lib/kinds/commands.js +57 -15
  14. package/lib/kinds/containers.js +2 -1
  15. package/lib/kinds/countfig.d.ts +2 -0
  16. package/lib/kinds/countfig.js +6 -1
  17. package/lib/kinds/cubes.d.ts +29 -5
  18. package/lib/kinds/cubes.js +82 -34
  19. package/lib/kinds/die.d.ts +8 -0
  20. package/lib/kinds/die.js +75 -14
  21. package/lib/kinds/domino.js +3 -3
  22. package/lib/kinds/dotgrid.d.ts +3 -0
  23. package/lib/kinds/dotgrid.js +6 -4
  24. package/lib/kinds/figure.js +5 -4
  25. package/lib/kinds/fold-parts.d.ts +0 -2
  26. package/lib/kinds/fold-parts.js +0 -4
  27. package/lib/kinds/fold.d.ts +20 -1
  28. package/lib/kinds/fold.js +15 -6
  29. package/lib/kinds/fraction.js +3 -2
  30. package/lib/kinds/gauges.js +2 -1
  31. package/lib/kinds/gears.d.ts +21 -2
  32. package/lib/kinds/gears.js +113 -62
  33. package/lib/kinds/grid.d.ts +5 -0
  34. package/lib/kinds/grid.js +30 -2
  35. package/lib/kinds/groups.d.ts +3 -1
  36. package/lib/kinds/groups.js +142 -28
  37. package/lib/kinds/lanes.js +3 -2
  38. package/lib/kinds/layers.d.ts +6 -0
  39. package/lib/kinds/layers.js +23 -4
  40. package/lib/kinds/line.d.ts +4 -0
  41. package/lib/kinds/line.js +117 -32
  42. package/lib/kinds/matchsticks.d.ts +4 -0
  43. package/lib/kinds/matchsticks.js +5 -3
  44. package/lib/kinds/net.js +4 -3
  45. package/lib/kinds/overlay.d.ts +23 -1
  46. package/lib/kinds/overlay.js +7 -3
  47. package/lib/kinds/pieces.js +3 -2
  48. package/lib/kinds/plane-parts.d.ts +0 -2
  49. package/lib/kinds/plane-parts.js +0 -6
  50. package/lib/kinds/plane.js +6 -6
  51. package/lib/kinds/ropes.js +1 -3
  52. package/lib/kinds/shapes.js +5 -7
  53. package/lib/kinds/solids.js +3 -2
  54. package/lib/kinds/tangram.js +2 -4
  55. package/lib/kinds/target.js +4 -6
  56. package/lib/kinds/tiles.js +5 -4
  57. package/lib/kinds/venn.js +3 -5
  58. package/lib/noto.js +1 -0
  59. package/lib/objects.js +2 -1
  60. package/lib/read.d.ts +2 -2
  61. package/lib/read.js +3 -3
  62. package/lib/solve/balance.d.ts +11 -0
  63. package/lib/solve/balance.js +116 -1
  64. package/lib/solve/calendar.d.ts +46 -0
  65. package/lib/solve/calendar.js +206 -0
  66. package/lib/solve/choice-scenes.d.ts +20 -0
  67. package/lib/solve/choice-scenes.js +51 -0
  68. package/lib/solve/clock.d.ts +54 -0
  69. package/lib/solve/clock.js +240 -0
  70. package/lib/solve/coins.d.ts +71 -0
  71. package/lib/solve/coins.js +222 -0
  72. package/lib/solve/commands.d.ts +36 -0
  73. package/lib/solve/commands.js +210 -0
  74. package/lib/solve/countfig.d.ts +41 -0
  75. package/lib/solve/countfig.js +199 -0
  76. package/lib/solve/cubes.d.ts +49 -0
  77. package/lib/solve/cubes.js +255 -0
  78. package/lib/solve/die.d.ts +39 -0
  79. package/lib/solve/die.js +93 -0
  80. package/lib/solve/domino.d.ts +57 -0
  81. package/lib/solve/domino.js +263 -0
  82. package/lib/solve/enumerate.d.ts +75 -0
  83. package/lib/solve/enumerate.js +323 -0
  84. package/lib/solve/figure.d.ts +26 -0
  85. package/lib/solve/figure.js +88 -0
  86. package/lib/solve/fold.d.ts +22 -0
  87. package/lib/solve/fold.js +68 -0
  88. package/lib/solve/gaps.d.ts +65 -0
  89. package/lib/solve/gaps.js +292 -0
  90. package/lib/solve/gears.d.ts +33 -0
  91. package/lib/solve/gears.js +130 -0
  92. package/lib/solve/grid.d.ts +35 -0
  93. package/lib/solve/grid.js +194 -0
  94. package/lib/solve/groups.d.ts +93 -0
  95. package/lib/solve/groups.js +346 -0
  96. package/lib/solve/index.d.ts +60 -0
  97. package/lib/solve/index.js +194 -0
  98. package/lib/solve/layers.d.ts +30 -0
  99. package/lib/solve/layers.js +72 -0
  100. package/lib/solve/magic.d.ts +32 -0
  101. package/lib/solve/magic.js +216 -0
  102. package/lib/solve/matchsticks.d.ts +37 -0
  103. package/lib/solve/matchsticks.js +214 -0
  104. package/lib/solve/order.d.ts +94 -0
  105. package/lib/solve/order.js +419 -0
  106. package/lib/solve/overlay.d.ts +22 -0
  107. package/lib/solve/overlay.js +56 -0
  108. package/lib/solve/parity.d.ts +22 -0
  109. package/lib/solve/parity.js +27 -0
  110. package/lib/solve/pattern.d.ts +62 -0
  111. package/lib/solve/pattern.js +533 -0
  112. package/lib/solve/pieces.d.ts +57 -0
  113. package/lib/solve/pieces.js +309 -0
  114. package/lib/solve/pigeonhole.d.ts +38 -0
  115. package/lib/solve/pigeonhole.js +95 -0
  116. package/lib/solve/proof.d.ts +115 -0
  117. package/lib/solve/proof.js +105 -0
  118. package/lib/solve/pyramid.d.ts +28 -0
  119. package/lib/solve/pyramid.js +161 -0
  120. package/lib/solve/question.d.ts +25 -0
  121. package/lib/solve/question.js +75 -0
  122. package/lib/solve/shapes.d.ts +30 -0
  123. package/lib/solve/shapes.js +182 -0
  124. package/lib/solve/target.d.ts +40 -0
  125. package/lib/solve/target.js +190 -0
  126. package/lib/style.d.ts +1 -1
  127. package/lib/style.js +1 -1
  128. package/lib/words.d.ts +15 -0
  129. package/lib/words.js +39 -0
  130. package/package.json +1 -1
@@ -3,15 +3,18 @@ import { textWidth } from "../pen.js";
3
3
  import { int, isObj, list, oneOf, only, text } from "../read.js";
4
4
  import { colorNames, colors, ink, isColor, outline, textOn } from "../style.js";
5
5
  import { byCodeUnit, n, poly } from "../svg.js";
6
+ import { article } from "../words.js";
6
7
  /**
7
8
  * Gears: toothed gears and smooth wheels on a board, each but the first set
8
9
  * beside an earlier one, and belts round pairs of wheels, open or crossed. A
9
10
  * gear set beside a gear with no gap meshes with it: the program puts it
10
11
  * exactly where the teeth of the two fit, and draws every tooth. Some gears
11
12
  * carry an arrow of the way they turn, and one may carry a "?". The check
12
- * refuses wheels that overlap without meshing, a ring of wheels that locks,
13
+ * refuses wheels that overlap without meshing, teeth that miss each other,
13
14
  * arrows that disagree with the gears, and belts that run over a wheel or
14
- * another belt.
15
+ * another belt. A ring of wheels that locks — an odd number of meshes and
16
+ * crossed belts round it — is drawn as it stands, so that a task asks whether
17
+ * the wheels can turn: its train shows one arrow at most, the way it is pushed.
15
18
  */
16
19
  export const directions = [
17
20
  "up",
@@ -36,6 +39,8 @@ const dedendum = 7;
36
39
  const toothHalf = 4.6;
37
40
  const flank = 0.12;
38
41
  const pitch = Math.PI * step;
42
+ /** How far, along the pitch circles, the teeth of meshing gears may miss each other and still fit. */
43
+ const fits = 1.2;
39
44
  const most = 10;
40
45
  const widest = 640;
41
46
  const tallest = 600;
@@ -350,6 +355,100 @@ function checkScene(scene, problems) {
350
355
  });
351
356
  return problems.length > 0 ? undefined : scene;
352
357
  }
358
+ /** placedBy tells whether gear b was set to mesh with gear a, its teeth fitted to a's. */
359
+ const placedBy = (a, b) => b.wheel.next === a.wheel.id && (b.wheel.gap ?? 0) === 0;
360
+ /** free tells whether a gear's teeth are not fitted to another's: the first, or one set apart. */
361
+ const free = (s) => s.index === 0 || (s.wheel.gap ?? 0) > 0;
362
+ /**
363
+ * turnToFit turns free gears, each with the gears fitted to it, so that the teeth of gears that
364
+ * meet without one set beside the other fit too, where a turn can do it. Each mesh passes a turn
365
+ * on the other way round, so round a ring of an odd number of meshes the two ends of the ring move
366
+ * apart by twice the turn and their teeth come to fit; round an even ring they move together and
367
+ * no turn helps. Gears whose teeth fit as placed keep their places.
368
+ */
369
+ function turnToFit(spots, byId) {
370
+ const meeting = [];
371
+ for (const [j, b] of spots.entries())
372
+ for (const a of spots.slice(0, j))
373
+ if (a.gear && b.gear && !placedBy(a, b) && Math.abs(dist(a, b) - a.r - b.r) < 0.5)
374
+ meeting.push([a, b]);
375
+ const worst = () => Math.max(0, ...meeting.map(([a, b]) => misfit(a, b)));
376
+ if (worst() <= fits)
377
+ return;
378
+ const offsets = spots.map(() => 0);
379
+ const rephase = () => {
380
+ for (const spot of spots)
381
+ if (spot.gear)
382
+ spot.phase = free(spot)
383
+ ? -Math.PI / 2 + offsets[spot.index] / spot.r
384
+ : phaseBeside(spots[byId.get(spot.wheel.next)], spot);
385
+ };
386
+ const tries = 256;
387
+ for (const spot of spots) {
388
+ if (!spot.gear || !free(spot))
389
+ continue;
390
+ let best = { at: 0, worst: Number.POSITIVE_INFINITY };
391
+ for (let k = 0; k < tries; k++) {
392
+ offsets[spot.index] = (k * pitch) / tries;
393
+ rephase();
394
+ const w = worst();
395
+ if (w < best.worst - 1e-9)
396
+ best = { at: offsets[spot.index], worst: w };
397
+ }
398
+ offsets[spot.index] = best.at;
399
+ rephase();
400
+ }
401
+ }
402
+ /**
403
+ * trainsOf joins every two gears that mesh, and every two wheels a belt goes round, and gives each
404
+ * wheel the first of its train and the side it turns on against that first.
405
+ */
406
+ function trainsOf(s, meshes) {
407
+ const byId = new Map(s.gears.map((w, i) => [w.id, i]));
408
+ const links = [
409
+ ...meshes.map(([a, b]) => ({ a, b, flip: true })),
410
+ ...(s.belts ?? []).map((belt) => ({
411
+ a: byId.get(belt.from),
412
+ b: byId.get(belt.to),
413
+ flip: belt.crossed === true,
414
+ })),
415
+ ];
416
+ const root = s.gears.map(() => -1);
417
+ const side = s.gears.map(() => 0);
418
+ // A train with a ring that locks: no way of turning agrees with every link of it.
419
+ const locked = new Set();
420
+ for (let first = 0; first < root.length; first++) {
421
+ if (root[first] !== -1)
422
+ continue;
423
+ root[first] = first;
424
+ const queue = [first];
425
+ while (queue.length > 0) {
426
+ const at = queue.shift();
427
+ for (const link of links) {
428
+ if (link.a !== at && link.b !== at)
429
+ continue;
430
+ const other = link.a === at ? link.b : link.a;
431
+ const want = side[at] ^ (link.flip ? 1 : 0);
432
+ if (root[other] === -1) {
433
+ root[other] = first;
434
+ side[other] = want;
435
+ queue.push(other);
436
+ }
437
+ else if (side[other] !== want)
438
+ locked.add(first);
439
+ }
440
+ }
441
+ }
442
+ return { root, side, locked };
443
+ }
444
+ /**
445
+ * trains gives how the wheels of a checked scene are joined, by the meshes the kind lays out and
446
+ * draws: a gear set beside a gear meshes with it, and so does one that meets another on its pitch
447
+ * circle where the layout puts them.
448
+ */
449
+ export function trains(scene) {
450
+ return plan(scene, []).trains;
451
+ }
353
452
  /** plan lays a checked scene out; it records what cannot be drawn well. */
354
453
  function plan(s, problems) {
355
454
  const byId = new Map(s.gears.map((w, i) => [w.id, i]));
@@ -378,18 +477,17 @@ function plan(s, problems) {
378
477
  }
379
478
  spots.push(spot);
380
479
  });
480
+ turnToFit(spots, byId);
381
481
  // Meshes and overlaps, every pair.
382
482
  const meshes = [];
383
- // Teeth that miss each other are told after a ring that locks, the deeper fault.
384
483
  const misfits = [];
385
484
  for (let j = 1; j < spots.length; j++)
386
485
  for (let i = 0; i < j; i++) {
387
486
  const a = spots[i];
388
487
  const b = spots[j];
389
488
  const d = dist(a, b);
390
- const placed = b.wheel.next === a.wheel.id && (b.wheel.gap ?? 0) === 0;
391
489
  if (a.gear && b.gear && Math.abs(d - a.r - b.r) < 0.5) {
392
- if (!placed && misfit(a, b) > 1.2)
490
+ if (!placedBy(a, b) && misfit(a, b) > fits)
393
491
  misfits.push(`gears[${j}]: meets ${nameOf(a.wheel)} too, but their teeth do not fit there; leave a gap between them or set the gears another way`);
394
492
  meshes.push([i, j]);
395
493
  }
@@ -449,74 +547,26 @@ function plan(s, problems) {
449
547
  problems.push(`belts: ${nameOf(spots[i].wheel)} carries ${k} belts; a wheel carries at most 2`);
450
548
  if (problems.length > 0)
451
549
  return undefined;
452
- // Which way each wheel turns, against the first of its train.
453
- const links = [
454
- ...meshes.map(([a, b]) => ({ a, b, flip: true })),
455
- ...(s.belts ?? []).map((belt) => ({
456
- a: byId.get(belt.from),
457
- b: byId.get(belt.to),
458
- flip: belt.crossed === true,
459
- })),
460
- ];
461
- const side = spots.map(() => undefined);
462
- const parent = spots.map((_, i) => i);
463
- for (let root = 0; root < spots.length; root++) {
464
- if (side[root] !== undefined)
465
- continue;
466
- side[root] = 0;
467
- const queue = [root];
468
- while (queue.length > 0) {
469
- const at = queue.shift();
470
- for (const link of links) {
471
- if (link.a !== at && link.b !== at)
472
- continue;
473
- const other = link.a === at ? link.b : link.a;
474
- const want = side[at] ^ (link.flip ? 1 : 0);
475
- if (side[other] === undefined) {
476
- side[other] = want;
477
- parent[other] = at;
478
- queue.push(other);
479
- }
480
- else if (side[other] !== want) {
481
- const up = (i) => {
482
- const out = [i];
483
- while (parent[out.at(-1)] !== out.at(-1))
484
- out.push(parent[out.at(-1)]);
485
- return out;
486
- };
487
- const left = up(at);
488
- const right = up(other);
489
- const meet = left.find((i) => right.includes(i));
490
- const ring = [
491
- ...left.slice(0, left.indexOf(meet) + 1),
492
- ...right.slice(0, right.indexOf(meet)).reverse(),
493
- ].map((i) => spots[i].wheel.id);
494
- problems.push(`gears: the ring ${[...ring, ring[0]].join(" – ")} locks and cannot turn: each mesh and each crossed belt turns the next wheel the other way, and this ring has an odd number of them; take a wheel out of the ring`);
495
- return undefined;
496
- }
497
- }
498
- }
499
- }
550
+ const trains = trainsOf(s, meshes);
551
+ const { side, locked } = trains;
500
552
  problems.push(...misfits);
501
553
  if (problems.length > 0)
502
554
  return undefined;
503
- const train = (i) => {
504
- let at = i;
505
- while (parent[at] !== at)
506
- at = parent[at];
507
- return at;
508
- };
509
555
  const shown = new Map();
510
556
  for (const spot of spots) {
511
557
  const turn = spot.wheel.turn;
512
558
  if (!turn)
513
559
  continue;
514
- const root = train(spot.index);
560
+ const root = trains.root[spot.index];
515
561
  const first = shown.get(root);
516
562
  if (!first) {
517
563
  shown.set(root, spot);
518
564
  continue;
519
565
  }
566
+ if (locked.has(root)) {
567
+ problems.push(`gears[${spot.index}].turn: ${nameOf(spot.wheel)} is in a train with a ring that locks, as ${nameOf(first.wheel)} is, so no second arrow can agree with the first; leave it out`);
568
+ continue;
569
+ }
520
570
  const same = side[first.index] === side[spot.index];
521
571
  const should = same
522
572
  ? first.wheel.turn
@@ -591,6 +641,7 @@ function plan(s, problems) {
591
641
  return {
592
642
  spots,
593
643
  meshes,
644
+ trains,
594
645
  belts,
595
646
  arrows,
596
647
  hubs,
@@ -779,8 +830,8 @@ export const gears = {
779
830
  const ends = `${nameOf(s.gears.find((w) => w.id === b.from))} and ${nameOf(s.gears.find((w) => w.id === b.to))}`;
780
831
  const color = colors[b.color ?? "gray"].word;
781
832
  out.push(b.crossed
782
- ? `A ${color} belt runs round ${ends}, crossed between them like a figure eight.`
783
- : `A ${color} belt runs round ${ends}, not crossed.`);
833
+ ? `${capital(article(color))} belt runs round ${ends}, crossed between them like a figure eight.`
834
+ : `${capital(article(color))} belt runs round ${ends}, not crossed.`);
784
835
  }
785
836
  return out.join(" ");
786
837
  },
@@ -9,6 +9,9 @@ import { type Thing } from "../things.ts";
9
9
  * "all", and doors are gaps in them; a path runs over the cells, as a line
10
10
  * through their centres or as jumps from cell to cell with labels. A place
11
11
  * is [row, column], counted from 1 at the top left, so cells[0][0] is [1, 1].
12
+ * Rows and columns may have labels on any side: rowLabels on the left and
13
+ * colLabels on top name them, and rightLabels and bottomLabels stand beside
14
+ * them on the right and below, as the sums of each row and column do.
12
15
  */
13
16
  export type At = [number, number];
14
17
  export type CellObj = {
@@ -39,6 +42,8 @@ export type GridScene = {
39
42
  path?: GridPath;
40
43
  rowLabels?: string[];
41
44
  colLabels?: string[];
45
+ rightLabels?: string[];
46
+ bottomLabels?: string[];
42
47
  };
43
48
  /** edgeKey names the line a side of a cell lies on: h<line>,<column> or v<row>,<line>. */
44
49
  export declare function edgeKey(at: At, side: Side): string;
package/lib/kinds/grid.js CHANGED
@@ -4,6 +4,7 @@ import { int, isObj, list, oneOf, only, text } from "../read.js";
4
4
  import { colorNames, colors, font, ground, ink, isColor, outline, textOn, } from "../style.js";
5
5
  import { esc, n, poly } from "../svg.js";
6
6
  import { checkThing, drawThing, wordOf } from "../things.js";
7
+ import { article, capital } from "../words.js";
7
8
  export const sides = ["top", "right", "bottom", "left"];
8
9
  export const pathStyles = ["line", "jumps"];
9
10
  const most = 10;
@@ -230,7 +231,17 @@ function sayLine(g, line) {
230
231
  }
231
232
  export const grid = {
232
233
  kind: "grid",
233
- members: ["kind", "cells", "walls", "doors", "path", "rowLabels", "colLabels"],
234
+ members: [
235
+ "kind",
236
+ "cells",
237
+ "walls",
238
+ "doors",
239
+ "path",
240
+ "rowLabels",
241
+ "colLabels",
242
+ "rightLabels",
243
+ "bottomLabels",
244
+ ],
234
245
  check(scene, problems) {
235
246
  only(scene, this.members, "scene", problems);
236
247
  const before = problems.length;
@@ -297,6 +308,8 @@ export const grid = {
297
308
  for (const [member, count] of [
298
309
  ["rowLabels", cells.length],
299
310
  ["colLabels", cols],
311
+ ["rightLabels", cells.length],
312
+ ["bottomLabels", cols],
300
313
  ]) {
301
314
  if (scene[member] === undefined)
302
315
  continue;
@@ -515,6 +528,17 @@ export const grid = {
515
528
  sub.text((c + 0.5) * s, top, label, { size: 20 });
516
529
  grow({ x: (c + 0.5) * s, y: top }, 10, 14);
517
530
  });
531
+ // labels on the right of the rows and below the columns, outside everything drawn so far
532
+ const right = Math.max(box.x1, cols * s + wallWidth / 2) + 12;
533
+ g.rightLabels?.forEach((label, r) => {
534
+ sub.text(right, (r + 0.5) * s, label, { size: 20, anchor: "start" });
535
+ grow({ x: right + textWidth(label, 20) / 2, y: (r + 0.5) * s }, textWidth(label, 20) / 2 + 4, 12);
536
+ });
537
+ const bottom = Math.max(box.y1, rows * s + wallWidth / 2) + 20;
538
+ g.bottomLabels?.forEach((label, c) => {
539
+ sub.text((c + 0.5) * s, bottom, label, { size: 20 });
540
+ grow({ x: (c + 0.5) * s, y: bottom }, textWidth(label, 20) / 2 + 4, 14);
541
+ });
518
542
  const pad = wallWidth / 2;
519
543
  box.x0 = Math.min(box.x0, -pad);
520
544
  box.y0 = Math.min(box.y0, -pad);
@@ -542,6 +566,10 @@ export const grid = {
542
566
  out.push(`The rows are labelled ${g.rowLabels.join(", ")} from the top.`);
543
567
  if (g.colLabels)
544
568
  out.push(`The columns are labelled ${g.colLabels.join(", ")} from the left.`);
569
+ if (g.rightLabels)
570
+ out.push(`On the right of the rows, from the top: ${g.rightLabels.join(", ")}.`);
571
+ if (g.bottomLabels)
572
+ out.push(`Below the columns, from the left: ${g.bottomLabels.join(", ")}.`);
545
573
  const shown = g.cells.some((row) => row.some((c) => c !== null && sayCell(c) !== "empty"));
546
574
  if (shown)
547
575
  g.cells.forEach((row, r) => {
@@ -574,7 +602,7 @@ export const grid = {
574
602
  const color = colors[g.path.color ?? "red"].word;
575
603
  const through = g.path.through;
576
604
  if ((g.path.style ?? "line") === "line")
577
- out.push(`A ${color} line with an arrow goes from ${name(g, through[0])}${through.length > 2
605
+ out.push(`${capital(article(color))} line with an arrow goes from ${name(g, through[0])}${through.length > 2
578
606
  ? ` through ${through
579
607
  .slice(1, -1)
580
608
  .map((at) => name(g, at))
@@ -4,7 +4,8 @@ import { type Thing } from "../things.ts";
4
4
  * Groups: things in groups, each group in a holder the picture draws itself —
5
5
  * a tree, a bush, a plate, a basket, a box, a pond, a jar — or in none. The
6
6
  * holder has places, and the picture puts each thing in a place of its own,
7
- * so a group shows exactly as many things as the scene says.
7
+ * so a group shows exactly as many things as the scene says. A group with no
8
+ * holder or in a box may stand in rows: an array of equal rows, as "3 rows of 4".
8
9
  */
9
10
  export declare const holders: readonly ["none", "tree", "bush", "plate", "basket", "box", "pond", "jar"];
10
11
  export type Holder = (typeof holders)[number];
@@ -15,6 +16,7 @@ export type Group = {
15
16
  count: number;
16
17
  }[];
17
18
  label?: string;
19
+ rows?: number;
18
20
  };
19
21
  export type GroupsScene = {
20
22
  kind: "groups";
@@ -1,16 +1,22 @@
1
1
  import { margin } from "../kind.js";
2
+ import { Pen } from "../pen.js";
2
3
  import { int, isObj, list, oneOf, only, text } from "../read.js";
3
- import { colors, ink, outline } from "../style.js";
4
+ import { colors, faint, ink, outline, soft } from "../style.js";
4
5
  import { poly } from "../svg.js";
5
6
  import { checkThing, count, drawThing } from "../things.js";
7
+ import { article, capital, listOf } from "../words.js";
6
8
  /**
7
9
  * Groups: things in groups, each group in a holder the picture draws itself —
8
10
  * a tree, a bush, a plate, a basket, a box, a pond, a jar — or in none. The
9
11
  * holder has places, and the picture puts each thing in a place of its own,
10
- * so a group shows exactly as many things as the scene says.
12
+ * so a group shows exactly as many things as the scene says. A group with no
13
+ * holder or in a box may stand in rows: an array of equal rows, as "3 rows of 4".
11
14
  */
12
15
  export const holders = ["none", "tree", "bush", "plate", "basket", "box", "pond", "jar"];
13
16
  const most = 24;
17
+ /** The holders whose things may stand in rows, and the most things a row holds. */
18
+ const rowHolders = ["none", "box"];
19
+ const mostInRow = 8;
14
20
  /** rowsIn lays k places out in rows that fit a shape, given each row's width. */
15
21
  function rowsIn(k, height, widthAt, size) {
16
22
  for (let s = size; s >= 14; s -= 2) {
@@ -59,6 +65,54 @@ function rowsIn(k, height, widthAt, size) {
59
65
  function expand(group) {
60
66
  return group.items.flatMap((item) => Array.from({ length: item.count }, () => item.thing));
61
67
  }
68
+ /** The side of a cell of a box whose things stand in rows, and the width of the box's walls. */
69
+ const cell = 42;
70
+ const wall = 12;
71
+ /**
72
+ * drawTray draws a box whose things stand in rows as a box seen from above, a cell for each
73
+ * thing inside its walls, as sweets lie in a box of sweets: the rows from the top, each from the
74
+ * left.
75
+ */
76
+ function drawTray(pen, things, rows) {
77
+ const cols = things.length / rows;
78
+ const x = 4;
79
+ const y = 4;
80
+ const w = cols * cell + 2 * wall;
81
+ const h = rows * cell + 2 * wall;
82
+ const color = colors.brown;
83
+ pen.el("rect", {
84
+ x,
85
+ y,
86
+ width: w,
87
+ height: h,
88
+ rx: 8,
89
+ fill: color.fill,
90
+ stroke: ink,
91
+ "stroke-width": outline,
92
+ });
93
+ pen.el("rect", {
94
+ x: x + wall,
95
+ y: y + wall,
96
+ width: cols * cell,
97
+ height: rows * cell,
98
+ fill: soft,
99
+ stroke: color.dark,
100
+ "stroke-width": 2,
101
+ });
102
+ let lines = "";
103
+ for (let c = 1; c < cols; c++)
104
+ lines += `M${x + wall + c * cell} ${y + wall}v${rows * cell}`;
105
+ for (let r = 1; r < rows; r++)
106
+ lines += `M${x + wall} ${y + wall + r * cell}h${cols * cell}`;
107
+ if (lines)
108
+ pen.el("path", { d: lines, stroke: faint, "stroke-width": 1.5, fill: "none" });
109
+ things.forEach((t, i) => {
110
+ const cx = x + wall + ((i % cols) + 0.5) * cell;
111
+ const cy = y + wall + (Math.floor(i / cols) + 0.5) * cell;
112
+ drawThing(pen, t, cx, cy, cell - 8);
113
+ });
114
+ return { width: w + 8, height: h + 8 };
115
+ }
62
116
  /** drawGroup draws one holder with its things, its top left at 0, 0. */
63
117
  function drawGroup(pen, group, fit) {
64
118
  const things = expand(group);
@@ -164,9 +218,10 @@ function drawGroup(pen, group, fit) {
164
218
  case "basket":
165
219
  case "box":
166
220
  case "jar": {
221
+ if (group.rows)
222
+ return drawTray(pen, things, group.rows);
167
223
  const s = 36;
168
224
  const cols = Math.min(5, Math.max(2, Math.ceil(Math.sqrt(room * 1.4))));
169
- const rows = Math.ceil(k / cols);
170
225
  const fitRows = Math.ceil(room / cols);
171
226
  const w = cols * s + 30;
172
227
  const cx = w / 2 + 17;
@@ -253,12 +308,13 @@ function drawGroup(pen, group, fit) {
253
308
  "stroke-width": 2,
254
309
  });
255
310
  }
256
- void rows;
257
311
  return { width: w + 34, height: bottom + 4 };
258
312
  }
259
313
  case "none": {
260
314
  const s = 44;
261
- const cols = Math.min(6, Math.max(1, Math.ceil(Math.sqrt(k * 1.6))));
315
+ const cols = group.rows
316
+ ? k / group.rows
317
+ : Math.min(6, Math.max(1, Math.ceil(Math.sqrt(k * 1.6))));
262
318
  const rows = Math.ceil(k / cols);
263
319
  things.forEach((t, i) => {
264
320
  const row = Math.floor(i / cols);
@@ -269,6 +325,35 @@ function drawGroup(pen, group, fit) {
269
325
  }
270
326
  }
271
327
  }
328
+ /** The room between two holders, and the widest a line of holders goes. */
329
+ const between = 18;
330
+ const widest = 640 - 2 * margin;
331
+ const leastScale = 0.8;
332
+ /** fitOf is how many things each holder of a kind makes room for: as many as the fullest has. */
333
+ function fitOf(list_, g) {
334
+ const holder = g.holder ?? "none";
335
+ return Math.max(...list_.filter((o) => (o.holder ?? "none") === holder).map((o) => expand(o).length));
336
+ }
337
+ /**
338
+ * linesOf splits the holders, by their widths, into the fewest lines that keep within the
339
+ * picture's width, as even as can be — the upper lines hold one more than the lower when the
340
+ * count does not divide —, the holders drawn smaller, down to a least scale, rather than on one
341
+ * more line.
342
+ */
343
+ function linesOf(widths) {
344
+ const k = widths.length;
345
+ for (let count = 1;; count++) {
346
+ const lines = [];
347
+ for (let l = 0, at = 0; l < count; l++) {
348
+ const take = Math.floor(k / count) + (l < k % count ? 1 : 0);
349
+ lines.push(Array.from({ length: take }, (_, j) => at + j));
350
+ at += take;
351
+ }
352
+ const scale = Math.min(1, ...lines.map((line) => (widest - between * (line.length - 1)) / line.reduce((sum, i) => sum + widths[i], 0)));
353
+ if (scale >= leastScale || count === k)
354
+ return { lines, scale: Math.min(1, scale) };
355
+ }
356
+ }
272
357
  export const groups = {
273
358
  kind: "groups",
274
359
  members: ["kind", "groups"],
@@ -280,7 +365,7 @@ export const groups = {
280
365
  problems.push(`${path}: must be a group`);
281
366
  return;
282
367
  }
283
- only(g, ["holder", "items", "label"], path, problems);
368
+ only(g, ["holder", "items", "label", "rows"], path, problems);
284
369
  if (g.holder !== undefined)
285
370
  oneOf(g.holder, holders, `${path}.holder`, problems);
286
371
  let total = 0;
@@ -297,39 +382,68 @@ export const groups = {
297
382
  problems.push(`${path}.items: at most ${most} things in a group`);
298
383
  if (g.label !== undefined)
299
384
  text(g.label, `${path}.label`, problems, 10);
385
+ if (g.rows !== undefined) {
386
+ const before = problems.length;
387
+ const rows = int(g.rows, `${path}.rows`, problems, 1, 6);
388
+ const holder = g.holder ?? "none";
389
+ if (holders.includes(holder) && !rowHolders.includes(holder))
390
+ problems.push(`${path}.rows: things stand in rows with no holder or in a box, not in ${article(String(holder))}`);
391
+ if (problems.length === before && total > 0) {
392
+ if (total % rows !== 0)
393
+ problems.push(`${path}.rows: ${total} things do not make ${rows} equal rows`);
394
+ else if (total / rows > mostInRow)
395
+ problems.push(`${path}.rows: at most ${mostInRow} things in a row; use more rows`);
396
+ }
397
+ }
300
398
  });
301
399
  },
302
400
  draw(scene, pen) {
303
401
  const list_ = scene.groups;
304
- const fitOf = (h) => Math.max(...list_.filter((g) => (g.holder ?? "none") === h).map((g) => expand(g).length));
305
402
  const drawn = list_.map((g) => {
306
403
  const sub = pen.sub();
307
- return { sub, label: g.label, ...drawGroup(sub, g, fitOf(g.holder ?? "none")) };
404
+ return { sub, label: g.label, ...drawGroup(sub, g, fitOf(list_, g)) };
308
405
  });
406
+ const { lines, scale } = linesOf(drawn.map((d) => d.width));
309
407
  const labelled = drawn.some((d) => d.label);
310
- const height = Math.max(...drawn.map((d) => d.height));
311
- let x = margin;
312
- for (const d of drawn) {
313
- pen.place(d.sub, x, margin + height - d.height);
314
- if (d.label)
315
- pen.text(x + d.width / 2, margin + height + 18, d.label, { size: 18 });
316
- x += d.width + 18;
317
- }
318
- return { width: x - 18 + margin, height: margin + height + (labelled ? 34 : 0) + margin };
408
+ const lineW = lines.map((line) => line.reduce((sum, i) => sum + drawn[i].width * scale, 0) + between * (line.length - 1));
409
+ const width = Math.max(...lineW);
410
+ let y = margin;
411
+ lines.forEach((line, l) => {
412
+ const height = Math.max(...line.map((i) => drawn[i].height * scale));
413
+ let x = margin + (width - lineW[l]) / 2;
414
+ for (const i of line) {
415
+ const d = drawn[i];
416
+ pen.place(d.sub, x, y + height - d.height * scale, scale);
417
+ if (d.label)
418
+ pen.text(x + (d.width * scale) / 2, y + height + 18, d.label, { size: 18 });
419
+ x += d.width * scale + between;
420
+ }
421
+ y += height + (labelled ? 34 : 0) + between;
422
+ });
423
+ return { width: width + 2 * margin, height: y - between + margin };
319
424
  },
320
425
  describe(scene) {
321
426
  const list_ = scene.groups;
322
- return list_
323
- .map((g, i) => {
324
- const what = g.items.map((item) => count(item.thing, item.count)).join(" and ");
427
+ const one = (g, alone) => {
428
+ const things = listOf(g.items.map((item) => count(item.thing, item.count)));
429
+ const k = g.items.reduce((sum, item) => sum + item.count, 0);
430
+ const what = g.rows
431
+ ? `${things}, in ${g.rows === 1 ? "a row" : `${g.rows} rows`} of ${k / g.rows}${g.items.length > 1 ? ", in that order row by row from the top, each row from the left" : ""}`
432
+ : things;
325
433
  const holder = g.holder && g.holder !== "none" ? g.holder : "group";
326
- const name = g.label
327
- ? `${holder[0].toUpperCase()}${holder.slice(1)} ${g.label}`
328
- : list_.length > 1
329
- ? `${holder[0].toUpperCase()}${holder.slice(1)} ${i + 1}`
330
- : `A ${holder}`;
331
- return `${name}: ${what}.`;
332
- })
333
- .join(" ");
434
+ if (alone)
435
+ return `${g.label ? `${capital(holder)} ${g.label}` : capital(article(holder))}: ${what}.`;
436
+ return `${g.label ? `${holder} ${g.label}` : article(holder)} with ${what}`;
437
+ };
438
+ if (list_.length === 1)
439
+ return one(list_[0], true);
440
+ const { lines } = linesOf(list_.map((g) => drawGroup(new Pen(), g, fitOf(list_, g)).width));
441
+ const say = (line) => line.map((i) => one(list_[i], false)).join("; ");
442
+ if (lines.length === 1)
443
+ return `${list_.length} groups, from left to right: ${say(lines[0])}.`;
444
+ return [
445
+ `${list_.length} groups in ${lines.length} lines, one under another.`,
446
+ ...lines.map((line, l) => `Line ${l + 1} from the top, from left to right: ${say(line)}.`),
447
+ ].join(" ");
334
448
  },
335
449
  };
@@ -3,6 +3,7 @@ import { int, isObj, list, oneOf, only } from "../read.js";
3
3
  import { colorNames, colors, ground, ink, isColor } from "../style.js";
4
4
  import { n } from "../svg.js";
5
5
  import { checkThing, drawThing, wordOf } from "../things.js";
6
+ import { article, capital, listOf } from "../words.js";
6
7
  import { smallText } from "./commands-parts.js";
7
8
  /**
8
9
  * Lanes: vertical lanes side by side, a thing at the top of each and,
@@ -206,14 +207,14 @@ export const lanes = {
206
207
  .filter((r) => r.at === at)
207
208
  .sort((p, q) => p.between[0] - q.between[0])
208
209
  .map((r) => `lanes ${r.between[0]} and ${r.between[1]}`);
209
- return here.length === 1 ? here[0] : `${here.join(" and ")}, at the same height`;
210
+ return here.length === 1 ? here[0] : `${listOf(here)}, at the same height`;
210
211
  });
211
212
  out.push(`${ladder ? "Rungs" : "Bars with a dot at each end"} join, from the top down: ${rows.join("; then ")}.`);
212
213
  if (s.trace) {
213
214
  const { end } = traceOf(s, s.trace.from);
214
215
  const from = s.tops[s.trace.from - 1];
215
216
  const to = s.bottoms?.[end - 1];
216
- out.push(`A ${colors[s.trace.color ?? "orange"].word} line traces the way down from the ${wordOf(from)} at the top of lane ${s.trace.from} to the bottom of lane ${end}${to ? `, at the ${wordOf(to)}` : ""}.`);
217
+ out.push(`${capital(article(colors[s.trace.color ?? "orange"].word))} line traces the way down from the ${wordOf(from)} at the top of lane ${s.trace.from} to the bottom of lane ${end}${to ? `, at the ${wordOf(to)}` : ""}.`);
217
218
  }
218
219
  return out.join(" ");
219
220
  },
@@ -28,4 +28,10 @@ export type LayersScene = {
28
28
  kind: "layers";
29
29
  sheets: Layer[];
30
30
  };
31
+ /**
32
+ * overlaps tells, for every two sheets of a checked scene, whether they overlap: whether some
33
+ * sample of the picture, as the kind looks at it, lies in both. The check makes the picture show
34
+ * which of two that overlap lies on the other.
35
+ */
36
+ export declare function overlaps(sheets: readonly Layer[]): boolean[][];
31
37
  export declare const layers: Kind;