Skip to content

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

11.3 KB
Raw Blame
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Slope — Elementary Algebra</title>
<link rel="stylesheet" href="../lib/engine.css">
<link rel="expect" href="#bar" blocking="render"><!-- hold the first frame until the player exists, so the page transition can run -->
</head>
<body>
<script src="../lib/engine.js"></script>
<script src="../lib/math.js"></script>
<script src="audio/en/timings.js"></script>
<script>
'use strict';
const CHAPTER = { number: 33, title: 'Slope', minutes: 5, next: '../ch34/' };
const RI = COL.irr, RU = COL.real, LN = COL.whole, PT = COL.task;   // rise, run, line, point

/* ---------- chapter drawing helpers ---------- */
const caption = (p, str, t0, y = 140, fill = COL.dim, size = 30, x = 800) => show(T(p, str, { x, y, size, fill, weight: 600, anchor: 'middle', o: 0 }), t0);
const mline = (p, parts, x, y, t0, fill = COL.chalk, size = 48, anchor = 'middle') => { const e = M(p, [].concat(parts), { x, y, size, fill, anchor, o: 0 }); show(e, t0); return e; };
const side = (p, parts, y, t0, fill = COL.chalk, size = 40) => mline(p, parts, 1060, y, t0, fill, size, 'start');
// Arrow on the plane from (x1, y1) to (x2, y2) with a label.
function arrowP(P, into, x1, y1, x2, y2, color, t0, label, run = tw) {
  const a = [P.PX(x1), P.PY(y1)], b = [P.PX(x2), P.PY(y2)], L = Math.hypot(b[0] - a[0], b[1] - a[1]), ux = (b[0] - a[0]) / L, uy = (b[1] - a[1]) / L;
  const line = path(into, `M${a[0]} ${a[1]}L${b[0]} ${b[1]}M${b[0] - 12 * ux - 8 * uy} ${b[1] - 12 * uy + 8 * ux}L${b[0]} ${b[1]}L${b[0] - 12 * ux + 8 * uy} ${b[1] - 12 * uy - 8 * ux}`, { stroke: color, 'stroke-width': 4 }, { d: 0 });
  run(line, { d: 1 }, t0, .5);
  if (label !== undefined) {
    const vertical = Math.abs(uy) > Math.abs(ux);
    const t = M(into, [label], { x: (a[0] + b[0]) / 2 + (vertical ? -24 : 18), y: (a[1] + b[1]) / 2 + (vertical ? 9 : -12), size: 26, fill: color, o: 0 });
    run(t, { o: 1 }, t0 + .3, .3);
  }
}
// Rise first, then run, from (x, y).
function stair(P, into, x, y, rise, runX, t0, run = tw) {
  arrowP(P, into, x, y, x, y + rise, RI, t0, String(rise), run);
  arrowP(P, into, x, y + rise, x + runX, y + rise, RU, t0 + .6, String(runX), run);
}
function makePlane(t0) { S.pg = G(scene); S.P = plane(S.pg, { cx: 520, cy: 450, u: 40, x0: -8, x1: 8, y0: -8, y1: 8, t0 }); return S.P; }

