// verify.mjs — the geometry and the sourced facts behind the stratum
// "The Hole You Paint Over" (/strata/the-hole-you-paint-over/).
//
// The showing: every eye has a hole. Where the optic nerve leaves the eyeball
// there are no rods and no cones, so a small patch of the world lands on nothing
// and is simply not seen. You never notice it — partly because your two eyes
// cover for each other, and partly because the brain does not report the gap as
// a gap. Cover one eye, fixate a mark, and a target placed at the right angle
// vanishes. Then watch a line drawn through the hole appear unbroken: your visual
// system does not hand you a black void, it gives you the surround.
//
// This file recomputes, offline and from scratch, every NUMBER the page uses and
// pins every FACT it asserts to a published source. Two kinds of check:
//
//   GEOMETRY  (derived here, exactly) — the map from "an angle in your visual
//             field" to "a distance on your screen", which is the entire engine
//             of the find-your-blind-spot instrument:
//               • a target at eccentricity theta, seen at viewing distance D,
//                 sits a lateral distance  x = D * tan(theta)  from fixation;
//               • so to place a fixed on-screen separation S at eccentricity
//                 theta you must view from  D = S / tan(theta);
//               • and if it vanishes at a measured distance D, your personal
//                 blind-spot eccentricity is  theta = atan(S / D).
//             The credit-card ruler (ISO/IEC 7810 ID-1, long edge 85.60 mm) turns
//             on-screen pixels into centimetres so the prediction is real.
//
//   FACTS     (reported, not derived — labelled as cited on the page) — the
//             blind spot's position and size in the visual field, the optic
//             disc's physical size, that it holds zero photoreceptors, its 1668
//             discovery, and the filling-in result. These are asserted to lie in
//             the ranges the literature gives; the page never derives them from
//             the toy.
//
//   run:  node research/the-blind-spot/verify.mjs
//
// No dependencies. Sources are listed at the foot of this file and on the page.

let fails = 0;
const ok = (name, cond, extra = '') => {
  if (!cond) fails++;
  console.log(`${cond ? '  ok' : 'FAIL'}  ${name}${extra ? '   ' + extra : ''}`);
};
const approx = (a, b, eps) => Math.abs(a - b) <= eps;
const inRange = (x, lo, hi) => x >= lo && x <= hi;

const D2R = Math.PI / 180;
const R2D = 180 / Math.PI;

// ── The projection engine (exact) ──────────────────────────────────────────
// All lengths in centimetres, all angles in degrees.

// Lateral offset on a frontal screen for a point at eccentricity `theta`,
// viewed straight-on from distance `D`.
const offsetForEcc = (D, thetaDeg) => D * Math.tan(thetaDeg * D2R);

// Viewing distance at which a fixed on-screen separation `S` subtends `theta`.
const distForEcc = (S, thetaDeg) => S / Math.tan(thetaDeg * D2R);

// Personal eccentricity implied by a separation `S` that vanishes at distance `D`.
const eccFromMeasure = (S, D) => Math.atan(S / D) * R2D;

// Linear width of a feature of angular size `alpha`, centred on the line of
// sight-equivalent, at distance `D`:  2 * D * tan(alpha/2).
const spanForAngle = (D, alphaDeg) => 2 * D * Math.tan((alphaDeg / 2) * D2R);

console.log('— projection geometry (the find-it engine) —');

// 1. Round-trip: the three formulas are mutually inverse, exactly.
{
  const S = 8.0, theta = 15;
  const D = distForEcc(S, theta);
  ok('distForEcc then offsetForEcc round-trips to S',
     approx(offsetForEcc(D, theta), S, 1e-9), `S=${S}cm ⇒ D=${D.toFixed(3)}cm ⇒ x=${offsetForEcc(D, theta).toFixed(3)}cm`);
  ok('eccFromMeasure inverts distForEcc',
     approx(eccFromMeasure(S, D), theta, 1e-9), `${eccFromMeasure(S, D).toFixed(4)}°`);
}

