Lot Q bords-canards, étape A : banc des bords (scripts/banc-bords.mjs), canards.ts (même parcours), test des canards rouge

Le test tests/canards.test.ts est rouge à dessein : 2 963 poses sur 3 144 hors de l'eau
(marge 0,4 m), le centre du parcours est sur la rive. Le parcours est sorti d'animaux.js
dans maquette/canards.ts sans changer de formule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jules Nény
2026-11-16 18:44:06 +01:00
co-authored by Claude Opus 5.5
parent 219697806d
commit c580c6f84e
4 changed files with 306 additions and 3 deletions
+56
View File
@@ -0,0 +1,56 @@
// LOT Q-bords-canards (16/11, retour de Jules : « les canards de la mare ne sont pas pile sur l'eau ») : à tout instant, le
// corps entier de chaque canard est dans le contour de l'eau (`mare` de data/scene2035.json), à 0,4 m au moins du bord.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { parcoursCanards, corpsAuSol } from '../maquette/canards.ts';
type Pt = [number, number];
const D = JSON.parse(readFileSync(new URL('../data/scene2035.json', import.meta.url), 'utf8')) as { canards: { c: Pt; r: number; n: number }; mare: Pt[] };
const MARGE = 0.4;
const dedans = (P: Pt[], x: number, z: number) => {
let c = false;
for (let i = 0, j = P.length - 1; i < P.length; j = i++) {
const [xi, zi] = P[i], [xj, zj] = P[j];
if (zi > z !== zj > z && x < ((xj - xi) * (z - zi)) / (zj - zi) + xi) c = !c;
}
return c;
};
const auBord = (P: Pt[], x: number, z: number) => {
let m = Infinity;
for (let i = 0, j = P.length - 1; i < P.length; j = i++) {
const a = P[j], b = P[i], dx = b[0] - a[0], dz = b[1] - a[1], L = dx * dx + dz * dz;
const t = L ? Math.max(0, Math.min(1, ((x - a[0]) * dx + (z - a[1]) * dz) / L)) : 0;
m = Math.min(m, Math.hypot(x - a[0] - t * dx, z - a[1] - t * dz));
}
return m;
};
test('canards : les six, sur un tour complet, ont le corps entier dans l\'eau, à 0,4 m du bord au moins', () => {
const nage = parcoursCanards(D);
assert.equal(nage.n, 6);
const tour = (2 * Math.PI) / 0.12; // la vitesse angulaire du parcours : un tour en ≈ 52 s ; on échantillonne deux tours
let mini = Infinity, dehors = 0, poses = 0;
for (let i = 0; i < nage.n; i++) for (let t = 0; t <= 2 * tour; t += 0.2) {
poses++;
let ok = true;
for (const [x, z] of corpsAuSol(nage.pos(i, t))) {
const d = dedans(D.mare, x, z) ? auBord(D.mare, x, z) : -auBord(D.mare, x, z);
mini = Math.min(mini, d); if (d < MARGE) ok = false;
}
if (!ok) dehors++;
}
assert.equal(dehors, 0, `${dehors} poses sur ${poses} hors de l'eau (marge ${MARGE} m) ; marge mini ${mini.toFixed(2)} m`);
assert.ok(mini >= MARGE, `marge mini ${mini.toFixed(2)} m`);
});
test('canards : le parcours est déterministe et garde la hauteur de l\'eau ; le centre du vol des oiseaux ne bouge pas', () => {
const a = parcoursCanards(D), b = parcoursCanards(D);
for (let i = 0; i < 6; i++) for (const t of [0, 7.3, 41]) assert.deepEqual(a.pos(i, t), b.pos(i, t));
const animaux = readFileSync(new URL('../maquette/animaux.js', import.meta.url), 'utf8');
assert.match(animaux, /o\.position\.set\(p\.x, 0\.05, p\.z\)/);
assert.match(animaux, /const centres = \[\[c\[0\], c\[1\]\], \[100, -40\]\]/);
assert.deepEqual(D.canards.c, [-21.79, -20.37], 'le centre de la mare des données (oiseaux, ondes, gestes, tour du mode fond) reste');
assert.doesNotMatch(readFileSync(new URL('../maquette/canards.ts', import.meta.url), 'utf8'), /Math\.random/);
});