/* ---------- beats ---------- */
const BEATS = [
  ['intro', 'Slope', (m, D) => {
    const g = G(scene, { o: 0, y: 24 });
    T(g, 'Unit 6, Chapter 33', { x: 800, y: 330, size: 28, fill: COL.dim, anchor: 'middle' });
    T(g, 'Slope', { x: 800, y: 430, size: 100, weight: 600, anchor: 'middle' });
    tw(g, { o: 1, y: 0 }, .2, 1.1, out);
    const r = mline(scene, ['slope = ', F('rise', 'run')], 800, 600, m('q'), COL.dim, 52);
    tw(g, { o: 0, y: -24 }, D - .9, .8); hide(r, D - .9, .8);
  }],
  ['define', 'Rise over run', (m) => {
    const P = makePlane(.2), p = panel(0); P.layer = p;
    P.line(-3, 1, 3, 4, LN, .5);
    P.dot(-3, 1, PT, .8, { label: 'A', dx: -16, dy: -12, anchor: 'end' }); P.dot(3, 4, PT, .8, { label: 'B' });
    arrowP(P, p, -3, 1, 3, 1, RU, m('run'), '6');
    arrowP(P, p, 3, 1, 3, 4, RI, m('rise'), '3');
    side(p, ['run = 6'], 240, m('run', .5), RU); side(p, ['rise = 3'], 310, m('rise', .5), RI);
    side(p, ['slope = ', F('rise', 'run'), ' = ', F(3, 6)], 430, m('ratio'), COL.chalk, 44);
    side(p, ['= ', F(1, 2)], 530, m('simp'), COL.good, 50);
    show(T(p, 'run 2, rise 1', { x: 1060, y: 610, size: 28, fill: COL.dim, o: 0 }), m('mean'));
  }],
  ['stairs', 'A staircase', (m) => {
    const p = panel(0), P = S.P; P.layer = p;
    side(p, ['slope ', F(2, 3)], 200, .2, COL.chalk, 50);
    P.dot(-5, -5, PT, m('start'));
    [['s1', -5, -5], ['s2', -2, -3], ['s3', 1, -1]].forEach(([mark, x, y]) => { stair(P, p, x, y, 2, 3, m(mark)); P.dot(x + 3, y + 2, PT, m(mark, 1.2)); });
    P.line(-5, -5, 4, 1, LN, m('line'));
    side(p, [F(2, 3), ' = ', F('−2', '−3')], 400, m('neg'), COL.dim, 44);
    show(T(p, 'down 2, left 3: same line', { x: 1060, y: 470, size: 26, fill: COL.dim, o: 0 }), m('neg', 3));
  }],
  ['q1', 'Quick check', () => {
    const p = panel(0), P = S.P; P.layer = p;
    P.line(-4, 3, 3, -3, LN, .2); P.dot(-4, 3, PT, .3); P.dot(3, -3, PT, .3);
  }, { ask: done => quiz({ x: 1000, y: 420, w: 580, cls: 'side' }, [{
    id: 'c-read', prompt: ['What is the slope of the line through the two points?'],
    build: blanks([{ parts: ['slope = ', { box: '-6/7' }], why: 'From (−4, 3) to (3, −3): rise −6, run 7.', hint: 'Start at the left point. Count the rise (down is negative), then the run.',
      fx: () => { const L = S.q || qlayer(); arrowP(S.P, L, -4, 3, -4, -3, RI, 0, '−6', fx); arrowP(S.P, L, -4, -3, 3, -3, RU, .6, '7', fx); } }]),
  }], done) }],
  ['types', 'Four types of slope', (m) => {
    hide(S.pg, 0, .4);
    const p = panel(0);
    [['positive', 'rises', [-1, -1, 1, 1], 'pos', COL.good], ['negative', 'falls', [-1, 1, 1, -1], 'neg', COL.nat], ['zero', 'horizontal', [-1, 0, 1, 0], 'zero', COL.whole], ['undefined', 'vertical', [0, -1, 0, 1], 'undef', COL.real]].forEach(([n, d, [a, b, c, e], mark, col], i) => {
      const cx = 260 + i * 360, cy = 380, u = 90;
      const g = G(p, { o: 0 });
      mk('rect', { x: cx - 130, y: cy - 130, width: 260, height: 260, rx: 18, fill: 'none', stroke: COL.faint, 'stroke-width': 2 }, g);
      path(g, `M${cx - 120} ${cy}H${cx + 120}M${cx} ${cy - 120}V${cy + 120}`, { stroke: COL.faint, 'stroke-width': 1.5 });
      show(g, m(mark));
      draw(path(p, `M${cx + a * u} ${cy - b * u}L${cx + c * u} ${cy - e * u}`, { stroke: col, 'stroke-width': 6 }, { d: 0 }), m(mark, .2), .6);
      show(T(p, n, { x: cx, y: cy + 180, size: 32, fill: col, weight: 600, anchor: 'middle', o: 0 }), m(mark, .4));
      show(T(p, d, { x: cx, y: cy + 220, size: 24, fill: COL.dim, anchor: 'middle', o: 0 }), m(mark, .6));
    });
    show(M(p, [F(0, 'run'), ' = 0'], { x: 980, y: 680, size: 34, fill: COL.whole, o: 0 }), m('zero', 2));
    show(M(p, [F('rise', 0), ': no meaning'], { x: 1340, y: 680, size: 34, fill: COL.real, o: 0 }), m('undef', 3));
  }],
  ['q2', 'Quick check', () => {
    show(S.pg, 0, .3);
    const p = panel(0), P = S.P; P.layer = p;
    P.line(3, -5, 3, 5, LN, .2);
  }, { ask: done => quiz({ x: 1000, y: 420, w: 580, cls: 'side' }, [{
    id: 'c-type', prompt: ['What type of slope does this line have?'],
    build: choice([['Positive'], ['Negative'], ['Zero'], ['Undefined']], 3, 'It is vertical: the run is 0, and you cannot divide by 0.',
      ['A positive slope rises from left to right.', 'A negative slope falls from left to right.', 'Zero slope is a horizontal line.', null]),
  }], done) }],
  ['formula', 'The slope formula', (m) => {
    const p = panel(0), P = S.P; P.layer = p;
    side(p, ['(x₁, y₁),  (x₂, y₂)'], 150, m('sub'), COL.dim, 36);
    side(p, ['m = ', F('y₂ − y₁', 'x₂ − x₁')], 250, m('f'), COL.chalk, 48);
    P.dot(2, 5, PT, m('ex'), { label: '(2, 5)' }); P.dot(3, 9, PT, m('ex'), { label: '(3, 9)' });
    P.line(2, 5, 3, 9, LN, m('ex', .5));
    side(p, ['m = ', F('9 − 5', '3 − 2'), ' = ', F(4, 1), ' = 4'], 380, m('calc'), COL.good, 42);
    P.dot(-5, 3, COL.nat, m('ex2'), { label: '(−5, 3)', dy: -18 }); P.dot(4, -2, COL.nat, m('ex2'), { label: '(4, −2)', dy: 30 });
    P.line(-5, 3, 4, -2, COL.nat, m('ex2', .5));
    side(p, ['m = ', F('−2 − 3', '4 − (−5)'), ' = −', F(5, 9)], 510, m('calc2'), COL.nat, 42);
    show(T(p, 'negative: the line falls ✓', { x: 1060, y: 600, size: 26, fill: COL.dim, o: 0 }), m('check'));
  }],
  ['q3', 'Quick check', () => { const p = panel(0), P = S.P; P.layer = p; P.dot(3, 1, PT, .2, { label: '(3, 1)' }); P.dot(5, 7, PT, .2, { label: '(5, 7)' }); }, { ask: done => quiz({ x: 1000, y: 420, w: 580, cls: 'side' }, [{
    id: 'c-formula', prompt: ['Find the slope of the line through (3, 1) and (5, 7).'],
    build: blanks([{ parts: ['m = ', F('7 − 1', '5 − 3'), ' = ', { box: '3' }], why: '6 ÷ 2 = 3.', hint: 'Subtract the y values on top and the x values on the bottom, in the same order.',
      fx: () => { const L = S.q || qlayer(); S.P.line(3, 1, 5, 7, COL.good, 0, { run: fx, into: L }); arrowP(S.P, L, 3, 1, 3, 7, RI, .6, '6', fx); arrowP(S.P, L, 3, 7, 5, 7, RU, 1.2, '2', fx); } }]),
  }], done) }],
  ['wrap', 'Summary', (m) => {
    hide(S.pg, 0, .4);
    const p = panel(0);
    [['slope = rise/run = change in y / change in x', 'w1'], ['Rises: +.  Falls: −.  Horizontal: 0.  Vertical: undefined.', 'w2'], ['Subtract in the same order on top and bottom.', 'w3']].forEach(([str, mark], i) => {
      const g = G(p, { x: -16, y: 260 + i * 140, o: 0 });
      mk('circle', { cx: 200, cy: -12, r: 26, fill: COL.task }, g);
      T(g, String(i + 1), { x: 200, y: -1, size: 30, fill: COL.board, weight: 700, anchor: 'middle' });
      T(g, str, { x: 260, size: 34, weight: 600 });
      tw(g, { o: 1, x: 0 }, m(mark), .6);
    });
  }],
  ['final1', 'Chapter practice', () => { panel(0); }, { ask: done => quiz(SCREEN, [{
    id: 'p-slope', prompt: ['Find the slope through each pair of points. Fractions are fine.'], build: blanks(SLOPEQ),
  }], done, 'Chapter practice   1 of 3') }],
  ['final2', 'Chapter practice', () => {}, { ask: done => quiz(SCREEN, [{
    id: 'p-type', prompt: ['What type of slope?'],
    build: grid(TYPEQ, [['p', 'Positive', COL.good], ['n', 'Negative', COL.nat], ['z', 'Zero', COL.whole], ['u', 'Undefined', COL.real]], { multi: false, text: true }),
  }], done, 'Chapter practice   2 of 3') }],
  ['final3', 'Chapter practice', () => {}, { ask: done => quiz(SCREEN, [{
    id: 'p-tf', prompt: ['True or false?'], build: grid(TFQ, TF, { multi: false, text: true }),
  }], done, 'Chapter practice   3 of 3') }],
  ['finish', 'Finished', () => {}, { ask: finishCard }],
].map(([id, title, run, extra]) => ({ id, title, run, ...extra }));