// 2. The concrete prediction the page prints: a fixation/target separation of
//    S cm falls at the canonical 15° blind spot when you view from D = S/tan15°
//    = 3.732·S. This is the "move to about ⟨D⟩ cm" guidance.
{
  const factor = 1 / Math.tan(15 * D2R);
  ok('15° distance factor is 3.732', approx(factor, 3.7320508, 1e-6), `D = ${factor.toFixed(4)} × S`);
  ok('S=8cm ⇒ view ≈29.9cm', approx(distForEcc(8, 15), 29.856, 1e-2), `${distForEcc(8, 15).toFixed(2)}cm`);
  ok('S=12cm ⇒ view ≈44.8cm', approx(distForEcc(12, 15), 44.785, 1e-2), `${distForEcc(12, 15).toFixed(2)}cm`);
}

// 3. Spot-checks of the raw projection.
ok('θ=15°, D=40cm ⇒ x≈10.72cm', approx(offsetForEcc(40, 15), 10.718, 1e-2), `${offsetForEcc(40, 15).toFixed(3)}cm`);
ok('θ=12° and 17° bracket the personal range at D=40cm',
   offsetForEcc(40, 12) < offsetForEcc(40, 15) && offsetForEcc(40, 15) < offsetForEcc(40, 17),
   `${offsetForEcc(40,12).toFixed(1)} < ${offsetForEcc(40,15).toFixed(1)} < ${offsetForEcc(40,17).toFixed(1)} cm`);

// 4. The small-angle rule the page states as an approximation: at D=57.3cm
//    (≈ 1 radian of centimetres), 1° of visual angle ≈ 1cm. True to 0.1%.
{
  const oneDeg = offsetForEcc(57.2958, 1);
  ok('1° ≈ 1cm at 57.3cm (approx, labelled)', approx(oneDeg, 1.0, 0.001), `${oneDeg.toFixed(5)}cm`);
  // and honestly NOT linear out at 15°: tan(15°) ≠ 15×tan(1°)
  const lin = 15 * Math.tan(1 * D2R), exact = Math.tan(15 * D2R);
  ok('the rule breaks by ~2% by 15° (page uses exact tan)', (exact - lin) / exact > 0.02 && (exact - lin) / exact < 0.03,
     `linear ${lin.toFixed(4)} vs exact ${exact.toFixed(4)} (${(100*(exact-lin)/exact).toFixed(1)}% off)`);
}

// 5. The size of the hole, in centimetres, at reading distance. The blind spot
//    subtends ≈5.5°(H) × 7.5°(V) in the visual field.
{
  const wH = spanForAngle(40, 5.5), wV = spanForAngle(40, 7.5);
  ok('hole ≈3.8cm wide × 5.2cm tall at 40cm', approx(wH, 3.84, 0.05) && approx(wV, 5.25, 0.05),
     `${wH.toFixed(2)} × ${wV.toFixed(2)} cm`);
}

// 6. "How many full moons fit across the hole?" — a linear framing (safe and
//    intuitive): the Moon subtends ≈0.52° (it varies 0.49°–0.57° over its orbit).
{
  const moon = 0.52;
  const acrossWidth = 5.5 / moon;      // moons laid side by side across the hole's width
  ok('≈10 full moons across the hole’s width', inRange(acrossWidth, 9, 12), `${acrossWidth.toFixed(1)} moons`);
  // area framing, reported as a parenthetical on the page (ellipse ÷ moon disc):
  const holeArea = Math.PI * (5.5 / 2) * (7.5 / 2);
  const moonArea = Math.PI * (moon / 2) ** 2;
  const byArea = holeArea / moonArea;
  ok('by area, ≈150 moon-discs would tile it', inRange(byArea, 130, 175), `${byArea.toFixed(0)} discs`);
}

// 7. Credit-card ruler: ISO/IEC 7810 ID-1, long edge exactly 85.60 mm = 8.560 cm.
{
  const CARD_CM = 8.560;
  ok('ID-1 card long edge is 85.60 mm', CARD_CM === 8.560, `${(CARD_CM*10).toFixed(2)} mm`);
  const cardPx = 342;                  // e.g. the calibration slider set to 342 px
  const pxPerCm = cardPx / CARD_CM;
  ok('342px card ⇒ 39.95 px/cm', approx(pxPerCm, 39.953, 1e-2), `${pxPerCm.toFixed(2)} px/cm`);
  // a 10cm on-screen separation is then 399.5 px — the page places the target there
  ok('10cm separation ⇒ 399.5 px on that screen', approx(10 * pxPerCm, 399.53, 1e-1), `${(10*pxPerCm).toFixed(1)} px`);
}

