WE.mount('pythagoras', function (S) { var C = S.C, ctx = S.ctx, mix = 0; var view = S.select('Show', [['sq', 'Squares on the sides'], ['proof', 'Proof by sliding']], 'sq'); var a = S.slider('Side a', 1, 8, 3, .5), b = S.slider('Side b', 1, 8, 4, .5), sl = S.toggle('Slide the triangles', false); function poly(p, fill, stroke) { ctx.beginPath(); p.forEach(function (q) { ctx.lineTo(q[0], q[1]); }); ctx.closePath(); if (fill) { ctx.fillStyle = fill; ctx.fill(); } if (stroke) { ctx.strokeStyle = stroke; ctx.lineWidth = 2; ctx.stroke(); } } function f(v) { return (Math.round(v * 100) / 100).toString(); } S.loop(function (dt) { var w = S.W, h = S.H, sm = S.small(), A = a.v, B = b.v, c = Math.sqrt(A * A + B * B); S.clear(); sl.el.hidden = view.v !== 'proof'; mix += ((sl.v ? 1 : 0) - mix) * Math.min(1, dt * 3.5); if (view.v === 'sq') { var ext = [A + B + B, A + B + A], u = Math.min((w * (sm ? .92 : .6)) / (A + B + B + .5), (h * .92) / (A + B + A + .5)), ox = (sm ? w * .5 : w * .36) - (A + 2 * B) * u / 2 + B * u, oy = h * .5 + (2 * A + B) * u / 2 - A * u; // triangle: right angle at (ox,oy); a goes right, b goes up var P = [ox, oy], Q = [ox + A * u, oy], R = [ox, oy - B * u]; poly([P, Q, [Q[0], Q[1] + A * u], [P[0], P[1] + A * u]], 'rgba(111,177,230,.3)', C.blue); poly([P, R, [R[0] - B * u, R[1]], [P[0] - B * u, P[1]]], 'rgba(126,203,147,.3)', C.green); var nx = B * u, ny = A * u; poly([Q, R, [R[0] + nx, R[1] - ny], [Q[0] + nx, Q[1] - ny]], 'rgba(213,163,90,.3)', C.acc); poly([P, Q, R], '#2a303b', C.ink); S.txt('a² = ' + f(A * A), ox + A * u / 2, oy + A * u / 2, C.blue, sm ? 13 : 16, 'center', 700); S.txt('b² = ' + f(B * B), ox - B * u / 2, oy - B * u / 2, C.green, sm ? 13 : 16, 'center', 700); S.txt('c² = ' + f(A * A + B * B), (Q[0] + R[0]) / 2 + nx / 2, (Q[1] + R[1]) / 2 - ny / 2, C.acc, sm ? 13 : 16, 'center', 700); if (!sm) { var tx = w * .68; S.txt(f(A * A) + ' + ' + f(B * B) + ' = ' + f(A * A + B * B), tx, h * .3, C.ink, 26, 'left', 700); S.txt('a² + b² = c²', tx, h * .3 + 36, C.mut, 15); S.txt('Longest side c = ' + c.toFixed(3), tx, h * .3 + 76, C.acc, 16, 'left', 700); } S.say('The two smaller squares together cover exactly the same area as the big one: ' + f(A * A) + ' + ' + f(B * B) + ' = ' + f(A * A + B * B) + '. So the longest side is ' + c.toFixed(3) + '. This works for every right triangle, and only for right triangles.'); } else { var s = A + B, U = Math.min(w * (sm ? .9 : .5), h * .9) / s, x0 = (sm ? w * .5 : w * .36) - s * U / 2, y0 = h * .5 - s * U / 2, e = mix * mix * (3 - 2 * mix); var T1 = [[[0, 0], [A, 0], [0, B]], [[s, 0], [s, A], [A, 0]], [[s, s], [B, s], [s, A]], [[0, s], [0, B], [B, s]]], T2 = [[[A, A], [A, 0], [s, A]], [[s, 0], [s, A], [A, 0]], [[A, A], [0, A], [A, s]], [[0, s], [A, s], [0, A]]]; function M(q) { return [x0 + q[0] * U, y0 + q[1] * U]; } poly([[0, 0], [s, 0], [s, s], [0, s]].map(M), null, C.dim); if (e < .5) poly([[A, 0], [s, A], [B, s], [0, B]].map(M), 'rgba(213,163,90,' + (.32 * (1 - e * 2)) + ')'); else { var al = .32 * (e * 2 - 1); poly([[0, 0], [A, 0], [A, A], [0, A]].map(M), 'rgba(111,177,230,' + al + ')'); poly([[A, A], [s, A], [s, s], [A, s]].map(M), 'rgba(126,203,147,' + al + ')'); } for (var i = 0; i < 4; i++) poly(T1[i].map(function (q, j) { var r = T2[i][j]; return M([q[0] + (r[0] - q[0]) * e, q[1] + (r[1] - q[1]) * e]); }), '#2a303b', C.ink); if (e < .3) S.txt('c² = ' + f(A * A + B * B), x0 + s * U / 2, y0 + s * U / 2, C.acc, sm ? 14 : 18, 'center', 700); if (e > .7) { S.txt('a² = ' + f(A * A), x0 + A * U / 2, y0 + A * U / 2, C.blue, sm ? 13 : 16, 'center', 700); S.txt('b² = ' + f(B * B), x0 + (A + B / 2) * U, y0 + (A + B / 2) * U, C.green, sm ? 13 : 16, 'center', 700); } if (!sm) { S.txt('Same box. Same four triangles.', w * .66, h * .4, C.ink, 18, 'left', 700); S.txt('So the space left over must be the same too.', w * .66, h * .4 + 28, C.mut, 14); } S.say(sl.v ? 'The four triangles have only moved. They now leave two holes: a square on side a and a square on side b. The leftover space cannot have changed, so a² + b² = c².' : 'Four copies of the triangle sit in a box, leaving one tilted square whose side is the longest side, c. Now press Slide the triangles.'); } }); });