/* ---------- chapter practice data ---------- */
const SLOPEQ = [
  { parts: ['(−4, 3) and (3, −3):   m = ', { box: '-6/7' }], why: '(−3 − 3)/(3 − (−4)) = −6/7.' },
  { parts: ['(−5, −4) and (−3, 2):   m = ', { box: '3' }], why: '6/2 = 3.' },
  { parts: ['(−4, −3) and (8, 6):   m = ', { box: '3/4' }], why: '9/12 = 3/4.' },
  { parts: ['(−4, 9) and (7, 2):   m = ', { box: '-7/11' }], why: '(2 − 9)/(7 − (−4)) = −7/11.' },
  { parts: ['(−10, −3) and (−4, −18):   m = ', { box: '-5/2' }], why: '−15/6 = −5/2.' },
];
const TYPEQ = [
  { parts: ['through (0, 1) and (2, −1)'], ans: ['n'], why: 'y goes down as x goes up.' },
  { parts: ['through (3, −2) and (3, 4)'], ans: ['u'], why: 'Same x: a vertical line.' },
  { parts: ['through (−1, 0) and (2, 3)'], ans: ['p'], why: 'y goes up as x goes up.' },
  { parts: ['through (−5, 2) and (4, 2)'], ans: ['z'], why: 'Same y: a horizontal line.' },
];
const TFQ = [
  { parts: ['A slope of ', $m(F(2, 3)), ' means: rise 2, run 3.'], ans: ['t'], why: 'slope = rise/run.' },
  { parts: [$m(F(2, 3)), ' and ', $m(F('−2', '−3')), ' are the same slope.'], ans: ['t'], why: 'They are equal fractions.' },
  { parts: ['A horizontal line has an undefined slope.'], ans: ['f'], why: 'Its rise is 0, so its slope is 0.' },
  { parts: ['A line that falls from left to right has a negative slope.'], ans: ['t'], why: 'The rise is negative for a positive run.' },
  { parts: ['In the slope formula, you can subtract the x values in the opposite order from the y values.'], ans: ['f'], why: 'Use the same order on top and bottom.' },
];
boot();
</script>
</body>
</html>