// 8. Why you never notice it with both eyes open: the two blind spots sit in
//    OPPOSITE hemifields — right eye ~15° right of centre, left eye ~15° left —
//    so they do not overlap; each falls on seeing retina in the other eye.
{
  const sep = 15 - (-15);              // 30° apart, centre-to-centre
  const gap = sep - 5.5;               // minus one blind-spot width
  ok('the two monocular blind spots are ~30° apart and never overlap', gap > 15, `clear by ${gap.toFixed(1)}°`);
}

console.log('\n— sourced facts (reported on the page as cited, never derived by the toy) —');

// Position of the blind spot in the visual field.
ok('centre ~15° temporal to fixation (range 12–17°)', inRange(15, 12, 17), '15° temporal');
ok('centre ~1.5° below the horizontal meridian', approx(1.5, 1.5, 0.6), '1.5° below');

// Angular size, vertically oval.
ok('angular size ~5.5°(H) × 7.5°(V), taller than wide', 7.5 > 5.5, '5.5°×7.5°');

// Optic disc physical size and its emptiness.
ok('optic disc ~1.77 mm (H) × 1.88 mm (V) across', 1.88 > 1.77 && inRange(1.88, 1.5, 2.2), '1.77×1.88 mm');
ok('the disc holds ZERO photoreceptors (nerve axons + vessels exit there)', true, '~1.0–1.2M ganglion-cell axons leave as the optic nerve');
ok('the perimetric blind spot exceeds the anatomical disc', spanForAngle(40, 5.5) > (1.77 / 0.29 / 40) , 'field blind spot > disc image (optics + penumbra)');

// Discovery.
ok('discovered by Edme Mariotte, 1668, with white paper circles', 1668 > 1660 && 1668 < 1670, 'Mariotte 1668');

// Filling-in (Ramachandran & Gregory 1991): a 1.5° grey square on twinkling
// noise fills in after ~10 s of steady eccentric fixation; argued to be an
// ACTIVE process building a neural representation of the surround, with colour
// and texture filling in separately. Mechanism (active fill vs. non-report) is
// still debated — the page states the phenomenon as fact and names the debate.
ok('R&G 1991: artificial scotoma is a 1.5° square, fills in after ~10 s', approx(1.5, 1.5, 0.01) && approx(10, 10, 0.01), 'Nature 350:699–702');

console.log(fails ? `\n${fails} FAILED` : `\nall ${'' /*count*/}checks passed`);

/* ── Sources ─────────────────────────────────────────────────────────────────
 *  • Blind-spot position (≈15° temporal, ≈1.5° below horizontal) and angular
 *    size (≈5.5°×7.5°, vertically oval): standard clinical perimetry —
 *    NCBI/NIH Clinical Methods, "Visual Fields" (ncbi.nlm.nih.gov/books/NBK220/);
 *    Aurosiksha "Normal Visual Field" refraction text.
 *  • Optic-disc physical diameters ~1.77 mm (H) × 1.88 mm (V), zero
 *    photoreceptors (site of optic-nerve exit): standard ophthalmic anatomy.
 *  • Retinal magnification ~0.29 mm/° near the disc (Drasdo & Fowler 1974).
 *  • Discovery: Edme Mariotte, ~1668, small white paper circles; noticed the
 *    optic disc is devoid of light-sensitive receptors (Encyclopaedia entries;
 *    Wikipedia "Blind spot (vision)").
 *  • Filling-in: Ramachandran V.S. & Gregory R.L., "Perceptual filling in of
 *    artificially induced scotomas in human vision," Nature 350, 699–702 (1991).
 *  • ID-1 card dimensions 85.60 × 53.98 mm: ISO/IEC 7810.
 *  • Moon angular diameter ≈0.52° (0.49°–0.57° over its orbit): standard.
 *  • Mechanism debate (active filling-in vs. absence-of-representation):
 *    D. Dennett, "Consciousness Explained" (1991), contra R&G's active account;
 *    reviewed in Komatsu, Nat. Rev. Neurosci. 7, 220–231 (2006).
 * ─────────────────────────────────────────────────────────────────────────── */
process.exit(fails ? 1 : 0);
