-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock-arithmetic.html
More file actions
executable file
·507 lines (476 loc) · 19.7 KB
/
Copy pathclock-arithmetic.html
File metadata and controls
executable file
·507 lines (476 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<!DOCTYPE html>
<html lang="en" data-theme="light">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Clock Arithmetic Explorer</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Instrument+Serif:ital@0;1&family=DM+Sans:opsz,wght@9..40,300..700&display=swap" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.4/p5.min.js"></script>
<style>
:root, [data-theme="light"] {
--color-bg: #f7f6f2;
--color-surface: #f9f8f5;
--color-surface-2: #fbfbf9;
--color-surface-offset: #f0ede8;
--color-border: #d4d1ca;
--color-text: #28251d;
--color-text-muted: #7a7974;
--color-text-faint: #bab9b4;
--color-primary: #01696f;
--color-primary-hover: #0c4e54;
--color-primary-hl: #cedcd8;
--color-success: #437a22;
--color-error: #a12c7b;
--font-display: 'Instrument Serif', Georgia, serif;
--font-body: 'DM Sans', 'Helvetica Neue', sans-serif;
--text-xs: clamp(0.75rem, 0.7rem + 0.25vw, 0.875rem);
--text-sm: clamp(0.875rem, 0.8rem + 0.35vw, 1rem);
--text-base: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
--text-lg: clamp(1.125rem, 1rem + 0.75vw, 1.5rem);
--text-xl: clamp(1.5rem, 1.2rem + 1.25vw, 2.25rem);
--text-2xl: clamp(2rem, 1.2rem + 2.5vw, 3.5rem);
--space-1:.25rem; --space-2:.5rem; --space-3:.75rem; --space-4:1rem;
--space-5:1.25rem; --space-6:1.5rem; --space-8:2rem; --space-10:2.5rem; --space-12:3rem;
--radius-sm:.375rem; --radius-md:.5rem; --radius-lg:.75rem; --radius-xl:1rem; --radius-full:9999px;
--shadow-md: 0 4px 12px oklch(0.2 0.01 80 / 0.09);
--shadow-lg: 0 12px 32px oklch(0.2 0.01 80 / 0.13);
--t: 180ms cubic-bezier(0.16, 1, 0.3, 1);
}
[data-theme="dark"] {
--color-bg: #171614;
--color-surface: #1c1b19;
--color-surface-2: #201f1d;
--color-surface-offset: #25241f;
--color-border: #393836;
--color-text: #cdccca;
--color-text-muted: #797876;
--color-text-faint: #5a5957;
--color-primary: #4f98a3;
--color-primary-hover: #227f8b;
--color-primary-hl: #1e3538;
--color-success: #6daa45;
--color-error: #d163a7;
--shadow-md: 0 4px 12px oklch(0 0 0 / 0.35);
--shadow-lg: 0 12px 32px oklch(0 0 0 / 0.45);
}
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-font-smoothing: antialiased; }
body {
min-height: 100dvh;
font-family: var(--font-body);
font-size: var(--text-base);
color: var(--color-text);
background: var(--color-bg);
transition: background var(--t), color var(--t);
display: flex; flex-direction: column; align-items: center;
}
input, button { font: inherit; color: inherit; cursor: pointer; }
:focus-visible { outline: 2px solid var(--color-primary); outline-offset: 3px; border-radius: var(--radius-sm); }
/* ── Header ── */
header {
width: 100%;
padding: var(--space-4) var(--space-8);
display: flex; align-items: center; justify-content: space-between;
border-bottom: 1px solid var(--color-border);
background: var(--color-surface);
position: sticky; top: 0; z-index: 10;
}
.logo {
display: flex; align-items: center; gap: var(--space-3);
font-family: var(--font-display);
font-size: var(--text-lg); color: var(--color-text);
}
.theme-btn {
background: none; border: 1px solid var(--color-border);
border-radius: var(--radius-full);
padding: var(--space-2) var(--space-3);
color: var(--color-text-muted);
display: flex; align-items: center; gap: var(--space-2);
font-size: var(--text-sm);
transition: background var(--t), border-color var(--t), color var(--t);
}
.theme-btn:hover { background: var(--color-surface-offset); color: var(--color-text); }
/* ── Main ── */
main {
width: 100%; max-width: 1100px;
padding: var(--space-8) var(--space-6) var(--space-12);
display: flex; flex-direction: column; align-items: center;
gap: var(--space-8);
}
.page-title {
text-align: center; font-family: var(--font-display);
font-size: var(--text-2xl); color: var(--color-text); line-height: 1.1;
}
.page-sub {
text-align: center; color: var(--color-text-muted);
font-size: var(--text-base); max-width: 60ch; line-height: 1.65;
margin-top: var(--space-2);
}
/* ── Card ── */
.card {
background: var(--color-surface);
border: 1px solid var(--color-border);
border-radius: var(--radius-xl);
box-shadow: var(--shadow-md);
padding: var(--space-6) var(--space-8);
width: 100%; max-width: 700px;
}
.ctrl-group { display: flex; flex-direction: column; gap: var(--space-5); }
.ctrl-row { display: flex; align-items: center; gap: var(--space-5); flex-wrap: wrap; }
.ctrl-label {
font-size: var(--text-sm); color: var(--color-text-muted);
font-weight: 500; min-width: 90px;
text-transform: uppercase; letter-spacing: 0.04em;
}
.ctrl-inputs { display: flex; align-items: center; gap: var(--space-3); flex: 1; }
input[type="range"] {
-webkit-appearance: none; appearance: none;
flex: 1; height: 6px;
background: var(--color-surface-offset);
border-radius: var(--radius-full); outline: none; cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none; appearance: none;
width: 20px; height: 20px; border-radius: 50%;
background: var(--color-primary); cursor: pointer;
border: 2px solid var(--color-surface);
box-shadow: 0 1px 4px oklch(0 0 0 / 0.2);
transition: background var(--t), transform var(--t);
}
input[type="range"]::-webkit-slider-thumb:hover { background: var(--color-primary-hover); transform: scale(1.12); }
input[type="range"]::-moz-range-thumb {
width: 20px; height: 20px; border-radius: 50%;
background: var(--color-primary); cursor: pointer;
border: 2px solid var(--color-surface);
}
input[type="number"] {
width: 90px; padding: var(--space-2) var(--space-3);
background: var(--color-surface-2);
border: 1px solid var(--color-border);
border-radius: var(--radius-md);
font-size: var(--text-base); font-weight: 600;
color: var(--color-text); text-align: center;
transition: border-color var(--t), box-shadow var(--t);
-moz-appearance: textfield; appearance: textfield;
}
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
input[type="number"]:focus {
outline: none; border-color: var(--color-primary);
box-shadow: 0 0 0 3px oklch(from var(--color-primary) l c h / 0.15);
}
input[type="number"].error {
border-color: var(--color-error);
box-shadow: 0 0 0 3px oklch(from var(--color-error) l c h / 0.15);
}
.range-hint { font-size: var(--text-xs); color: var(--color-text-faint); white-space: nowrap; }
/* ── Result banner ── */
.result-banner {
background: var(--color-primary-hl);
border: 1px solid oklch(from var(--color-primary) l c h / 0.28);
border-radius: var(--radius-lg);
padding: var(--space-4) var(--space-6);
text-align: center;
}
.result-main {
font-family: var(--font-display);
font-size: var(--text-xl); color: var(--color-primary);
line-height: 1.2;
}
.result-main strong { font-size: 1.25em; }
.result-formula {
font-size: var(--text-sm); color: var(--color-text-muted);
margin-top: var(--space-1);
}
/* ── Canvas wrap ── */
.canvas-wrap {
background: var(--color-surface);
border: 1px solid var(--color-border);
border-radius: var(--radius-xl);
box-shadow: var(--shadow-lg);
padding: var(--space-4);
display: inline-flex; align-items: center; justify-content: center;
}
#p5-container canvas { display: block; border-radius: var(--radius-lg); }
/* ── Explainer ── */
.explainer {
background: var(--color-surface);
border: 1px solid var(--color-border);
border-radius: var(--radius-xl);
padding: var(--space-6) var(--space-8);
width: 100%; max-width: 700px;
}
.explainer h2 {
font-family: var(--font-display);
font-size: var(--text-lg); color: var(--color-text);
margin-bottom: var(--space-4);
}
.explainer p {
font-size: var(--text-sm); color: var(--color-text-muted);
line-height: 1.75; margin-bottom: var(--space-3);
}
.explainer p:last-child { margin-bottom: 0; }
.explainer strong { color: var(--color-text); }
.explainer code {
font-family: 'Courier New', monospace; font-size: 0.9em;
background: var(--color-surface-offset);
padding: 0.1em 0.4em; border-radius: var(--radius-sm);
color: var(--color-primary);
}
@media (max-width: 600px) {
.ctrl-row { flex-direction: column; align-items: flex-start; }
.ctrl-inputs { width: 100%; }
.card, .explainer { padding: var(--space-4) var(--space-5); }
}
</style>
</head>
<body>
<header>
<div class="logo">
<svg width="30" height="30" viewBox="0 0 32 32" fill="none" aria-label="Clock logo">
<circle cx="16" cy="16" r="13.5" stroke="var(--color-primary)" stroke-width="2"/>
<circle cx="16" cy="16" r="2.5" fill="var(--color-primary)"/>
<line x1="16" y1="4.5" x2="16" y2="7.5" stroke="var(--color-primary)" stroke-width="2" stroke-linecap="round"/>
<line x1="27.5" y1="16" x2="24.5" y2="16" stroke="var(--color-primary)" stroke-width="2" stroke-linecap="round"/>
<line x1="16" y1="27.5" x2="16" y2="24.5" stroke="var(--color-primary)" stroke-width="2" stroke-linecap="round"/>
<line x1="4.5" y1="16" x2="7.5" y2="16" stroke="var(--color-primary)" stroke-width="2" stroke-linecap="round"/>
<line x1="16" y1="16" x2="22" y2="10" stroke="var(--color-primary)" stroke-width="2.5" stroke-linecap="round"/>
</svg>
Clock Arithmetic Explorer
</div>
<button class="theme-btn" id="themeBtn" aria-label="Switch to dark mode">
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="5"/>
<path d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42"/>
</svg>
Light
</button>
</header>
<main>
<div>
<h1 class="page-title">A mod N</h1>
<p class="page-sub">
Set the modulus <em>N</em> and a value <em>A</em>.
The clock hand points to the remainder — that is, <em>A</em> mod <em>N</em>.
</p>
</div>
<div class="card">
<div class="ctrl-group">
<div class="ctrl-row">
<span class="ctrl-label">Modulus N</span>
<div class="ctrl-inputs">
<input type="range" id="modSlider" min="4" max="31" value="12" step="1" aria-label="Modulus N">
<input type="number" id="modNum" min="4" max="31" value="12" aria-label="Modulus N value">
</div>
<span class="range-hint">4 – 31</span>
</div>
<div class="ctrl-row">
<span class="ctrl-label">Value A</span>
<div class="ctrl-inputs">
<input type="range" id="valSlider" min="-999" max="999" value="0" step="1" aria-label="Value A">
<input type="number" id="valNum" min="-999" max="999" value="0" aria-label="Value A">
</div>
<span class="range-hint">−999 – 999</span>
</div>
<div class="result-banner">
<div class="result-main" id="resMath">0 mod 12 = <strong>0</strong></div>
<div class="result-formula" id="resFormula">0 = 0 × 12 + 0 → remainder is 0</div>
</div>
</div>
</div>
<div class="canvas-wrap">
<div id="p5-container"></div>
</div>
<div class="explainer">
<h2>How to read this clock</h2>
<p>
A regular clock has 12 positions (0–11 in our convention) and
<strong>wraps around</strong> after each full revolution. A modular-arithmetic clock
works the same way with <em>N</em> positions labeled <strong>0 through N−1</strong>.
The hand always points to the <em>remainder</em> when <em>A</em> is divided by <em>N</em>.
</p>
<p>
<strong>Positive values:</strong> counting forward from 0 wraps around as many times as needed.
With <code>N = 12</code>, the value <code>A = 14</code> goes once around (12 steps)
and lands 2 past zero, so <code>14 mod 12 = 2</code>.
</p>
<p>
<strong>Negative values:</strong> we count <em>backwards</em>, but the result is
always kept in the range 0 to N−1. So <code>−1 mod 12 = 11</code> —
one step back from 0 wraps to position 11. The formula used here is
<code>((A % N) + N) % N</code>, which ensures a non-negative remainder.
</p>
<p>
The <strong style="color:var(--color-success)">green-outlined circle</strong> marks position 0 (the start),
and the <strong style="color:var(--color-primary)">filled teal circle</strong> is the
current remainder — where the hand points.
</p>
</div>
</main>
<script>
/* ── Theme toggle ── */
(function(){
const btn = document.getElementById('themeBtn');
const root = document.documentElement;
let theme = matchMedia('(prefers-color-scheme:dark)').matches ? 'dark' : 'light';
root.setAttribute('data-theme', theme);
syncBtn();
btn.addEventListener('click', () => {
theme = theme === 'dark' ? 'light' : 'dark';
root.setAttribute('data-theme', theme);
syncBtn();
if (window._p5inst) window._p5inst.redraw();
});
function syncBtn(){
btn.setAttribute('aria-label','Switch to '+(theme==='dark'?'light':'dark')+' mode');
btn.innerHTML = theme==='dark'
? '<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="5"/><path d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42"/></svg> Light'
: '<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg> Dark';
}
})();
/* ── Shared state ── */
let N = 12, A = 0;
function trueMod(a, n){ return ((a % n) + n) % n; }
function updateBanner(){
const r = trueMod(A, N);
const q = (A - r) / N;
document.getElementById('resMath').innerHTML =
A + ' mod ' + N + ' = <strong>' + r + '</strong>';
document.getElementById('resFormula').textContent =
A + ' = ' + q + ' \u00d7 ' + N + ' + ' + r + ' \u2192 remainder is ' + r;
}
/* ── Controls ── */
const modSlider = document.getElementById('modSlider');
const modNum = document.getElementById('modNum');
const valSlider = document.getElementById('valSlider');
const valNum = document.getElementById('valNum');
function applyN(v){
v = Math.max(4,Math.min(31,Math.round(+v||4)));
N=v; modSlider.value=v; modNum.value=v;
modNum.classList.remove('error');
updateBanner();
if(window._p5inst) window._p5inst.redraw();
}
function applyA(v){
v = Math.max(-999,Math.min(999,Math.round(+v||0)));
A=v; valSlider.value=v; valNum.value=v;
valNum.classList.remove('error');
updateBanner();
if(window._p5inst) window._p5inst.redraw();
}
modSlider.addEventListener('input',()=>applyN(modSlider.value));
valSlider.addEventListener('input',()=>applyA(valSlider.value));
modNum.addEventListener('input',()=>{
const v=+modNum.value;
if(!isNaN(v)&&Number.isInteger(v)){
if(v<4||v>31) modNum.classList.add('error');
else applyN(v);
}
});
modNum.addEventListener('blur',()=>applyN(modNum.value));
valNum.addEventListener('input',()=>{
const v=+valNum.value;
if(!isNaN(v)&&Number.isInteger(v)){
if(v<-999||v>999) valNum.classList.add('error');
else applyA(v);
}
});
valNum.addEventListener('blur',()=>applyA(valNum.value));
updateBanner();
/* ── p5.js sketch ── */
new p5(function(p){
window._p5inst = p;
const SZ = 560;
p.setup = function(){
const cvs = p.createCanvas(SZ,SZ);
cvs.parent('p5-container');
p.noLoop();
p.textFont('DM Sans');
};
p.draw = function(){
const dark = document.documentElement.getAttribute('data-theme')==='dark';
const C = {
bg: dark?'#1c1b19':'#f9f8f5',
ring: dark?'#393836':'#d4d1ca',
dotBg: dark?'#25241f':'#f0ede8',
dotStroke: dark?'#4f98a3':'#01696f',
dotActive: dark?'#4f98a3':'#01696f',
dotTxt: dark?'#cdccca':'#28251d',
hand: dark?'#4f98a3':'#01696f',
sweep: dark?'rgba(79,152,163,0.13)':'rgba(1,105,111,0.09)',
zero: dark?'#6daa45':'#437a22',
sub: dark?'#797876':'#7a7974',
};
p.background(C.bg);
const cx=SZ/2, cy=SZ/2;
/* Radius scales down as N grows so label circles don't overlap */
const R = p.map(N,4,31,222,172);
const dotR = p.map(N,4,31,24,14);
const result = trueMod(A,N);
function ang(k){ return -p.HALF_PI + (p.TWO_PI*k/N); }
const ra = ang(result);
/* ── Sweep arc from 12 o'clock to result ── */
if(result>0){
p.noStroke(); p.fill(C.sweep);
p.arc(cx,cy,R*1.55,R*1.55,-p.HALF_PI,ra);
}
/* ── Outer ring ── */
p.noFill(); p.stroke(C.ring); p.strokeWeight(2);
p.ellipse(cx,cy,R*2,R*2);
/* ── Hand (shadow then main) ── */
const hx=cx+p.cos(ra)*R*0.88, hy=cy+p.sin(ra)*R*0.88;
p.stroke(dark?'rgba(0,0,0,0.4)':'rgba(0,0,0,0.10)');
p.strokeWeight(6); p.strokeCap(p.ROUND);
p.line(cx+1,cy+2,hx+1,hy+2);
p.stroke(C.hand); p.strokeWeight(4); p.strokeCap(p.ROUND);
p.line(cx,cy,hx,hy);
/* Arrowhead */
const aL=11, perp=ra+p.HALF_PI;
p.fill(C.hand); p.noStroke();
p.triangle(
hx, hy,
hx-p.cos(ra)*aL+p.cos(perp)*(aL*0.45), hy-p.sin(ra)*aL+p.sin(perp)*(aL*0.45),
hx-p.cos(ra)*aL-p.cos(perp)*(aL*0.45), hy-p.sin(ra)*aL-p.sin(perp)*(aL*0.45)
);
/* ── Label nodes ── */
for(let k=0;k<N;k++){
const a=ang(k);
const nx=cx+p.cos(a)*R, ny=cy+p.sin(a)*R;
const isRes=k===result, isZero=k===0;
if(isRes){
p.noFill(); p.stroke(C.hand); p.strokeWeight(2);
p.ellipse(nx,ny,dotR*2+10,dotR*2+10);
p.fill(C.dotActive); p.stroke(C.dotActive); p.strokeWeight(1.5);
} else if(isZero){
p.fill(C.dotBg); p.stroke(C.zero); p.strokeWeight(2.5);
} else {
p.fill(C.dotBg); p.stroke(C.dotStroke); p.strokeWeight(1.2);
}
p.ellipse(nx,ny,dotR*2,dotR*2);
const fs=p.map(N,4,31,16,10);
p.textSize(fs); p.textAlign(p.CENTER,p.CENTER);
p.textStyle(isRes||isZero ? p.BOLD : p.NORMAL);
p.noStroke();
if(isRes) p.fill(255);
else if(isZero) p.fill(C.zero);
else p.fill(C.dotTxt);
p.text(k,nx,ny);
}
/* ── Center pivot ── */
p.fill(C.hand); p.noStroke(); p.ellipse(cx,cy,10,10);
p.fill(C.bg); p.ellipse(cx,cy,4,4);
/* ── Bottom legend ── */
p.textStyle(p.NORMAL); p.textAlign(p.CENTER,p.CENTER);
p.fill(C.sub); p.textSize(11);
p.text('N = '+N+' \u00b7 A = '+A+' \u00b7 A mod N = '+result, cx, SZ-36);
p.fill(C.zero); p.textSize(11);
p.text('\u25cf 0 (start)', cx-85, SZ-17);
p.fill(C.hand);
p.text('\u25cf '+result+' (result = A mod N)', cx+85, SZ-17);
};
});
</script>
</body>
</html>