Skip to content

An AI skill that turns books into animated, narrated, interactive web experiences

feat(linear-algebra): turn a line by its dots

yzra41c92d852db
diff --git a/site/linear-algebra/ch01/index.html b/site/linear-algebra/ch01/index.html
index e71256c..9a84965 100644
--- a/site/linear-algebra/ch01/index.html
+++ b/site/linear-algebra/ch01/index.html
@@ -382,7 +382,7 @@ const BEATS = [
const sys = S.sys = oneLine(p, 1, 2, 2);
drop(sys, 0, 3, .5, COL.task); drop(sys, 3, 0, .7, COL.task, '(3, 0)', -14, 56, undefined, 'end');
}, { ask: done => quiz(CARD, [{
- id: 'c-make', prompt: ['Make the line pass through ', $m('(0, 3)'), ' and ', $m('(3, 0)'), '. Drag a number sideways, or drag the line.'],
+ id: 'c-make', prompt: ['Make the line pass through ', $m('(0, 3)'), ' and ', $m('(3, 0)'), '. Drag a number sideways, drag the line to slide it, or drag one of its dots to turn it.'],
build: lsTask(S.sys, v => (v.a00 || v.a01) && v.a01 * 3 === v.b0 && v.a00 * 3 === v.b0,
'x + y = 3 (or any multiple, like 2x + 2y = 6): both points add up to 3.',
'Put each point into your equation. Both must give the right side exactly. Try equal numbers in front of x and y.',
@@ -438,7 +438,7 @@ const BEATS = [
S.sys = oneLine(p, 2, -1, 0);
const hint = text(p, 'try: make it flat, make it pass through (0, 0), make it steep', 90, 330, m('go'), { size: 28, fill: COL.dim });
hide(hint, m('go') + 6, .6);
- }, { ask: playAsk(() => S.sys, { tip: 'Your line. Change any number, drag the line, or draw on the board. Nothing is marked.' }) }],
+ }, { ask: playAsk(() => S.sys, { tip: 'Your line. Change any number, slide the line, turn it by its dots, or draw on the board. Nothing is marked.' }) }],
['finish', 'Finished', () => { panel(0); }, { ask: finishCard }],
].map(([id, title, run, extra]) => ({ id, title, run, ...extra }));
diff --git a/site/linear-algebra/ch21/index.html b/site/linear-algebra/ch21/index.html
index 3caef96..6e8154b 100644
--- a/site/linear-algebra/ch21/index.html
+++ b/site/linear-algebra/ch21/index.html
@@ -33,7 +33,7 @@ const thick = (sys, i, t0) => { tw(sys.lines[i].l, { 'a_stroke-width': 10 }, t0,
// 2D: the plane on the right (moved over a little to leave room for three matrices), no equations (the product is them).
const two = (p, { A, b }, caption, { t0 = .1, E = null } = {}) => {
const sys = linsys(p, { A, b, eq: [90, 200], mat: null, sol: false, plane: { cx: 1215, u: 50 }, t0 });
- sys.E.style.display = 'none';
+ sys.E.style.visibility = 'hidden'; // hidden, not display: none: hidden text still measures, so a seek replays the same frame
if (caption) say(p, [caption], 70, 175, t0 + .1, { size: 28, fill: COL.dim });
const em = emat(p, sys, { A, b, E, at: AT2, size: 38, cw: 64, ecw: 56, rh: 62, gap: 66, t0 });
return { sys, em };
@@ -41,7 +41,7 @@ const two = (p, { A, b }, caption, { t0 = .1, E = null } = {}) => {
// 3D: the space on the right, smaller and further out, three 3 × 3 tables on the left.
const three = (p, { A, b }, caption, { t0 = .1, E = null, view = { yaw: -40, pitch: 20 } } = {}) => {
const sys = lin3(p, { A, b, eq: [90, 190], mat: null, sol: false, space: { cx: 1262, size: 520, ...view }, t0 });
- sys.E.style.display = 'none';
+ sys.E.style.visibility = 'hidden'; // hidden, not display: none: hidden text still measures, so a seek replays the same frame
if (caption) say(p, [caption], 64, 160, t0 + .1, { size: 26, fill: COL.dim });
const em = emat(p, sys, { A, b, E, at: AT3, size: 32, cw: 52, ecw: 44, rh: 54, gap: 64, t0 });
return { sys, em, V: sys.space };
@@ -274,7 +274,7 @@ const BEATS = [
const p = panel(0);
text(p, 'Both steps in one matrix', 60, 110, .2, { size: 40, weight: 600 });
const sys = lin3(p, { A: LEC.A, b: LEC.b, eq: [90, 190], mat: null, sol: false, space: { cx: 1262, size: 520, yaw: -30, pitch: 22 } });
- sys.E.style.display = 'none';
+ sys.E.style.visibility = 'hidden'; // hidden, not display: none: hidden text still measures, so a seek replays the same frame
const V = sys.space;
V.sway(5, 13);
const sym = G(p, { o: 0 }); show(sym, m('write'), .4);
diff --git a/site/linear-algebra/lib/linsys.js b/site/linear-algebra/lib/linsys.js
index 4bc870c..807e8a3 100644
--- a/site/linear-algebra/lib/linsys.js
+++ b/site/linear-algebra/lib/linsys.js
@@ -220,7 +220,7 @@ function linsys(p, { A = [[2, -1], [-1, 2]], b = [0, 3], eq = [90, 250], mat = [
});
const tipLabels = [0, 1, 2].map(() => G(COLV, {}));
- // Drag in the plane: a line slides to pass through the pointer (its right side changes);
+ // Drag in the plane: a line slides to pass through the pointer (its right side changes; its handles turn it, below);
// an arrow tip goes to the nearest grid point (a column of A, or b).
const dragIn = (el, onPoint) => {
let on = false;
@@ -231,6 +231,53 @@ function linsys(p, { A = [[2, -1], [-1, 2]], b = [0, 3], eq = [90, 250], mat = [
};
lines.forEach(({ hit }, i) => dragIn(hit, ([X, Y]) => can(`b${i}`) && edit({ [`b${i}`]: clamp(`b${i}`, Math.round(v[`a${i}0`] * X + v[`a${i}1`] * Y)) })));
const grid = (n, lo, hi) => Math.max(lo, Math.min(hi, Math.round(n)));
+ // Two handles on each line while the reader may change its whole equation: drag one and the line turns about the
+ // other. Both sit on whole-number points, so the new equation has whole numbers (in lowest terms).
+ const rowKeys = i => [`a${i}0`, `a${i}1`, `b${i}`], rowFree = i => rowKeys(i).every(can);
+ const knobPos = RI.map(() => [[0, 0], [0, 0]]);
+ let turning = null; // { i, k, pivot, at } while a handle is dragged; the handles then stay where the hand put them
+ const knobs = lines.map(({ g }, i) => [0, 1].map(k => {
+ const h = G(g, { o: 0 });
+ mk('circle', { r: 24, fill: 'transparent' }, h);
+ mk('circle', { r: 10, fill: COL.board, stroke: LS_ROW[i], 'stroke-width': 4 }, h);
+ h.style.pointerEvents = 'none';
+ h.style.touchAction = 'none';
+ h.addEventListener('pointerdown', e => {
+ if (!rowFree(i)) return;
+ e.preventDefault();
+ turning = { i, k, pivot: knobPos[i][1 - k], at: knobPos[i][k] };
+ h.setPointerCapture(e.pointerId);
+ });
+ h.addEventListener('pointermove', e => {
+ if (!turning) return;
+ const [X, Y] = pt(e), q = [grid(X, bx[0], bx[1]), grid(Y, by[0], by[1])], p0 = turning.pivot;
+ if (q[0] === p0[0] && q[1] === p0[1]) return;
+ let a = q[1] - p0[1], b2 = p0[0] - q[0], c = a * p0[0] + b2 * p0[1];
+ const g2 = gcd(gcd(a, b2), c) || 1;
+ a /= g2; b2 /= g2; c /= g2;
+ if (a * v[`a${i}0`] + b2 * v[`a${i}1`] < 0) { a = -a; b2 = -b2; c = -c; } // keep the signs the reader had, where the turn allows
+ if (Math.abs(a) > RANGE[`a${i}0`] || Math.abs(b2) > RANGE[`a${i}1`] || Math.abs(c) > RANGE[`b${i}`]) return;
+ turning.at = q;
+ edit({ [`a${i}0`]: a, [`a${i}1`]: b2, [`b${i}`]: c });
+ draw(); // the handles follow the hand even when the equation did not change
+ });
+ const end = () => { if (turning) { turning = null; draw(); } };
+ h.addEventListener('pointerup', end);
+ h.addEventListener('lostpointercapture', end);
+ return h;
+ }));
+ // Where the handles sit: two whole-number points of the line, a quarter and three quarters along those in the grid
+ // (or a quarter and three quarters along the drawn line, when it has fewer than two).
+ const knobsFor = i => {
+ const a = v[`a${i}0`], b2 = v[`a${i}1`], c = v[`b${i}`], pts = [];
+ if ([a, b2, c].every(Number.isInteger)) {
+ if (b2) { for (let X = bx[0]; X <= bx[1]; X++) if ((c - a * X) % b2 === 0) { const Y = (c - a * X) / b2; if (Y >= by[0] && Y <= by[1]) pts.push([X, Y]); } }
+ else if (a && c % a === 0) for (let Y = by[0]; Y <= by[1]; Y++) pts.push([c / a, Y]);
+ }
+ if (pts.length >= 2) { const m = pts.length, j = Math.floor((m - 1) / 4), k = Math.max(j + 1, Math.ceil(3 * (m - 1) / 4)); return [pts[j], pts[k]]; }
+ const cl = clipLine(a, b2, c);
+ return cl ? [[cl[0] + (cl[2] - cl[0]) / 4, cl[1] + (cl[3] - cl[1]) / 4], [cl[0] + 3 * (cl[2] - cl[0]) / 4, cl[1] + 3 * (cl[3] - cl[1]) / 4]] : null;
+ };
const tipKeys = { b: ['b0', 'b1'], col0: ['a00', 'a10'], col1: ['a01', 'a11'] };
tips.forEach(({ g, k }) => dragIn(g, ([X, Y]) => {
if (!tipKeys[k].every(can)) return;
@@ -262,6 +309,8 @@ function linsys(p, { A = [[2, -1], [-1, 2]], b = [0, 3], eq = [90, 250], mat = [
lines.forEach(({ hit, l }, i) => {
const c = clipLine(v[`a${i}0`], v[`a${i}1`], v[`b${i}`]), d = c ? `M${PX(c[0])} ${PY(c[1])}L${PX(c[2])} ${PY(c[3])}` : '';
if (l.getAttribute('d') !== d) { l.setAttribute('d', d); hit.setAttribute('d', d); }
+ const kp = turning && turning.i === i ? (turning.k ? [turning.pivot, turning.at] : [turning.at, turning.pivot]) : knobsFor(i);
+ if (kp) { knobPos[i] = kp; kp.forEach(([x, y], k) => put(knobs[i][k], { x: PX(x), y: PY(y) })); }
});
const s = solve(), inGrid = s.x !== undefined && s.x >= bx[0] && s.x <= bx[1] && s.y >= by[0] && s.y <= by[1];
put(meet, { x: inGrid ? PX(s.x) : 0, y: inGrid ? PY(s.y) : 0, o: inGrid ? 1 : 0 });
@@ -361,9 +410,11 @@ function linsys(p, { A = [[2, -1], [-1, 2]], b = [0, 3], eq = [90, 250], mat = [
if (ok) s.box.setAttribute('tabindex', 0); else s.box.removeAttribute('tabindex');
}
lines.forEach(({ hit }, i) => { hit.style.cursor = can(`b${i}`) ? 'move' : ''; });
+ knobs.forEach((ks, i) => ks.forEach(h => { const ok = rowFree(i); put(h, { o: ok ? 1 : 0 }); h.style.pointerEvents = ok ? '' : 'none'; h.style.cursor = ok ? 'grab' : ''; }));
tips.forEach(({ g, ring, k }) => { const ok = tipKeys[k].every(can); g.style.cursor = ok ? 'grab' : ''; ring.style.opacity = ok ? 1 : 0; });
},
set: edit, // at once, for a reader's action or an answer shown
+ knobs, knobPos, // the turning handles of each line and their grid points (for tests)
num: key => slots.filter(s => s.key === key).map(s => s.t), // every text that shows this value (to flash it)
eqRows,
onChange(f) { listeners.push(f); },
@@ -380,6 +431,6 @@ const lsTask = (sys, test, why, hint, reveal, { only, trail = false } = {}) => (
check: () => { const v = sys.v, s = sys.solve(), ok = test(v, s); api.grade(ok, ok ? why : typeof hint === 'function' ? hint(v, s) : hint); }, // a hint may say where the reader is now
reveal: () => { reveal?.(); return why; },
lock: () => sys.live(false),
- hint: [kbd('Tab'), ' pick a number ', kbd('↑'), kbd('↓'), ' change it, or drag in the picture'],
+ hint: [kbd('Tab'), ' pick a number ', kbd('↑'), kbd('↓'), ' change it; drag a line to slide it, its dots to turn it'],
};
};
diff --git a/site/linear-algebra/lib/play.js b/site/linear-algebra/lib/play.js
index 06dfd14..0adc830 100644
--- a/site/linear-algebra/lib/play.js
+++ b/site/linear-algebra/lib/play.js
@@ -60,7 +60,7 @@ function playAsk(getSys, { pos = { x: 40, y: 430, w: 720, cls: 'side' }, tip = '
const acts = h('div', 'acts', [bDraw, bInk, bClear, bReset, bOn]);
acts.style.flexWrap = 'wrap';
c.replaceChildren(h('div', 'head', [h('p', 'kicker', 'Playground · no score'), h('p', 'prompt', [rich([tip], 24)]),
- h('p', 'keys', ['Drag numbers sideways or use ', kbd('Tab'), kbd('↑'), kbd('↓'), '; drag lines and arrows; ', kbd('D'), ' to draw on the board.'])]), acts);
+ h('p', 'keys', ['Drag numbers sideways or use ', kbd('Tab'), kbd('↑'), kbd('↓'), '; drag a line to slide it, its dots to turn it, arrows by their tips; ', kbd('D'), ' to draw on the board.'])]), acts);
paint();
P.keys = playKeys = e => {
if (P.gen !== gen) return false;