Branchwell

Watch Encyclopedia / website · watchencyclopedia.org / exp / unit-circle.js

Raw 3.7 KB on 8b38e375b3e16f98454b93adca8e1fbd4fc534c5
1WE.mount('unit-circle', function (S) {
2 var C = S.C, ctx = S.ctx, a = .7, grab = false, G = {};
3 var spin = S.toggle('Spin', true);
4 S.pointer({ down: function (x, y) { grab = true; spin.set(false); set(x, y); }, move: function (x, y, d) { if (d && grab) set(x, y); }, up: function () { grab = false; } });
5 function set(x, y) { if (x > G.gx - 10 && G.gw) { a = Math.max(0, Math.min(1, (x - G.gx) / G.gw)) * S.TAU; return; } a = Math.atan2(G.cy - y, x - G.cx); if (a < 0) a += S.TAU; }
6 function fr(v) { var t = [[0, '0'], [1 / 6, 'π/6'], [1 / 4, 'π/4'], [1 / 3, 'π/3'], [1 / 2, 'π/2'], [2 / 3, '2π/3'], [3 / 4, '3π/4'], [5 / 6, '5π/6'], [1, 'π'], [7 / 6, '7π/6'], [5 / 4, '5π/4'], [4 / 3, '4π/3'], [3 / 2, '3π/2'], [5 / 3, '5π/3'], [7 / 4, '7π/4'], [11 / 6, '11π/6']]; for (var i = 0; i < t.length; i++) if (Math.abs(v / Math.PI - t[i][0]) < .012) return t[i][1]; return (v / Math.PI).toFixed(2) + 'π'; }
7 S.loop(function (dt) {
8 if (spin.v) a = (a + dt * .5) % S.TAU;
9 var w = S.W, h = S.H, sm = S.small(), R = sm ? Math.min(w * .3, h * .2) : Math.min(w * .19, h * .36), cx = sm ? w * .36 : w * .25, cy = sm ? R + 34 : h * .5; G.cx = cx; G.cy = cy;
10 var px = cx + R * Math.cos(a), py = cy - R * Math.sin(a), s = Math.sin(a), c = Math.cos(a);
11 S.clear(); S.ln(cx - R * 1.25, cy, cx + R * 1.25, cy, C.grid, 1); S.ln(cx, cy - R * 1.25, cx, cy + R * 1.25, C.grid, 1); S.ring(cx, cy, R, C.dim, 1.5);
12 ctx.beginPath(); ctx.moveTo(cx, cy); ctx.arc(cx, cy, R * .22, 0, -a, true); ctx.closePath(); ctx.fillStyle = 'rgba(213,163,90,.25)'; ctx.fill();
13 S.ln(cx, cy, px, py, C.ink, 2); S.ln(cx, cy, px, cy, C.blue, 4); S.ln(px, cy, px, py, C.red, 4); S.dot(px, py, 9, C.acc); S.ring(px, py, 14, 'rgba(213,163,90,.4)', 1.5);
14 S.txt('cos', (cx + px) / 2, cy + (s >= 0 ? 14 : -14), C.blue, 12.5, 'center', 700); S.txt('sin', px + (c >= 0 ? 10 : -10), (cy + py) / 2, C.red, 12.5, c >= 0 ? 'left' : 'right', 700);
15 S.txt('1', cx + R + 8, cy + 12, C.dim, 11); S.txt('1', cx + 8, cy - R - 8, C.dim, 11);
16 // readout
17 var tx = sm ? w * .7 : cx - R, ty = sm ? cy - R + 6 : 26;
18 if (sm) { S.txt(Math.round(a * 180 / Math.PI) + '°', tx, ty, C.ink, 22, 'left', 700); S.txt(fr(a) + ' rad', tx, ty + 24, C.mut, 12); S.txt('sin ' + s.toFixed(2), tx, ty + 50, C.red, 14, 'left', 700); S.txt('cos ' + c.toFixed(2), tx, ty + 70, C.blue, 14, 'left', 700); }
19 else { S.txt(Math.round(a * 180 / Math.PI) + '°', tx, ty, C.ink, 26, 'left', 700); S.txt('= ' + fr(a) + ' radians', tx + 78, ty + 2, C.mut, 14); }
20 // graph
21 var gx = sm ? 16 : w * .5, gw = sm ? w - 32 : w * .45, gy = sm ? cy + R + (h - cy - R) * .5 : h * .5, gh = sm ? (h - cy - R) * .32 : h * .3; G.gx = gx; G.gw = gw;
22 S.ln(gx, gy, gx + gw, gy, C.grid, 1); S.ln(gx, gy - gh, gx, gy + gh, C.grid, 1);
23 [0, 90, 180, 270, 360].forEach(function (d) { var x = gx + gw * d / 360; S.ln(x, gy - 4, x, gy + 4, C.dim, 1); S.txt(d + '°', x, gy + gh + 14, C.dim, 11, 'center'); });
24 [[Math.sin, C.red], [Math.cos, C.blue]].forEach(function (f) { ctx.beginPath(); for (var i = 0; i <= 120; i++) ctx.lineTo(gx + gw * i / 120, gy - f[0](i / 120 * S.TAU) * gh); ctx.strokeStyle = f[1]; ctx.lineWidth = 2; ctx.stroke(); });
25 var mx = gx + gw * a / S.TAU; S.ln(mx, gy - gh, mx, gy + gh, 'rgba(255,255,255,.25)', 1); S.dot(mx, gy - s * gh, 6, C.red); S.dot(mx, gy - c * gh, 6, C.blue);
26 if (!sm) { S.txt('sin = ' + s.toFixed(3), gx, gy - gh - 30, C.red, 17, 'left', 700); S.txt('cos = ' + c.toFixed(3), gx + 170, gy - gh - 30, C.blue, 17, 'left', 700); }
27 S.say('Drag the gold point around the circle. <b style="color:#e8735c">Sine</b> is how high the point is and <b style="color:#6fb1e6">cosine</b> is how far across. Unroll one trip around the circle and you get the two waves on the graph.');
28 });
29});