Download static/visuals/optics.js from Ashish-K/stepwise: direct link, hf CLI and curl.
- Browser
- Download file 27.4 kB
-
https://huggingface.co/spaces/Ashish-K/stepwise/resolve/main/static/visuals/optics.js
- Command line
-
hf download hf://spaces/Ashish-K/stepwise/static/visuals/optics.js
-
curl -L -o optics.js https://huggingface.co/spaces/Ashish-K/stepwise/resolve/main/static/visuals/optics.js
27.4 kB
| // Visual class: refraction, total internal reflection and the converging lens (Cambridge IGCSE Physics 0625, 3.2.2–3.2.3). | |
| // Model -> solver (+ independent cross-check) -> plausibility checks -> sensible changes -> questions. | |
| import { Checks } from "./core.js"; | |
| export const ID = "optics"; | |
| export const ATOMS = {}; // ideas come from the chapter it is attached to (data/chapters/0625_light.json) | |
| export const TITLE = "Refraction and lenses"; | |
| export const SCENARIOS = { into: "Light entering a block", out: "Light leaving a block", tir: "Total internal reflection", lens: "Converging lens: real image", lens2F: "Converging lens: object at 2F", magnifier: "Converging lens: virtual image" }; | |
| const D2R = Math.PI / 180, R2D = 180 / Math.PI; | |
| const sinD = (d) => Math.sin(d * D2R), asinD = (x) => Math.asin(x) * R2D; | |
| export const MEDIA = [{ name: "water", n: 1.33 }, { name: "perspex", n: 1.49 }, { name: "glass", n: 1.52 }, { name: "dense glass", n: 1.65 }, { name: "diamond", n: 2.42 }]; | |
| const inMedium = (m) => (m === "water" ? "water" : `a block of ${m}`); | |
| // ---------- solver ---------- | |
| export function solve(inst) { | |
| if (inst.scenario === "lens" || inst.scenario === "lens2F" || inst.scenario === "magnifier") { | |
| const { f, u, h } = inst; | |
| if (u === f) return { kind: "lens", none: true }; | |
| const real = u > f; const v = real ? (u * f) / (u - f) : (u * f) / (f - u); // distance from the lens; virtual images are on the object's side | |
| const m = v / u; const size = Math.abs(m - 1) < 1e-9 ? "same size" : m > 1 ? "enlarged" : "diminished"; | |
| return { kind: "lens", real, v, m, hi: h * m, size, nature: real ? "real, inverted" : "virtual, upright" }; | |
| } | |
| const n = inst.n; const c = asinD(1 / n); | |
| if (inst.scenario === "into") { const r = asinD(sinD(inst.i) / n); return { kind: "ray", c, r, bend: "towards", speed: "slows down", tir: false }; } | |
| // leaving the block: i is measured inside the medium | |
| if (inst.i > c) return { kind: "ray", c, tir: true, reflect: inst.i }; | |
| const r = asinD(n * sinD(inst.i)); | |
| return { kind: "ray", c, r, bend: "away", speed: "speeds up", tir: false }; | |
| } | |
| // independent check: rebuild the answer a second way (Snell's law back-substitution; lens via the magnification triangle) | |
| export function crossCheck(inst, sol) { | |
| const p = []; | |
| if (sol.kind === "ray" && !sol.tir) { | |
| const back = inst.scenario === "into" ? sinD(inst.i) / sinD(sol.r) : sinD(sol.r) / sinD(inst.i); | |
| if (Math.abs(back - inst.n) > 1e-6) p.push(`n back ${back} vs ${inst.n}`); | |
| } | |
| if (sol.kind === "ray" && Math.abs(sinD(sol.c) * inst.n - 1) > 1e-9) p.push("critical angle"); | |
| if (sol.kind === "lens" && !sol.none) { | |
| // the ray through the optical centre and the parallel ray must meet at (v, -hi) for real, (-v, hi) for virtual | |
| const x = sol.real ? sol.v : -sol.v; const yCentre = -inst.h * (x / inst.u); const yParallel = inst.h - (inst.h / inst.f) * x; | |
| if (Math.abs(yCentre - yParallel) > 1e-6) p.push(`rays do not meet: ${yCentre} vs ${yParallel}`); | |
| } | |
| return p; | |
| } | |
| // ---------- plausibility ---------- | |
| const nearEdge = (x, sf = 2) => { const p = Math.pow(10, Math.floor(Math.log10(Math.abs(x))) - (sf - 1)); const fr = (Math.abs(x) / p) % 1; return Math.abs(fr - 0.5) < 0.15; }; | |
| export function checkInstance(inst) { | |
| const c = new Checks(`optics:${inst.scenario}`); const sol = solve(inst); | |
| if (sol.kind === "ray") { | |
| c.ok("angle of incidence a whole number of degrees, 10°–80°", Number.isInteger(inst.i) && inst.i >= 10 && inst.i <= 80, `i ${inst.i}`); | |
| c.ok("refractive index from a real material", MEDIA.some((m) => m.n === inst.n), `n ${inst.n}`); | |
| if (!sol.tir && sol.r != null) c.ok("refraction angle not on a rounding edge (2 s.f.)", !nearEdge(sol.r), `r ${sol.r.toFixed(2)}`); | |
| c.ok("critical angle not on a rounding edge (2 s.f.)", !nearEdge(sol.c), `c ${sol.c.toFixed(2)}`); | |
| if (inst.scenario === "out") c.ok("clearly below the critical angle (≥ 4°)", inst.i <= sol.c - 4, `i ${inst.i}, c ${sol.c.toFixed(1)}`); | |
| if (inst.scenario === "tir") c.ok("clearly above the critical angle (≥ 4°)", inst.i >= sol.c + 4, `i ${inst.i}, c ${sol.c.toFixed(1)}`); | |
| if (inst.scenario === "into") c.ok("visible bending (i − r ≥ 5°)", inst.i - sol.r >= 5, `i ${inst.i}, r ${sol.r.toFixed(1)}`); | |
| if (inst.scenario === "out") c.ok("visible bending (r − i ≥ 5°) and r below 85°", sol.r - inst.i >= 5 && sol.r <= 85, `r ${sol.r && sol.r.toFixed(1)}`); | |
| } else { | |
| c.ok("focal length a round number of cm (5–20)", [5, 6, 8, 10, 12, 15, 20].includes(inst.f), `f ${inst.f}`); | |
| c.ok("object distance whole cm", Number.isInteger(inst.u) && inst.u > 0, `u ${inst.u}`); | |
| c.ok("object height 1–4 cm", inst.h >= 1 && inst.h <= 4, `h ${inst.h}`); | |
| c.ok("image exists", !sol.none, "object at F"); | |
| if (inst.scenario === "lens2F") c.ok("object exactly at 2F", inst.u === 2 * inst.f, "u"); | |
| if (inst.scenario === "lens") { | |
| c.ok("real image: object beyond F", inst.u > inst.f, "u <= f"); | |
| c.ok("object clearly away from F and 2F (≥ 15% of f)", Math.abs(inst.u - inst.f) >= 0.15 * inst.f && Math.abs(inst.u - 2 * inst.f) >= 0.15 * inst.f, `u ${inst.u}, f ${inst.f}`); | |
| c.ok("image within a drawable 5f", sol.v <= 5 * inst.f, `v ${sol.v.toFixed(1)}`); | |
| } else if (inst.scenario === "magnifier") { | |
| c.ok("virtual image: object inside F", inst.u < inst.f, "u >= f"); | |
| c.ok("object well inside F (between 0.3f and 0.5f) so the traced-back rays cross clearly", inst.u >= 0.3 * inst.f && inst.u <= 0.5 * inst.f, `u ${inst.u}`); | |
| } | |
| if (!sol.none && inst.scenario !== "lens2F") c.ok("image distance not on a rounding edge (2 s.f.)", !nearEdge(sol.v), `v ${sol.v.toFixed(2)}`); | |
| if (!sol.none && inst.scenario !== "lens2F") c.ok("image not exactly at F or 2F (answerable without drawing)", Math.abs(sol.v - inst.f) > 0.05 * inst.f && Math.abs(sol.v - 2 * inst.f) > 0.05 * inst.f, `v ${sol.v}`); | |
| } | |
| const x = crossCheck(inst, sol); c.ok("cross-check agrees", x.length === 0, x.join("; ")); | |
| return c; | |
| } | |
| // ---------- sampling ---------- | |
| export function sample(r, scenario, tries = 2000, stats = null) { | |
| for (let t = 0; t < tries; t++) { | |
| const inst = build(r, scenario); const c = checkInstance(inst); if (c.passed) return inst; | |
| if (stats) for (const f of c.failures) stats[f.name] = (stats[f.name] || 0) + 1; | |
| } | |
| return null; | |
| } | |
| function build(r, scenario) { | |
| if (scenario === "lens2F") { const f = r.pick([5, 6, 8, 10, 12, 15]); return { cls: ID, scenario, f, u: 2 * f, h: r.pick([1, 2, 3]) }; } | |
| if (scenario === "lens" || scenario === "magnifier") { | |
| const f = r.pick([5, 6, 8, 10, 12, 15, 20]); const h = r.pick([1, 2, 3, 4]); | |
| const u = scenario === "lens" ? r.int(Math.ceil(1.15 * f), Math.floor(4 * f)) : r.int(Math.ceil(0.3 * f), Math.floor(0.5 * f)); | |
| return { cls: ID, scenario, f, u, h }; | |
| } | |
| const m = r.pick(MEDIA); const i = r.int(10, 80); | |
| return { cls: ID, scenario, medium: m.name, n: m.n, i }; | |
| } | |
| // ---------- text ---------- | |
| const f2 = (x) => Number(x.toPrecision(2)); | |
| export function describe(inst) { | |
| if (inst.scenario === "lens" || inst.scenario === "lens2F" || inst.scenario === "magnifier") return `An object ${inst.h} cm tall stands ${inst.u} cm from a thin converging lens of focal length ${inst.f} cm.`; | |
| if (inst.scenario === "into") return `A ray of light in air strikes the flat surface of ${inMedium(inst.medium)} (refractive index ${inst.n}) at an angle of incidence of ${inst.i}°.`; | |
| return `A ray of light inside ${inMedium(inst.medium)} (refractive index ${inst.n}) meets the flat surface with air at an angle of incidence of ${inst.i}°.`; | |
| } | |
| const fig = (inst, show, extra = {}) => ({ cls: ID, inst, show, ...extra }); | |
| function mcq(r, { key, distractors }) { | |
| const all = r.shuffle([{ text: key, key: true }, ...distractors]); | |
| return { options: all.map((o) => o.text), answer: all.findIndex((o) => o.key), distractors: all.filter((o) => !o.key).map((o) => ({ text: o.text, why: o.why, misconception_id: o.mis })) }; | |
| } | |
| const deg = (x) => `${f2(x)}°`; | |
| // ---------- questions ---------- | |
| export function questions(inst, r) { | |
| const qs = []; const sol = solve(inst); const D = describe(inst); | |
| if (sol.kind === "ray") { | |
| if (inst.scenario === "into" || inst.scenario === "out") { | |
| // 1. direction: (towards / away from the normal) x (slows down / speeds up), a full 2x2 | |
| const ok = `It bends ${sol.bend === "towards" ? "towards" : "away from"} the normal and ${sol.speed}`; | |
| const opts = [["towards", "slows down"], ["towards", "speeds up"], ["away from", "slows down"], ["away from", "speeds up"]].map(([b, s]) => `It bends ${b} the normal and ${s}`); | |
| qs.push({ type: "mcq", kind: "bend", group: "core", rule: "0625-3.2-A14", prompt: `${D} What happens to the ray as it crosses the surface?`, | |
| ...mcq(r, { key: ok, distractors: opts.filter((o) => o !== ok).map((o) => ({ text: o, why: "direction or speed change reversed", mis: "0625-3.2-M06" })) }), | |
| explanation: inst.scenario === "into" ? `Glass, water and diamond are optically denser than air: light slows down entering them and bends towards the normal (r < i).` : `Leaving the ${inst.medium} for air, light speeds up and bends away from the normal: the angle in air is larger.`, | |
| atom_ids: ["0625-3.2-A13", "0625-3.2-A14"], misconception_id: "0625-3.2-M06", figure: fig(inst, ["incident"]), difficulty: 1, signature: `bend:${inst.scenario}` }); | |
| // 2. the refracted angle (numeric, 2 s.f.) | |
| qs.push({ type: "numeric", kind: "angle", group: "core", rule: "0625-3.2-A24", | |
| prompt: `${D} Calculate the angle of ${inst.scenario === "into" ? "refraction" : "refraction in the air"}. Give your answer in degrees to 2 significant figures.`, | |
| answer: Number(sol.r.toFixed(4)), sf: 2, unit: "°", allowFraction: true, | |
| explanation: inst.scenario === "into" ? `n = sin i / sin r, so sin r = sin ${inst.i}° / ${inst.n} = ${(sinD(inst.i) / inst.n).toFixed(3)}; r = ${sol.r.toFixed(1)}° ≈ ${deg(sol.r)}.` : `Leaving the ${inst.medium === "water" ? "water" : "block"}: sin(angle in air) = n × sin(angle inside) = ${inst.n} × sin ${inst.i}° = ${(inst.n * sinD(inst.i)).toFixed(3)}; r = ${sol.r.toFixed(1)}° ≈ ${deg(sol.r)}.`, | |
| atom_ids: inst.scenario === "into" ? ["0625-3.2-A23", "0625-3.2-A24"] : ["0625-3.2-A25"], misconception_id: "0625-3.2-M07", figure: fig(inst, ["incident"]), difficulty: 2, signature: `angle:${inst.scenario}` }); | |
| } | |
| // 3. critical angle (numeric) | |
| if (inst.scenario !== "into") qs.push({ type: "numeric", kind: "critical", group: "core", rule: "0625-3.2-A36", | |
| prompt: `Light travels inside ${inst.medium} (refractive index ${inst.n}) towards a boundary with air. Calculate the critical angle. Give your answer in degrees to 2 significant figures.`, | |
| answer: Number(sol.c.toFixed(4)), sf: 2, unit: "°", allowFraction: true, explanation: `n = 1 / sin c, so sin c = 1 / ${inst.n} = ${(1 / inst.n).toFixed(3)} and c = ${sol.c.toFixed(1)}° ≈ ${deg(sol.c)}.`, | |
| atom_ids: ["0625-3.2-A35", "0625-3.2-A36"], misconception_id: "0625-3.2-M11", figure: fig(inst, ["boundary"]), difficulty: 2, signature: "critical" }); | |
| // 4. what happens at the boundary from inside (refracts or total internal reflection), with the angles | |
| if (inst.scenario !== "into") { | |
| const rOut = sol.tir ? asinD(Math.min(1, inst.n * sinD(Math.min(inst.i, sol.c - 1)))) : sol.r; | |
| const key = sol.tir ? `It is totally internally reflected, at ${inst.i}° to the normal` : `It refracts into the air at about ${deg(sol.r)} to the normal`; | |
| const ds = sol.tir ? [ | |
| { text: `It refracts into the air at about ${deg(asinD(sinD(inst.i) / inst.n))} to the normal`, why: "missed that i is above the critical angle, and divided by n", mis: "0625-3.2-M10" }, | |
| { text: `It refracts and runs along the boundary at 90° to the normal`, why: "that happens only at i = c", mis: "0625-3.2-M10" }, | |
| { text: `It is totally internally reflected, at ${90 - inst.i}° to the normal`, why: "measured from the surface", mis: "0625-3.2-M01" }] | |
| : [{ text: `It is totally internally reflected, at ${inst.i}° to the normal`, why: "i is below the critical angle", mis: "0625-3.2-M10" }, | |
| { text: `It refracts into the air at about ${deg(asinD(sinD(inst.i) / inst.n))} to the normal`, why: "bent towards the normal on leaving", mis: "0625-3.2-M06" }, | |
| { text: `It refracts and runs along the boundary at 90° to the normal`, why: "that happens only at i = c", mis: "0625-3.2-M10" }]; | |
| qs.push({ type: "mcq", kind: "boundary", group: "core", rule: "0625-3.2-A30", prompt: `${D} What happens to the ray at the surface?`, ...mcq(r, { key, distractors: ds }), | |
| explanation: `Critical angle c = sin⁻¹(1/${inst.n}) = ${sol.c.toFixed(1)}°. Here i = ${inst.i}° is ${sol.tir ? "greater than c, so all the light is reflected back inside (total internal reflection), with angle of reflection = angle of incidence" : `less than c, so the ray refracts out, bending away from the normal: sin r = ${inst.n} × sin ${inst.i}°, r ≈ ${deg(sol.r)}`}.`, | |
| atom_ids: ["0625-3.2-A29", "0625-3.2-A30", "0625-3.2-A31"], misconception_id: "0625-3.2-M10", figure: fig(inst, ["incident"]), difficulty: 3, signature: `boundary:${inst.scenario}` }); | |
| } | |
| // 5. find the error in a drawn diagram (exactly one thing wrong) | |
| { | |
| const errs = []; | |
| if (inst.scenario === "into") errs.push({ rule: "away", text: "The ray bends the wrong way at the surface." }); | |
| if (inst.scenario === "out") errs.push({ rule: "tirWrong", text: "The light should refract out, not be reflected." }); | |
| if (inst.scenario === "tir") errs.push({ rule: "refractWrong", text: "All the light should be reflected; none escapes." }); | |
| errs.push({ rule: "surface", text: "The angle is measured from the surface, not the normal." }); | |
| let w = r.pick(errs); | |
| if (r.next() < 0.2) w = { rule: "none", text: "Nothing: the diagram is correct." }; // sometimes the diagram is right | |
| const all = [ | |
| { rule: "away", text: "The ray bends the wrong way at the surface." }, | |
| { rule: "surface", text: "The angle is measured from the surface, not the normal." }, | |
| { rule: "normal", text: "The normal is not at 90° to the surface." }, | |
| { rule: "none", text: "Nothing: the diagram is correct." }, | |
| { rule: "tirWrong", text: "The light should refract out, not be reflected." }, | |
| { rule: "refractWrong", text: "All the light should be reflected; none escapes." }, | |
| ].filter((x) => x.rule !== w.rule && (inst.scenario === "into" ? !["tirWrong", "refractWrong"].includes(x.rule) : x.rule !== "away")); | |
| const ds = w.rule === "none" ? r.shuffle(all).slice(0, 3) : r.shuffle(all.filter((x) => x.rule !== "none")).slice(0, 2).concat(all.filter((x) => x.rule === "none")).slice(0, 3); | |
| if (ds.length === 3) qs.push({ type: "mcq", kind: "error", statementOptions: true, rule: "0625-3.2-A14", prompt: `${D} A student drew the diagram shown. What is wrong with it?`, | |
| ...mcq(r, { key: w.text, distractors: ds.map((x) => ({ text: x.text, why: "not the fault in this diagram", mis: "0625-3.2-M01" })) }), | |
| explanation: `${{ away: "Entering a denser medium the ray slows and must bend towards the normal.", surface: `The marked angle is measured from the surface; measured from the normal it is ${inst.i}°.`, none: `Every part is right: angles from the normal, the ray bends the correct way${sol.tir ? ", and with i above the critical angle all the light is reflected" : ""}.`, tirWrong: `i = ${inst.i}° is below the critical angle (${sol.c.toFixed(1)}°), so the ray refracts out.`, refractWrong: `i = ${inst.i}° is above the critical angle (${sol.c.toFixed(1)}°), so all the light is reflected inside.` }[w.rule] || w.text} `, | |
| atom_ids: w.rule === "surface" ? ["0625-3.2-A03"] : ["tirWrong", "refractWrong"].includes(w.rule) ? ["0625-3.2-A30"] : ["0625-3.2-A14"], misconception_id: w.rule === "surface" ? "0625-3.2-M01" : "0625-3.2-M06", figure: fig(inst, ["drawn"], { wrong: w.rule === "none" ? null : w.rule }), difficulty: 2, signature: `error:${w.rule}` }); | |
| } | |
| } else { | |
| // lens questions | |
| const sizeWord = sol.size; const natureKey = `${sol.nature}, ${sizeWord}`; | |
| // 1. image characteristics: (real inverted / virtual upright) x (enlarged / diminished) — the same-size case is excluded by the checks | |
| const grid = ["real, inverted, enlarged", "real, inverted, diminished", "real, inverted, same size", "virtual, upright, enlarged"]; | |
| qs.push({ type: "mcq", kind: "image", group: "core", rule: "0625-3.2-A50", prompt: `${D} Describe the image.`, | |
| ...mcq(r, { key: natureKey, distractors: grid.filter((g) => g !== natureKey).map((g) => ({ text: g, why: "image type or size mixed up", mis: "0625-3.2-M14" })) }), | |
| explanation: `f = ${inst.f} cm, so F is ${inst.f} cm and 2F is ${2 * inst.f} cm from the lens. The object at ${inst.u} cm is ${inst.u < inst.f ? "inside F: the rays leave the lens diverging and only appear to come from a point behind the object, giving a virtual, upright, enlarged image (a magnifying glass)" : inst.u === 2 * inst.f ? "exactly at 2F: the image is real, inverted and the same size, at 2F on the other side" : inst.u < 2 * inst.f ? "between F and 2F: the image is real, inverted and enlarged, beyond 2F on the other side (a projector)" : "beyond 2F: the image is real, inverted and diminished, between F and 2F on the other side (a camera, the eye)"}.`, | |
| atom_ids: ["0625-3.2-A49", "0625-3.2-A50"].concat(sol.real ? [] : ["0625-3.2-A53"]), misconception_id: "0625-3.2-M14", figure: fig(inst, ["object"]), difficulty: 2, signature: `image:${inst.scenario}` }); | |
| // 2. image distance from the scale drawing (numeric, drawing tolerance) | |
| qs.push({ type: "numeric", kind: "distance", group: "core", rule: "0625-3.2-A51", | |
| prompt: `${D} Draw a scale ray diagram. How far from the lens is the image? Give your answer in cm.`, | |
| answer: Number(sol.v.toFixed(2)), tolerance: 0.6 / sol.v, unit: "cm", allowFraction: true, tier: sol.real ? "core" : "supplement", | |
| explanation: `Draw the ray parallel to the axis (then through F) and the ray through the centre of the lens. ${sol.real ? "They cross" : "They spread apart; extend them backwards and they meet"} about ${f2(sol.v)} cm from the lens${sol.real ? " on the far side" : " on the same side as the object"}. (Answers within 0.6 cm of this are accepted, allowing for drawing accuracy.)`, | |
| atom_ids: ["0625-3.2-A47", "0625-3.2-A51"].concat(sol.real ? [] : ["0625-3.2-A53"]), misconception_id: "0625-3.2-M13", figure: fig(inst, ["object"]), difficulty: 3, signature: `distance:${inst.scenario}` }); | |
| // 3. find the error in a drawn ray diagram | |
| { | |
| const errs = [{ rule: "parallel", text: "The parallel ray misses F after the lens." }, | |
| { rule: "centre", text: "The ray through the centre is bent." }]; | |
| let w = r.pick(errs); let others = [errs.find((e) => e !== w), { rule: "focal", text: "F is drawn at 2f instead of f." }, { rule: "none", text: "Nothing: the diagram is correct." }]; | |
| if (r.next() < 0.2) { w = { rule: "none", text: "Nothing: the diagram is correct." }; others = [...errs, { rule: "focal", text: "F is drawn at 2f instead of f." }]; } | |
| qs.push({ type: "mcq", kind: "error", statementOptions: true, rule: "0625-3.2-A47", prompt: `${D} A student drew the ray diagram shown. What is wrong with it?`, | |
| ...mcq(r, { key: w.text, distractors: others.map((x) => ({ text: x.text, why: "not the fault in this diagram", mis: "0625-3.2-M13" })) }), | |
| explanation: `${w.rule === "parallel" ? "After the lens the ray that arrived parallel to the axis must pass through F." : w.rule === "centre" ? "The ray through the optical centre goes straight on without bending." : "Every ray is drawn correctly."} The standard rays: parallel to the axis then through F; through the centre undeviated; through F then parallel.`, | |
| atom_ids: ["0625-3.2-A47"], misconception_id: "0625-3.2-M13", figure: fig(inst, ["drawn"], { wrong: w.rule === "none" ? null : w.rule }), difficulty: 2, signature: `error:${w.rule}` }); | |
| } | |
| } | |
| // syllabus tier: Snell's law and critical-angle calculations and virtual images are Supplement (Extended) | |
| const sup = (q) => ["angle", "critical"].includes(q.kind) || inst.scenario === "magnifier" || (q.kind === "boundary"); | |
| if (inst.scenario === "lens2F") return qs.filter((q) => q.kind === "image").map((q) => ({ ...q, marks: 1, tier: "core" })); // the same-size case is about the description | |
| return qs.map((q) => ({ ...q, marks: 1, tier: q.tier || (sup(q) ? "supplement" : "core") })); | |
| } | |
| // "What if" questions: change one component and predict | |
| export function whatIfQuestions(inst, r) { | |
| const qs = []; const sol = solve(inst); const D = describe(inst); | |
| if (sol.kind === "ray" && inst.scenario === "into") { | |
| // a denser medium: r gets smaller (2x2: smaller/larger x towards/away) | |
| const denser = MEDIA.filter((m) => m.n > inst.n + 0.1); | |
| if (denser.length) { | |
| const m = r.pick(denser); const ni = { ...inst, medium: m.name, n: m.n }; | |
| if (checkInstance(ni).passed) { | |
| const key = "The angle of refraction is smaller: the ray bends more towards the normal"; | |
| const ds = ["The angle of refraction is larger: the ray bends less towards the normal", "The angle of refraction is smaller: the ray now bends away from the normal", "The angle of refraction is larger: the ray now bends away from the normal"]; | |
| qs.push({ type: "mcq", kind: "whatif", group: "core", change: "denser", rule: "0625-3.2-A22", prompt: `${D} The block is replaced by ${m.name} (refractive index ${m.n}), with the same angle of incidence. How does the refracted ray change?`, | |
| ...mcq(r, { key, distractors: ds.map((t) => ({ text: t, why: "a higher index bends light more, still towards the normal", mis: "0625-3.2-M08" })) }), | |
| explanation: `A higher refractive index means light slows more and bends more: sin r = sin ${inst.i}° / ${m.n} gives r ≈ ${deg(solve(ni).r)} instead of ${deg(sol.r)}.`, | |
| atom_ids: ["0625-3.2-A22", "0625-3.2-A24"], misconception_id: "0625-3.2-M08", figure: fig(inst, ["incident"], { after: ni }), difficulty: 3, signature: "whatif:denser", marks: 1 }); | |
| } | |
| } | |
| } | |
| if (sol.kind === "ray" && inst.scenario === "out") { | |
| // raise i above c: total internal reflection | |
| const below = Math.floor(sol.c) - 4; | |
| for (const goTir of [true, false]) { | |
| if (!goTir && below <= inst.i + 3) continue; | |
| const i2 = goTir ? Math.ceil(sol.c) + 6 : r.int(inst.i + 3, below); const ni = { ...inst, i: i2, scenario: goTir ? "tir" : "out" }; | |
| if (i2 > 80 || !checkInstance(ni).passed) continue; | |
| const opts = [`It is totally internally reflected`, `It refracts out, bending further from the normal than before`, `It refracts out at a smaller angle to the normal than before`, `It refracts out along the surface (at 90° to the normal)`]; | |
| const why = { [opts[0]]: "thought any bigger angle gives total internal reflection", [opts[1]]: "missed that i is now above the critical angle", [opts[2]]: "thought a bigger angle inside gives a smaller angle in air", [opts[3]]: "that happens only at exactly the critical angle" }; | |
| const key = goTir ? opts[0] : opts[1]; const ds = opts.filter((o) => o !== key); | |
| qs.push({ type: "mcq", kind: "whatif", group: "core", change: goTir ? "tir" : "rise", rule: "0625-3.2-A30", prompt: `${D} The angle of incidence is increased to ${i2}°. What happens to the ray now?`, | |
| ...mcq(r, { key, distractors: ds.map((t) => ({ text: t, why: why[t], mis: "0625-3.2-M10" })) }), | |
| explanation: goTir ? `c = ${sol.c.toFixed(1)}°. At ${i2}° the angle of incidence is above c, so no light refracts out: it is all reflected inside.` : `c = ${sol.c.toFixed(1)}°. ${i2}° is still below c, so the ray still refracts out; a bigger angle inside gives a bigger angle in air (sin r = ${inst.n} × sin ${i2}°, r ≈ ${deg(solve(ni).r)}).`, | |
| atom_ids: ["0625-3.2-A30", "0625-3.2-A31"], misconception_id: "0625-3.2-M10", figure: fig(inst, ["incident"], { after: ni }), difficulty: 3, signature: `whatif:${goTir ? "tir" : "rise"}`, marks: 1 }); | |
| } | |
| } | |
| if (sol.kind === "lens" && inst.scenario === "lens" && inst.u < 2 * inst.f) { | |
| // move the object out beyond 2F: the image becomes diminished | |
| const u2 = Math.round(3 * inst.f); const ni = { ...inst, u: u2 }; | |
| if (checkInstance(ni).passed) { | |
| const key = "real, inverted, diminished"; const grid = ["real, inverted, enlarged", "virtual, upright, enlarged", "real, inverted, same size"]; | |
| qs.push({ type: "mcq", kind: "whatif", group: "core", change: "beyond2F", rule: "0625-3.2-A50", prompt: `${D} The object is moved back to ${u2} cm from the lens. Describe the new image.`, | |
| ...mcq(r, { key, distractors: grid.map((t) => ({ text: t, why: "beyond 2F the image is smaller than the object", mis: "0625-3.2-M14" })) }), | |
| explanation: `${u2} cm is beyond 2F (${2 * inst.f} cm): the image is real, inverted and diminished, between F and 2F on the far side (a camera).`, | |
| atom_ids: ["0625-3.2-A49", "0625-3.2-A50"], misconception_id: "0625-3.2-M14", figure: fig(inst, ["object"], { after: ni }), difficulty: 3, signature: "whatif:beyond2F", marks: 1, tier: "core" }); | |
| } | |
| } | |
| if (sol.kind === "lens" && inst.scenario === "lens" && inst.u > 2 * inst.f) { | |
| // move the object inside F: the image becomes virtual and upright | |
| const u2 = Math.max(Math.round(0.4 * inst.f), 2); const ni = { ...inst, u: u2, scenario: "magnifier" }; | |
| if (checkInstance(ni).passed) { | |
| const key = "virtual, upright, enlarged"; const grid = ["real, inverted, enlarged", "real, inverted, diminished", "virtual, upright, diminished"]; | |
| qs.push({ type: "mcq", kind: "whatif", group: "core", change: "insideF", rule: "0625-3.2-A53", prompt: `${D} The object is moved to ${u2} cm from the lens. Describe the new image.`, | |
| ...mcq(r, { key, distractors: grid.map((t) => ({ text: t, why: "a converging lens does not always make a real image", mis: "0625-3.2-M14" })) }), | |
| explanation: `${u2} cm is inside F (${inst.f} cm): the rays leave the lens diverging, so the image is virtual (on the same side as the object), upright and enlarged: the lens acts as a magnifying glass.`, | |
| atom_ids: ["0625-3.2-A52", "0625-3.2-A53", "0625-3.2-A54"], misconception_id: "0625-3.2-M14", figure: fig(inst, ["object"], { after: ni }), difficulty: 3, signature: "whatif:insideF", marks: 1 }); | |
| } | |
| } | |
| const TIER = { denser: "core", beyond2F: "core", tir: "supplement", rise: "supplement", insideF: "supplement" }; | |
| return qs.map((q) => ({ ...q, tier: q.tier || TIER[q.change] || "supplement" })); | |
| } | |
| export function checkClassQuestion(q) { | |
| const c = new Checks(q.id); const inst = q.figure.inst; | |
| c.ok("instance passes", checkInstance(inst).passed, "instance fails checks"); | |
| if (q.figure.after) c.ok("changed instance passes", checkInstance(q.figure.after).passed, "changed instance fails"); | |
| if (q.type === "numeric" && q.sf) c.ok("answer not on a rounding edge", !nearEdge(q.answer, q.sf), `answer ${q.answer}`); | |
| return c; | |
| } | |