-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
733 lines (678 loc) · 22.6 KB
/
Copy pathindex.html
File metadata and controls
733 lines (678 loc) · 22.6 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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Function Plotter with JS code Multi Graphics</title>
<meta name="keywords" content="plotter,function,multi,free,plotter,graphics,code">
<meta name="description" content="Function Plotter - with write JS code free, multi display graphics pix, gamma, special function!">
<style>
* { margin:0; padding:0; box-sizing:border-box; }
body { font-family:'Consolas',monospace; display:flex; height:100vh; background:#1e1e1e; color:#d4d4d4; }
canvas { width:100%; height:100%; cursor:crosshair; display:block; }
#editor { width:35%; display:flex; flex-direction:column; border-right:2px solid #333; }
#graph { width:65%; position:relative; background:#fff; }
#editor-header { background:#252526; padding:10px 15px; display:flex; justify-content:space-between; align-items:center; border-bottom:1px solid #333; }
#editor-header h2 { font-size:14px; color:#4ec9b0; }
#run-btn { background:#0e639c; color:white; border:none; padding:6px 20px; cursor:pointer; font-family:inherit; font-size:13px; border-radius:3px; }
#run-btn:hover { background:#1177bb; }
#code { flex:1; background:#1e1e1e; color:#d4d4d4; border:none; padding:15px; font-family:'Consolas',monospace; font-size:14px; resize:none; outline:none; line-height:1.6; }
#controls { position:absolute; top:10px; right:10px; background:rgba(255,255,255,0.95); padding:8px; border-radius:5px; }
#controls button { background:#f0f0f0; border:1px solid #ccc; padding:5px 10px; margin:2px; cursor:pointer; border-radius:3px; font-family:inherit; }
#info { position:absolute; bottom:10px; left:10px; background:rgba(255,255,255,0.95); padding:8px 12px; border-radius:5px; font-size:12px; color:#333; }
#msg { position:absolute; bottom:10px; right:10px; padding:8px 12px; border-radius:5px; font-size:12px; display:none; }
#msg.err { background:rgba(255,0,0,0.9); color:white; }
#msg.ok { background:rgba(0,200,0,0.9); color:black; }
#toggle-editor { position:absolute; top:10px; left:10px; background:rgba(255,255,255,0.95); border:1px solid #ccc; padding:4px 10px; cursor:pointer; border-radius:3px; font-family:inherit; font-size:12px; z-index:10; }
#toggle-editor:hover { background:#e0e0e0; }
</style>
</head>
<body>
<div id="editor">
<div id="editor-header">
<h2>📝 Editor</h2>
<button id="run-btn" onclick="runCode()">▶ RUN</button>
</div>
<textarea id="code" spellcheck="false">
primes = primes(20000);
plot(x => pix(x, primes));
plot(x => ceil(x / ln(x)));
plot(x => floor(li(x)));
plot(x => ceil(x / ln(x) + x / pow(ln(x), 2)));
//plot(x => cgamma(complex(0, x)).y);
/*for (let i = 0; i < primes.length / 100; i++) {
const p = primes[i];
const f = (x) => x > p ? sin(x * (PI / p)) * p : 0;
plot(x => f(x), { color: randomColor(), width: 1.5 });
}*/
/*for (let i = 0; i < 10.0; i += 0.05) {
let ecolor = randomColor();
plot(x => sqrt(pow(x, 3) - 2 * x + 2.0 / i), { color: ecolor });
plot(x => -sqrt(pow(x, 3) - 2 * x + 2.0 / i), { color: ecolor });
}*/
/*for (let k = 0; k < 10.0; k += 0.1) {
plot(x => cgammaphi(-k + 1, x).x);
}*/
/*
function realMandelbrot(c, n) {
let z = 0;
for (let i = 0; i < n; i++) {
z = pow(z, 2) + c;
//z = sin(z) + c;
}
return z;
}
for (let n = 20; n <= 100; n += 1) {
plot(x => -realMandelbrot(x, n));
}
*/
</textarea>
</div>
<div id="graph">
<canvas id="c"></canvas>
<button id="toggle-editor" onclick="toggleEditor()">◀</button>
<div id="controls">
<button onclick="resetView()">⌂</button>
</div>
<div id="info">Graphics: <span id="cnt">0</span> | Scale: <span id="sc">1.0</span></div>
<div id="msg"></div>
</div>
<script>
let editorVisible = true;
function toggleEditor() {
const editor = document.getElementById('editor');
const graph = document.getElementById('graph');
const btn = document.getElementById('toggle-editor');
editorVisible = !editorVisible;
if (editorVisible) {
editor.style.display = 'flex';
editor.style.width = '35%';
graph.style.width = '65%';
btn.textContent = '◀';
} else {
editor.style.display = 'none';
graph.style.width = '100%';
btn.textContent = '▶';
}
setTimeout(resize, 200);
}
const savedCode = localStorage.getItem('plotter_code');
if (savedCode !== null && savedCode.length >= 6) { document.getElementById('code').value = savedCode; }
const canvas = document.getElementById('c');
const ctx = canvas.getContext('2d');
let scale = 50, cx = 0, cy = 0;
let graphs = [], dragging = false, lx = 0, ly = 0;
const sin=Math.sin, cos=Math.cos, tan=Math.tan, log=Math.log, log2=Math.log2, log10=Math.log10;
const sqrt=Math.sqrt, abs=Math.abs, exp=Math.exp, pow=Math.pow, PI=Math.PI, E=Math.E;
const asin=Math.asin, acos=Math.acos, atan=Math.atan, atan2=Math.atan2;
const sinh=Math.sinh, cosh=Math.cosh, tanh=Math.tanh;
const floor=Math.floor, ceil=Math.ceil, round=Math.round, sign=Math.sign;
const min=Math.min, max=Math.max, random=Math.random;
const step = (edge, x) => x >= edge ? 1.0 : 0.0;
const clamp = (v, mn, mx) => Math.max(mn, Math.min(mx, v));
const relumin = (x) => x >= 0 ? undefined : x;
const relumax = (x) => x <= 0 ? undefined : x;
function gcd(a, b) {
while (b) {
[a, b] = [b, a % b];
}
return a;
}
function primes(n) {
const seq = [];
for (let i = 2; i <= n; i++) {
let isPrime = true;
for (let j = 2; j * j <= i; j++) {
if (i % j === 0) { isPrime = false; break; }
}
if (isPrime) { seq.push(i); }
}
return seq;
}
function factorial(n) {
if (n <= 0) return [];
const seq = [1];
for (let i = 2; i <= n; i++) {
seq.push(seq[seq.length - 1] * i);
}
return seq;
}
function isprime(x) {
if (x < 2) { return false; }
if (x <= 3) { return true; }
if (x % 2 === 0 || x % 3 === 0) { return false; }
for (let i = 5; i * i <= x; i += 6) {
if (x % i === 0 || x % (i + 2) === 0) {
return false;
}
}
return true;
}
// According to this logic, almost all functions can be turned into prime versions. like fib-prime or mersenne-prime with add condition
function primorial(n) {
if (n <= 0) return [];
const seq = [1];
for (let i = 2; i <= n; i++) {
if (isprime(i)) { seq.push(seq[seq.length - 1] * i); }
}
return seq;
}
function fib(n) {
if (n <= 0) return [];
if (n === 1) return [0];
const seq = [0, 1];
for (let i = 2; i < n; i++) {
seq.push(seq[i - 1] + seq[i - 2]);
}
return seq;
}
function mersenne(n) {
const seq = [];
for (let i = 1; i <= n; i++) {
seq.push(Math.pow(2, i) - 1);
}
return seq;
}
function geometric(start, ratio, count) {
const seq = [];
for (let i = 0; i < count; i++) {
seq.push(Math.pow(ratio, i) * start);
}
return seq;
}
function arithmetic(start, diff, count) {
const seq = [];
for (let i = 0; i < count; i++) {
seq.push(start + i * diff);
}
return seq;
}
function primePolynomial(n, coff1, coff2, coff3) { /// 11, 17, 41
const seq = [];
for (let i = 0; i <= n; i++) {
seq.push(coff1 * i * i + coff2 * i + coff3);
}
return seq;
}
function catalan(n) {
if (n <= 0) return [];
const seq = [1];
for (let i = 1; i < n; i++) {
seq.push(seq[i-1] * 2 * (2 * i - 1) / (i + 1));
}
return seq;
}
function eulerTotient(n) {
const seq = [];
for (let i = 1; i <= n; i++) {
let count = 0;
for (let j = 1; j <= i; j++) {
if (gcd(i, j) === 1) count++;
}
seq.push(count);
}
return seq;
}
function binomial(n, k) {
if (k < 0 || k > n) return 0;
let result = 1;
for (let i = 1; i <= k; i++) {
result = result * (n - i + 1) / i;
}
return result;
}
function bell(n) {
if (n <= 0) return [];
const seq = [1];
for (let i = 1; i < n; i++) {
let prev = seq[i-1];
let sum = 0;
for (let j = 0; j < i; j++) {
sum += binomial(i-1, j) * seq[j];
}
seq.push(sum);
}
return seq;
}
function lucas(n) {
if (n <= 0) return [];
if (n === 1) return [2];
const seq = [2, 1];
for (let i = 2; i < n; i++) {
seq.push(seq[i-1] + seq[i-2]);
}
return seq;
}
function tribonacci(n) { //n-bonacci
if (n <= 0) return [];
if (n === 1) return [0];
if (n === 2) return [0, 1];
const seq = [0, 1, 1];
for (let i = 3; i < n; i++) {
seq.push(seq[i-1] + seq[i-2] + seq[i-3]);
}
return seq;
}
function pell(n) {
if (n <= 0) return [];
if (n === 1) return [0];
const seq = [0, 1];
for (let i = 2; i < n; i++) {
seq.push(2 * seq[i-1] + seq[i-2]);
}
return seq;
}
function golomb(n) {
if (n <= 0) return [];
const seq = [1, 2, 2];
for (let i = 3; i < n; i++) {
let count = 0;
for (let j = 0; j < seq[i-1]; j++) {
count++;
}
seq.push(count);
}
return seq;
}
function stirlingSecond(n, k) {
if (k === 0 || k > n) return 0;
if (k === 1 || k === n) return 1;
const dp = Array.from({length: n+1}, () => Array(k+1).fill(0));
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= Math.min(i, k); j++) {
if (j === 1 || j === i) {
dp[i][j] = 1;
} else {
dp[i][j] = j * dp[i-1][j] + dp[i-1][j-1];
}
}
}
return dp[n][k];
}
function collatz(n) {
if (n <= 0) return [];
const seq = [n];
while (n !== 1) {
if (n % 2 === 0) { n = n / 2; }
else { n = (3 * n + 1); } // /2
seq.push(n);
}
return seq;
}
function perfectNumbers(limit) {
const seq = [];
for (let n = 2; n <= limit; n++) {
let sum = 1;
for (let i = 2; i * i <= n; i++) {
if (n % i === 0) {
sum += i;
if (i !== n / i) sum += n / i;
}
}
if (sum === n && n > 1) seq.push(n);
}
return seq;
}
function armstrongNumbers(limit) {
const seq = [];
for (let n = 0; n <= limit; n++) {
const str = String(n);
const len = str.length;
let sum = 0;
for (const digit of str) {
sum += Math.pow(parseInt(digit), len);
}
if (sum === n) seq.push(n);
}
return seq;
}
function btn(minusKey, plusKey) {
let value = 0;
const minusBtn = document.getElementById(minusKey) || document.querySelector(`[data-key="${minusKey}"]`);
const plusBtn = document.getElementById(plusKey) || document.querySelector(`[data-key="${plusKey}"]`);
const getBtn = (key) => {
const el = document.getElementById(key);
if (el) return el;
return {
addEventListener: (event, handler) => {
document.addEventListener('keydown', (e) => {
if (e.key === key) handler(e);
});
}
};
};
const minus = getBtn(minusKey);
const plus = getBtn(plusKey);
const num = () => value;
num.valueOf = () => value;
num.toString = () => String(value);
const update = (newVal) => {
value = newVal;
if (window.render) window.render(value);
return value;
};
minus.addEventListener('click', () => update(value - 1));
plus.addEventListener('click', () => update(value + 1));
document.addEventListener('keydown', (e) => {
if (e.key === minusKey) update(value - 1);
if (e.key === plusKey) update(value + 1);
});
return num;
}
function pix(x, primes) {
let left = 0;
let right = primes.length - 1;
let result = 0;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (primes[mid] <= x) {
result = mid + 1;
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
function li(x) {
if (x < 2) return 0;
const a = 2;
const b = x;
const n = 1000;
if (b <= a) return 0;
const h = (b - a) / n;
let sum = 0;
const f = (t) => {
if (t <= 1) return 0;
const ln_t = Math.log(t);
if (Math.abs(ln_t) < 1e-10) return 0;
return 1 / ln_t;
};
for (let i = 0; i <= n; i++) {
const t = a + i * h;
const val = f(t);
if (i === 0 || i === n) {
sum += val;
} else if (i % 2 === 1) {
sum += 4 * val;
} else {
sum += 2 * val;
}
}
return (h / 3) * sum;
}
function complex(x, y) {
return {
x, y,
add(w) { return complex(this.x + w.x, this.y + w.y); },
sub(w) { return complex(this.x - w.x, this.y - w.y); },
mul(w) { return complex(this.x * w.x - this.y * w.y, this.x * w.y + this.y * w.x); },
dot(w) { return complex(this.x * w.x + this.y * w.y, this.y * w.x - this.x * w.y); },
scale(k) { return complex(this.x * k, this.y * k); },
conj() { return complex(this.x, -this.y); },
inv() { const denom = this.x * this.x + this.y * this.y; return complex(this.x / denom, -this.y / denom); },
abs() { return Math.sqrt(this.x * this.x + this.y * this.y); }
};
}
function toComplex(a, b) {
if (a && typeof a === 'object' && 'x' in a && 'y' in a) {
return a;
}
if (typeof a === 'number' && typeof b === 'number') {
return complex(a, b);
}
if (typeof a === 'number' && b === undefined) {
return complex(a, 0);
}
return complex(0, 0);
}
function cmul_i(x, y) { const z = toComplex(x, y); return complex(-z.y, z.x); }
function conj(x, y) { const z = toComplex(x, y); return complex(z.x, -z.y); }
function cinv(x, y) { const z = toComplex(x, y); return z.inv(); }
function cexp(x, y) { const z = toComplex(x, y); const ex = Math.exp(z.x); return complex(ex * Math.cos(z.y), ex * Math.sin(z.y)); }
function clog(x, y) { const z = toComplex(x, y); return complex(Math.log(z.x * z.x + z.y * z.y) * 0.5, Math.atan2(z.y, z.x)); }
function csin(x, y) { const z = toComplex(x, y); return complex(Math.sin(z.x) * Math.cosh(z.y), Math.cos(z.x) * Math.sinh(z.y)); }
function ccos(x, y) { const z = toComplex(x, y); return complex(Math.cos(z.x) * Math.cosh(z.y), -Math.sin(z.x) * Math.sinh(z.y)); }
function cadd(z, w) { return z.add(w); }
function csub(z, w) { return z.sub(w); }
function cmul(z, w) { return z.mul(w); }
function cdiv(z, w) { return z.mul(w.inv()); }
function cpow(z, w) { return cexp(w.mul(clog(z))); }
function cdot(z, w) { return z.dot(w); }
function cmulcomp(z, w) { return complex(z.x * w.x, z.y * w.y); }
function cscale(z, k) { return z.scale(k); }
function cgamma_right(z) {
const w = complex(z.x - 1.0, z.y);
const t = complex(w.x + 7.5, w.y);
let x = complex(0.99999999999980993, 0.0);
x = x.add(complex(w.x + 1.0, w.y).inv().scale(676.5203681218851));
x = x.add(complex(w.x + 2.0, w.y).inv().scale(-1259.1392167224028));
x = x.add(complex(w.x + 3.0, w.y).inv().scale(771.32342877765313));
x = x.add(complex(w.x + 4.0, w.y).inv().scale(-176.61502916214059));
x = x.add(complex(w.x + 5.0, w.y).inv().scale(12.507343278686905));
x = x.add(complex(w.x + 6.0, w.y).inv().scale(-0.13857109526572012));
x = x.add(complex(w.x + 7.0, w.y).inv().scale(9.9843695780195716e-6));
x = x.add(complex(w.x + 8.0, w.y).inv().scale(1.5056327351493116e-7));
return cmul(x, cexp(cadd(cmul(clog(t), complex(0.5 + w.x, w.y)), t.scale(-1.0)))).scale(2.50662827463);
}
function cgamma_left(z) { return (cmul(csin(z.scale(Math.PI)), cgamma_right(complex(1.0 - z.x, -z.y)))).inv().scale(Math.PI); }
function cgamma(x, y) { let z = toComplex(x, y); z = complex(z.x + 1.0, z.y); return z.x < 0.5 ? cgamma_left(z) : cgamma_right(z); }
const EULER_GAMMA = 0.5772156649015329;
function cgammaw(x, y) {
let z = toComplex(x, y); z = complex(z.x + 1.0, z.y);
const one = complex(1.0, 0.0);
let result = cmul(z, cexp(z.scale(EULER_GAMMA)));
for (let n = 1; n <= 256; n++) {
const invn = 1.0 / n;
result = cmul(result, cmul(z.scale(invn).add(one), cexp(z.scale(-invn))));
}
return result.inv();
}
function cgammap(x, y) {
let z = toComplex(x, y); z = complex(z.x + 1.0, z.y);
const one = complex(1.0, 0.0);
let result = cmul(z, cexp(z.scale(EULER_GAMMA)));
const primesv = primes(256);
for (let n = 0; n < primesv.length; n++) {
const invn = 1.0 / primesv[n];
result = cmul(result, cmul(z.scale(invn).add(one), cexp(z.scale(-invn))));
}
return result.inv();
}
function randomColor() { const h = Math.floor(Math.random() * 360); return `hsl(${h},70%,55%)`; }
function extractReal(value) { return value && typeof value === 'object' && 'x' in value && 'y' in value ? value.x : value; }
const print = (fn, opts={}) => {
if (Array.isArray(fn)) {
const arr = fn;
fn = x => (x > arr.length - 1 ? 0 : arr[floor(x)]);
}
const wrappedFn = (x) => extractReal(fn(x));
graphs.push({
fn: wrappedFn,
color: opts.color || randomColor(),
width: opts.width || 2,
opacity: opts.opacity ?? 0.8
});
};
const plot = print;
function resize() {
const p = document.getElementById('graph');
canvas.width = p.clientWidth;
canvas.height = p.clientHeight;
render();
}
window.addEventListener('resize', resize);
canvas.addEventListener('mousedown', e => { dragging=true; lx=e.clientX; ly=e.clientY; });
canvas.addEventListener('mousemove', e => {
if (!dragging) return;
cx -= (e.clientX-lx)/scale;
cy += (e.clientY-ly)/scale;
lx=e.clientX; ly=e.clientY;
render();
});
canvas.addEventListener('mouseup', () => dragging=false);
canvas.addEventListener('mouseleave', () => dragging=false);
canvas.addEventListener('wheel', e => { e.preventDefault(); zoom(e.deltaY>0?0.9:1.1); });
function zoom(f) { scale *= f; render(); }
function resetView() { scale=50; cx=0; cy=0; render(); }
function toScreen(x, y) { return [(x-cx)*scale + canvas.width/2, canvas.height/2 - (y-cy)*scale]; }
function toMath(px, py) { return [(px - canvas.width/2)/scale + cx, -(py - canvas.height/2)/scale + cy]; }
function render() {
const W = canvas.width, H = canvas.height;
ctx.clearRect(0, 0, W, H);
ctx.fillStyle = '#1a1a1a';
ctx.fillRect(0, 0, W, H);
const [xMin, yMax] = toMath(0, 0);
const [xMax, yMin] = toMath(W, H);
let gridStep;
const targetPixels = 60;
const rawStep = targetPixels / scale;
const magnitude = Math.pow(10, Math.floor(Math.log10(rawStep)));
const normalized = rawStep / magnitude;
if (normalized < 1.5) gridStep = magnitude;
else if (normalized < 3.5) gridStep = 2 * magnitude;
else if (normalized < 7.5) gridStep = 5 * magnitude;
else gridStep = 10 * magnitude;
ctx.strokeStyle = '#2d2d2d';
ctx.lineWidth = 1.0;
ctx.beginPath();
const startX = Math.ceil(xMin / gridStep) * gridStep;
for (let x = startX; x <= xMax; x += gridStep) {
const px = (x - cx) * scale + W / 2;
if (px < -10 || px > W + 10) continue;
ctx.moveTo(px, 0);
ctx.lineTo(px, H);
}
const startY = Math.ceil(yMin / gridStep) * gridStep;
for (let y = startY; y <= yMax; y += gridStep) {
const py = H / 2 - (y - cy) * scale;
if (py < -10 || py > H + 10) continue;
ctx.moveTo(0, py);
ctx.lineTo(W, py);
}
ctx.stroke();
const labelStep = gridStep * (scale > 200 ? 1 : scale > 80 ? 2 : 4);
ctx.fillStyle = '#777';
ctx.textAlign = 'center';
const [, axisY_px] = toScreen(0, 0);
for (let x = Math.ceil(xMin / labelStep) * labelStep; x <= xMax; x += labelStep) {
if (Math.abs(x) < 1e-9) continue;
const px = (x - cx) * scale + W / 2;
if (px < 10 || px > W - 10) continue;
const labelY = Math.min(H - 4, Math.max(14, axisY_px + 16));
ctx.fillText(parseFloat(x.toFixed(6)).toString(), px, labelY);
}
ctx.textAlign = 'right';
const [axisX_px] = toScreen(0, 0);
for (let y = Math.ceil(yMin / labelStep) * labelStep; y <= yMax; y += labelStep) {
if (Math.abs(y) < 1e-9) continue;
const py = H / 2 - (y - cy) * scale;
if (py < 10 || py > H - 10) continue;
const labelX = Math.min(W - 4, Math.max(20, axisX_px - 8));
ctx.fillText(parseFloat(y.toFixed(6)).toString(), labelX, py + 4);
}
ctx.strokeStyle = '#545454';
ctx.lineWidth = 1.5;
ctx.beginPath();
if (axisY_px >= 0 && axisY_px <= H) {
ctx.moveTo(0, axisY_px); ctx.lineTo(W, axisY_px);
}
if (axisX_px >= 0 && axisX_px <= W) {
ctx.moveTo(axisX_px, H); ctx.lineTo(axisX_px, 0);
}
ctx.stroke();
const W_half = W / 2;
const H_half = H / 2;
const inv_scale = 1 / scale;
function findDomainEdge(fn, xValid, xInvalid) {
let a = xValid, b = xInvalid;
for (let i = 0; i < 16; i++) {
const mid = (a + b) * 0.5;
if (Number.isFinite(fn(mid))) a = mid;
else b = mid;
}
return a;
}
for (let g = 0; g < graphs.length; g++) {
const g_fn = graphs[g].fn;
ctx.strokeStyle = graphs[g].color;
ctx.lineWidth = graphs[g].width;
ctx.globalAlpha = graphs[g].opacity;
ctx.beginPath();
let inPath = false;
let prevPx = 0, prevPy = 0, prevX = 0, prevY = 0;
for (let px = 0; px <= W; px++) {
const x = (px - W_half) * inv_scale + cx;
const y = g_fn(x);
const isValid = Number.isFinite(y);
if (isValid) {
const py = H_half - (y - cy) * scale;
if (!inPath) {
const prevX_math = ((px - 1) - W_half) * inv_scale + cx;
const edgeX = findDomainEdge(g_fn, x, prevX_math);
const edgePx = (edgeX - cx) * scale + W_half;
const edgeY = g_fn(edgeX);
const edgePy = H_half - ((Number.isFinite(edgeY) ? edgeY : 0) - cy) * scale;
ctx.moveTo(edgePx, edgePy);
ctx.lineTo(px, py);
inPath = true;
} else {
if (Math.abs(py - prevPy) > H * 0.8 && Math.sign(y) !== Math.sign(prevY)) {
ctx.moveTo(px, py);
} else {
ctx.lineTo(px, py);
}
}
prevPx = px; prevPy = py; prevX = x; prevY = y;
} else {
if (inPath) {
const edgeX = findDomainEdge(g_fn, prevX, x);
const edgePx = (edgeX - cx) * scale + W_half;
const edgeY = g_fn(edgeX);
const edgePy = H_half - ((Number.isFinite(edgeY) ? edgeY : 0) - cy) * scale;
ctx.lineTo(edgePx, edgePy);
inPath = false;
}
}
}
ctx.stroke();
}
ctx.globalAlpha = 1;
document.getElementById('sc').textContent = (scale / 50).toFixed(2);
}
function showMsg(text, type) {
const m = document.getElementById('msg');
m.textContent = text;
m.className = type;
m.style.display = 'block';
setTimeout(() => m.style.display = 'none', 3000);
}
function runCode() {
const code = document.getElementById('code').value;
graphs = [];
try {
const fn = new Function('randomColor','sin','cos','tan','log','ln','log2','log10',
'sqrt','abs','exp','pow','PI','E','asin','acos','atan','atan2',
'sinh','cosh','tanh','floor','ceil','round','sign','min','max','random','primes','pix','li','complex',
'cgamma','csin','cexp','clog','cinv','cpow',
code);
fn(randomColor,sin,cos,tan,log,log,log2,log10,sqrt,abs,exp,pow,PI,E,
asin,acos,atan,atan2, sinh,cosh,tanh, floor,ceil,round,sign,min,max,random,primes,pix,li,complex,cgamma,csin,cexp,clog,cinv,cpow);
document.getElementById('cnt').textContent = graphs.length;
render();
showMsg(`✓ Build: ${graphs.length}`, 'ok');
try {
localStorage.setItem('plotter_code', code);
} catch(e) {}
} catch(e) {
showMsg(`✗ ${e.message}`, 'err');
}
}
document.getElementById('code').addEventListener('keydown', e => {
if (e.ctrlKey && e.key === 'Enter') runCode();
});
resize();
setTimeout(runCode, 100);
</script>
<script src="./zeta-fast.js"></script>
</body>
</html>