-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplines.html
More file actions
1149 lines (981 loc) · 54.7 KB
/
splines.html
File metadata and controls
1149 lines (981 loc) · 54.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
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="uk">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Симулятор Сплайнів (v5.3 - з Catmull-Rom)</title>
<script>
MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']]
}
};
</script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" id="MathJax-script"></script>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
line-height: 1.6;
background-color: #f4f7f6;
color: #333;
margin: 0;
padding: 20px;
}
h1, h2, h3 {
color: #2c3e50;
}
.container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.controls {
flex: 1;
min-width: 300px;
background: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.canvas-container {
flex: 2;
min-width: 400px;
position: relative;
}
canvas {
border: 1px solid #aaa;
background-color: #fff;
border-radius: 8px;
cursor: crosshair;
}
.controls label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.controls select, .controls button {
width: 100%;
padding: 8px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.controls button {
background-color: #e74c3c;
color: white;
border: none;
cursor: pointer;
}
.controls button:hover {
background-color: #c0392b;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 15px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: center;
}
th {
background-color: #ecf0f1;
}
td input {
width: 90%;
border: 1px solid #ccc;
border-radius: 4px;
padding: 4px;
text-align: center;
}
#gradient-controls {
display: none;
background-color: #f8f9fa;
border: 1px solid #e0e0e0;
border-radius: 4px;
padding: 10px;
margin-bottom: 15px;
}
.slider-container {
margin-bottom: 10px;
}
.slider-container label {
font-size: 0.9em;
font-weight: normal;
display: flex;
justify-content: space-between;
}
.slider-container input[type="range"] {
width: 100%;
margin-top: 5px;
}
#view-controls {
margin-bottom: 10px;
padding: 10px;
background: #f9f9f9;
border-radius: 4px;
border: 1px solid #ddd;
display: flex;
flex-wrap: wrap;
gap: 10px;
align-items: center;
font-size: 0.9em;
}
#view-controls label {
display: inline-block;
margin-bottom: 0;
font-weight: normal;
}
#view-controls input {
width: 60px;
padding: 4px;
}
#view-controls button {
width: auto;
padding: 5px 10px;
margin-bottom: 0;
font-size: 0.9em;
}
.hidden-control {
display: none !important;
}
#equations-container {
width: 100%;
margin-top: 20px;
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
#equations-container h3 {
margin-top: 0;
}
#equations-container div {
font-family: "Courier New", Courier, monospace;
font-size: 0.95em;
padding: 8px;
border-radius: 4px;
margin-bottom: 5px;
overflow-x: auto;
}
#equations-container .mathjax-explanation {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
font-size: 0.9em;
color: #333;
margin-top: 10px;
line-height: 1.6;
}
#theory {
width: 100%;
margin-top: 20px;
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
#theory summary {
padding: 20px;
font-size: 1.2em;
font-weight: bold;
cursor: pointer;
color: #2980b9;
}
#theory-content {
padding: 0 20px 20px 20px;
border-top: 1px solid #eee;
font-family: "Times New Roman", Times, serif;
font-size: 1.1em;
line-height: 1.7;
}
</style>
</head>
<body>
<h1>📐 Симулятор Сплайнів </h1>
<div class="container">
<div class="controls">
<h2>Панель керування</h2>
<label for="splineType">Тип сплайна:</label>
<select id="splineType">
<option value="cubic">Кубічний (Natural/Clamped)</option>
<option value="catmull_rom">Catmull-Rom </option>
<option value="quadratic">Квадратичний (Інтерполяція)</option>
<option value="b_spline">B-spline (Апроксимація)</option>
</select>
<div id="alphaControl" style="display: none;"> <label for="alphaSlider">Напруженість (Alpha): <span id="alphaValue">0.5</span></label>
<input type="range" id="alphaSlider" min="0" max="1" step="0.01" value="0.5" class="slider">
<small><i>(0.5 = Catmull-Rom, 0 = Ослаблена, 1 = Перетягнута)</i></small>
</div>
<div id="boundary-controls-container">
<label for="boundaryCond">Крайові умови:</label>
<select id="boundaryCond">
</select>
<div id="gradient-controls">
<div class="slider-container">
<label for="startSlope">Початковий нахил S'(x₀): <span id="startSlopeValue">0.0</span></label>
<input type="range" id="startSlope" min="-10" max="10" step="0.1" value="0">
</div>
<div class="slider-container">
<label for="endSlope">Кінцевий нахил S'(xₙ₋₁): <span id="endSlopeValue">0.0</span></label>
<input type="range" id="endSlope" min="-10" max="10" step="0.1" value="0">
</div>
</div>
</div>
<button id="clearButton">Очистити точки</button>
<h3>Точки (макс. 8)</h3>
<p style="font-size: 0.9em; color: #555;">Клікніть на графіку або введіть значення в таблицю.</p>
<table id="pointsTable">
<thead>
<tr>
<th>#</th>
<th>X</th>
<th>Y</th>
</tr>
</thead>
<tbody id="pointsTableBody">
</tbody>
</table>
</div>
<div class="canvas-container">
<div id="view-controls">
<b>Керування видом:</b>
<label>X min: <input type="number" id="viewMinX" step="10"></label>
<label>X max: <input type="number" id="viewMaxX" step="10"></label>
<label>Y min: <input type="number" id="viewMinY" step="10"></label>
<label>Y max: <input type="number" id="viewMaxY" step="10"></label>
<button id="applyViewButton" style="background-color: #3498db;">Застосувати</button>
<button id="resetViewButton" style="background-color: #95a5a6;">Скинути</button>
</div>
<canvas id="splineCanvas" width="600" height="400"></canvas>
</div>
</div>
<div id="equations-container">
<h3>Рівняння / Опис</h3>
<div id="equations-list">
<p>Додайте принаймні 2 точки, щоб побачити рівняння.</p>
</div>
</div>
<details id="theory" open>
<summary>📘 Теоретичні відомості</summary>
<div id="theory-content">
<p><b>Сплайн</b> — це спеціальна функція, яка використовується в чисельних методах для інтерполяції та наближення. Вона являє собою "зшиту" з кількох поліноміальних сегментів криву. Головна перевага сплайнів — вони дозволяють отримати гладку криву, що проходить через заданий набір точок (вузлів), при цьому уникаючи "ефекту Рунге" (сильних коливань), який часто виникає при використанні одного полінома високого степеня.</p>
<hr>
<h2>Квадратичний сплайн (Інтерполяція)</h2>
<p>Для $n$ точок $(x_0, y_0), \dots, (x_{n-1}, y_{n-1})$ ми будуємо $n-1$ сегментів $S_i(x)$ на відрізку $[x_i, x_{i+1}]$:</p>
$$S_i(x) = a_i (x-x_i)^2 + b_i (x-x_i) + c_i$$
<p><b>Умови:</b> $S_i(x_i) = y_i$, $S_i(x_{i+1}) = y_{i+1}$ ($C^0$ неперервність) та $S'_{i-1}(x_i) = S'_i(x_i)$ ($C^1$ гладкість). Це залишає 1 ступінь свободи, який визначається крайовою умовою (наприклад, $S'_0(x_0) = 0$).</p>
<hr>
<h2>Кубічний сплайн (Інтерполяція, $C^2$)</h2>
<p>Це найпоширеніший "академічний" тип ($C^2$ гладкість). Для $n$ точок ми будуємо $n-1$ сегментів $S_i(x)$:</p>
$$S_i(x) = a_i (x-x_i)^3 + b_i (x-x_i)^2 + c_i (x-x_i) + d_i$$
<p><b>Умови:</b> $C^0$, $C^1$ та $C^2$ неперервність у вузлах. Це залишає 2 ступені свободи, які визначаються крайовими умовами (Natural, Clamped).</p>
<p><b>Особливість:</b> Розв'язується глобальна система рівнянь. Зміна однієї точки впливає на всю криву. Мінімізація $S''$ (у "Natural") може призвести до "зайвих" осциляцій далеко від вузлів.</p>
<hr>
<h2>Сплайн Катмулла-Рома (Інтерполяція, $C^1$)</h2>
<p>Це теж кубічний сплайн, але іншого типу. Він **гарантовано проходить через всі точки** (інтерполює), але має лише $C^1$ гладкість. Це стандарт для плавної анімації та графіків, як в Excel.</p>
<p><b>Особливість:</b> Це параметрична крива $C(t)$. Сегмент $S_i(t)$ (від $P_i$ до $P_{i+1}$, де $t \in [0, 1]$) визначається чотирма точками: $P_{i-1}, P_i, P_{i+1}, P_{i+2}$.</p>
<p>Дотична (нахил) у кожній точці $P_i$ не розв'язується глобально, а обчислюється автоматично за формулою, що базується на сусідніх точках:</p>
$$T_i = \alpha (P_{i+1} - P_{i-1})$$
<p>(де $\alpha$ - "напруженість", зазвичай 0.5). Це робить його поведінку дуже "локальною" та візуально приємною, уникаючи широких осциляцій "Natural" сплайна.</p>
<hr>
<h2>B-сплайн (Апроксимація)</h2>
<p>На відміну від інтерполюючих сплайнів, <b>B-сплайни</b> зазвичай не проходять через задані точки. Натомість точки виступають як <b>"контрольні точки"</b>, які "притягують" до себе криву.</p>
<p>Крива B-сплайна визначається як $C(t) = \sum_{i=0}^{n-1} P_i N_{i,p}(t)$.</p>
<p><b>Переваги:</b> Надзвичайна гладкість, ідеально для показу трендів зашумлених даних, ігнорує "викиди".</p>
<hr>
<h2>Порівняння з іншими методами</h2>
<table style="width:100%; border-collapse: collapse;">
<thead style="background-color: #ecf0f1;">
<tr>
<th style="border: 1px solid #ddd; padding: 8px;">Метод</th>
<th style="border: 1px solid #ddd; padding: 8px;">Інтерполює? (Проходить через точки)</th>
<th style="border: 1px solid #ddd; padding: 8px;">Переваги</th>
<th style="border: 1px solid #ddd; padding: 8px;">Недоліки</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid #ddd; padding: 8px;"><b>Поліном Лагранжа</b></td>
<td style="border: 1px solid #ddd; padding: 8px;"><b>Так</b></td>
<td style="border: 1px solid #ddd; padding: 8px;">- Одна глобальна функція.</td>
<td style="border: 1px solid #ddd; padding: 8px;">- <b>Ефект Рунге:</b> сильні коливання.</td>
</tr>
<tr>
<td style="border: 1px solid #ddd; padding: 8px;"><b>Кубічний (Natural)</b></td>
<td style="border: 1px solid #ddd; padding: 8px;"><b>Так</b></td>
<td style="border: 1px solid #ddd; padding: 8px;">- <b>Гладкість $C^2$</b> (неперервна кривина).<br>- Математично "ідеальний".</td>
<td style="border: 1px solid #ddd; padding: 8px;">- Глобальний вплив (зміна 1 точки = зміна всієї кривої).<br>- Може створювати "зайві" хвилі.</td>
</tr>
<tr>
<td style="border: 1px solid #ddd; padding: 8px;"><b>Catmull-Rom</b></td>
<td style="border: 1px solid #ddd; padding: 8px;"><b>Так</b></td>
<td style="border: 1px solid #ddd; padding: 8px;">- <b>Як в Excel/Spreadsheets.</b><br>- Візуально плавний.<br>- Локальний контроль (зміна 1 точки = зміна поруч).</td>
<td style="border: 1px solid #ddd; padding: 8px;">- Лише $C^1$ гладкість (кривина може "стрибати" у вузлах).<br>- Може створювати петлі.</td>
</tr>
<tr>
<td style="border: 1px solid #ddd; padding: 8px;"><b>B-сплайн</b></td>
<td style="border: 1px solid #ddd; padding: 8px;"><b>Ні</b> (Апроксимує)</td>
<td style="border: 1px solid #ddd; padding: 8px;">- <b>Найвища гладкість.</b><br>- Ідеально для трендів/шуму.<br>- Локальний контроль.</td>
<td style="border: 1px solid #ddd; padding: 8px;">- Не проходить через точки (це його мета).</td>
</tr>
</tbody>
</table>
</div>
</details>
<script>
document.addEventListener('DOMContentLoaded', () => {
// --- Отримання елементів DOM ---
const canvas = document.getElementById('splineCanvas');
const ctx = canvas.getContext('2d');
const splineTypeSelect = document.getElementById('splineType');
const boundaryCondSelect = document.getElementById('boundaryCond');
const pointsTableBody = document.getElementById('pointsTableBody');
const clearButton = document.getElementById('clearButton');
const equationsList = document.getElementById('equations-list');
const boundaryControlsContainer = document.getElementById('boundary-controls-container');
const gradientControls = document.getElementById('gradient-controls');
const startSlope = document.getElementById('startSlope');
const startSlopeValue = document.getElementById('startSlopeValue');
const endSlope = document.getElementById('endSlope');
const endSlopeValue = document.getElementById('endSlopeValue');
// *** НОВІ ЕЛЕМЕНТИ ДЛЯ CATMULL-ROM ***
const alphaControl = document.getElementById('alphaControl');
const alphaSlider = document.getElementById('alphaSlider');
const alphaValue = document.getElementById('alphaValue');
let points = [];
const MAX_POINTS = 8;
const POINT_RADIUS = 5;
const segmentColors = ['#e74c3c', '#3498db', '#2ecc71', '#9b59b6', '#f1c40f', '#e67e22', '#1abc9c', '#34495e'];
const DEFAULT_VIEW = { minX: 0, maxX: 600, minY: 0, maxY: 400 };
let view = { ...DEFAULT_VIEW };
const viewMinXInput = document.getElementById('viewMinX');
const viewMaxXInput = document.getElementById('viewMaxX');
const viewMinYInput = document.getElementById('viewMinY');
const viewMaxYInput = document.getElementById('viewMaxY');
const applyViewButton = document.getElementById('applyViewButton');
const resetViewButton = document.getElementById('resetViewButton');
// === Ініціалізація та Оновлення UI ===
function updateBoundaryOptions() {
const type = splineTypeSelect.value;
const currentBoundary = boundaryCondSelect.value;
boundaryCondSelect.innerHTML = '';
// *** ОНОВЛЕНО: Ховаємо для B-spline ТА Catmull-Rom ***
if (type === 'b_spline' || type === 'catmull_rom') {
boundaryControlsContainer.classList.add('hidden-control');
} else {
boundaryControlsContainer.classList.remove('hidden-control');
}
// *** ОНОВЛЕНО: Показуємо/ховаємо слайдер Alpha ***
if (type === 'catmull_rom') {
alphaControl.style.display = 'block';
} else {
alphaControl.style.display = 'none';
}
// (ця логіка залишається, але boundaryControlsContainer вже прихований, якщо треба)
if (type === 'cubic') {
boundaryCondSelect.innerHTML = `
<option value="natural">Natural (S'' = 0 на кінцях)</option>
<option value="clamped">Clamped (Затиснутий)</option>
`;
if (currentBoundary === 'natural' || currentBoundary === 'clamped') {
boundaryCondSelect.value = currentBoundary;
}
} else if (type === 'quadratic') {
boundaryCondSelect.innerHTML = `
<option value="start_zero">Simple (S' = 0 на початку)</option>
<option value="end_zero">Simple (S' = 0 в кінці)</option>
`;
if (currentBoundary === 'start_zero' || currentBoundary === 'end_zero') {
boundaryCondSelect.value = currentBoundary;
}
}
toggleGradientControls();
mainDraw();
}
function toggleGradientControls() {
const type = splineTypeSelect.value;
const boundary = boundaryCondSelect.value;
if (type === 'cubic' && boundary === 'clamped') {
gradientControls.style.display = 'block';
} else {
gradientControls.style.display = 'none';
}
}
// --- Функції: Система координат, Cітка, Вид ---
function worldToPixel(worldX, worldY) {
const pixelX = (worldX - view.minX) / (view.maxX - view.minX) * canvas.width;
const pixelY = (1 - (worldY - view.minY) / (view.maxY - view.minY)) * canvas.height;
return { x: pixelX, y: pixelY };
}
function pixelToWorld(pixelX, pixelY) {
const worldX = (pixelX / canvas.width) * (view.maxX - view.minX) + view.minX;
const worldY = (1 - pixelY / canvas.height) * (view.maxY - view.minY) + view.minY;
return { x: worldX, y: worldY };
}
function updateInputsFromView() {
viewMinXInput.value = view.minX.toFixed(0);
viewMaxXInput.value = view.maxX.toFixed(0);
viewMinYInput.value = view.minY.toFixed(0);
viewMaxYInput.value = view.maxY.toFixed(0);
}
function updateViewFromInputs() {
const minX = parseFloat(viewMinXInput.value);
const maxX = parseFloat(viewMaxXInput.value);
const minY = parseFloat(viewMinYInput.value);
const maxY = parseFloat(viewMaxYInput.value);
if (isNaN(minX) || isNaN(maxX) || isNaN(minY) || isNaN(maxY)) {
alert("Некоректні значення для виду.");
return;
}
if (maxX <= minX || maxY <= minY) {
alert("Max має бути більшим за Min.");
return;
}
view = { minX, maxX, minY, maxY };
mainDraw();
}
function drawGrid() {
ctx.save();
ctx.strokeStyle = '#eee';
ctx.lineWidth = 1;
ctx.font = "10px Arial";
ctx.fillStyle = "#aaa";
ctx.textAlign = "left";
ctx.textBaseline = "top";
const xRange = view.maxX - view.minX;
const yRange = view.maxY - view.minY;
const xPower = Math.pow(10, Math.floor(Math.log10(xRange)) - 1);
const xStep = (xRange / xPower > 20) ? xPower * 5 : (xRange / xPower > 10 ? xPower * 2 : xPower);
const yPower = Math.pow(10, Math.floor(Math.log10(yRange)) - 1);
const yStep = (yRange / yPower > 20) ? yPower * 5 : (yRange / yPower > 10 ? yPower * 2 : yPower);
const xStart = Math.ceil(view.minX / xStep) * xStep;
const yStart = Math.ceil(view.minY / yStep) * yStep;
for (let x = xStart; x <= view.maxX; x += xStep) {
const { x: pixelX } = worldToPixel(x, 0);
ctx.beginPath();
ctx.moveTo(pixelX, 0);
ctx.lineTo(pixelX, canvas.height);
ctx.stroke();
ctx.fillText(x.toFixed(1), pixelX + 2, 2);
}
for (let y = yStart; y <= view.maxY; y += yStep) {
const { y: pixelY } = worldToPixel(0, y);
ctx.beginPath();
ctx.moveTo(0, pixelY);
ctx.lineTo(canvas.width, pixelY);
ctx.stroke();
ctx.fillText(y.toFixed(1), 2, pixelY + 2);
}
ctx.restore();
}
// === Обробники подій ===
splineTypeSelect.addEventListener('change', updateBoundaryOptions);
boundaryCondSelect.addEventListener('change', () => {
toggleGradientControls();
mainDraw();
});
clearButton.addEventListener('click', () => {
points = [];
mainDraw();
});
canvas.addEventListener('click', (e) => {
if (points.length >= MAX_POINTS) {
alert(`Досягнуто ліміту в ${MAX_POINTS} точок.`);
return;
}
const rect = canvas.getBoundingClientRect();
const pixelX = e.clientX - rect.left;
const pixelY = e.clientY - rect.top;
const { x: worldX, y: worldY } = pixelToWorld(pixelX, pixelY);
points.push({ x: worldX, y: worldY });
mainDraw();
});
startSlope.addEventListener('input', () => {
startSlopeValue.textContent = parseFloat(startSlope.value).toFixed(1);
mainDraw();
});
endSlope.addEventListener('input', () => {
endSlopeValue.textContent = parseFloat(endSlope.value).toFixed(1);
mainDraw();
});
// *** НОВИЙ ОБРОБНИК ДЛЯ СЛАЙДЕРА ALPHA ***
alphaSlider.addEventListener('input', () => {
alphaValue.textContent = parseFloat(alphaSlider.value).toFixed(2);
mainDraw();
});
applyViewButton.addEventListener('click', updateViewFromInputs);
resetViewButton.addEventListener('click', () => {
view = { ...DEFAULT_VIEW };
updateInputsFromView();
mainDraw();
});
// === Синхронізація таблиці та точок ===
function updateTableFromPoints() {
pointsTableBody.innerHTML = '';
let sortedPoints = [...points];
// *** ОНОВЛЕНО: Сортуємо для всіх, крім B-spline (для Catmull-Rom порядок кліків теж має значення) ***
const type = splineTypeSelect.value;
if (type === 'cubic' || type === 'quadratic') {
sortedPoints.sort((a, b) => a.x - b.x);
}
// Для B-spline та Catmull-Rom сортування не потрібне, порядок введення важливий
sortedPoints.forEach((point, index) => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${index + 1}</td>
<td><input type="number" step="0.1" class="point-input" data-index="${index}" data-coord="x" value="${point.x.toFixed(2)}"></td>
<td><input type="number" step="0.1" class="point-input" data-index="${index}" data-coord="y" value="${point.y.toFixed(2)}"></td>
`;
pointsTableBody.appendChild(row);
});
document.querySelectorAll('.point-input').forEach(input => {
input.addEventListener('change', updatePointsFromTable);
});
}
function updatePointsFromTable() {
const newPoints = [];
const rows = pointsTableBody.querySelectorAll('tr');
rows.forEach((row, index) => {
const xInput = row.querySelector('input[data-coord="x"]');
const yInput = row.querySelector('input[data-coord="y"]');
const x = parseFloat(xInput.value);
const y = parseFloat(yInput.value);
if (!isNaN(x) && !isNaN(y)) {
newPoints.push({ x, y });
}
});
points = newPoints;
mainDraw();
}
// === Головна функція (Обчислення та Рендеринг) ===
function mainDraw() {
const type = splineTypeSelect.value;
// *** ОНОВЛЕНО: Сортування ***
if (type === 'cubic' || type === 'quadratic') {
points.sort((a, b) => a.x - b.x);
}
updateTableFromPoints();
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawGrid();
drawPoints();
if (points.length < 2) {
equationsList.innerHTML = '<p>Потрібно щонайменше 2 точки.</p>';
return;
}
try {
if (type === 'b_spline') {
const bSplineDegree = 3;
if (points.length <= bSplineDegree) {
equationsList.innerHTML = `<p>B-сплайн (кубічний) потребує щонайменше ${bSplineDegree + 1} контрольні точки.</p>`;
return;
}
const bSplineCurvePoints = calculateBSplinePoints(points, bSplineDegree, 100);
drawParametricSpline(bSplineCurvePoints, '#007bff');
displayEquationsBSpline(points, bSplineDegree);
} else if (type === 'catmull_rom') {
// *** ОНОВЛЕНО: Блок Catmull-Rom зі зчитуванням Alpha ***
if (points.length < 2) {
equationsList.innerHTML = '<p>Сплайн Катмулла-Рома потребує щонайменше 2 точки.</p>';
return;
}
// *** ЗЧИТУЄМО ALPHA ЗІ СЛАЙДЕРА ***
const alpha = parseFloat(alphaSlider.value);
// *** ПЕРЕДАЄМО ALPHA В ОБЧИСЛЕННЯ ***
const crCurvePoints = calculateCatmullRomSpline(points, 20, alpha); // 20 кроків на сегмент
drawParametricSpline(crCurvePoints, '#28a745'); // Малюємо зеленим
// *** ПЕРЕДАЄМО ALPHA У ВІДОБРАЖЕННЯ РІВНЯНЬ ***
displayEquationsCatmullRom(points, alpha);
} else {
// (Cubic та Quadratic)
let segments = [];
if (type === 'cubic') {
segments = calculateCubicSpline(points, boundaryCondSelect.value);
} else {
segments = calculateQuadraticSpline(points, boundaryCondSelect.value);
}
if (segments.length > 0) {
drawSegmentedSpline(segments);
displayEquations(segments);
}
}
} catch (error) {
equationsList.innerHTML = `<p style="color: red;">Помилка обчислення: ${error.message}</p>`;
return;
}
}
// === Функції рендерингу ===
function drawPoints() {
const type = splineTypeSelect.value;
// *** ОНОВЛЕНО: Колір точок ***
if (type === 'b_spline') {
ctx.fillStyle = '#aaa'; // Сірі "контрольні точки"
ctx.strokeStyle = '#aaa';
ctx.lineWidth = 1;
// Малюємо "контрольний полігон"
let first = true;
ctx.beginPath();
for (const point of points) {
const { x: pixelX, y: pixelY } = worldToPixel(point.x, point.y);
if (first) {
ctx.moveTo(pixelX, pixelY);
first = false;
} else {
ctx.lineTo(pixelX, pixelY);
}
}
ctx.stroke();
} else {
ctx.fillStyle = '#000'; // Чорні "вузли" (для Cubic, Quadratic, Catmull-Rom)
}
for (const point of points) {
const { x: pixelX, y: pixelY } = worldToPixel(point.x, point.y);
if (pixelX > -5 && pixelX < canvas.width + 5 && pixelY > -5 && pixelY < canvas.height + 5) {
ctx.beginPath();
ctx.arc(pixelX, pixelY, POINT_RADIUS, 0, 2 * Math.PI);
ctx.fill();
}
}
}
// *** (ПЕРЕЙМЕНОВАНО) Малює y=f(x) сплайни ***
function drawSegmentedSpline(segments) {
ctx.lineWidth = 2;
for (let i = 0; i < segments.length; i++) {
const segment = segments[i];
const p1 = points[i];
const p2 = points[i+1];
const color = segmentColors[i % segmentColors.length];
ctx.strokeStyle = color;
const { x: startPixelX, y: startPixelY } = worldToPixel(p1.x, p1.y);
ctx.beginPath();
ctx.moveTo(startPixelX, startPixelY);
const step = Math.max(1e-3, (p2.x - p1.x) / 100);
for (let world_x = p1.x + step; world_x < p2.x; world_x += step) {
const world_y = segment.eval(world_x);
const { x: pixelX, y: pixelY } = worldToPixel(world_x, world_y);
ctx.lineTo(pixelX, pixelY);
}
const { x: endPixelX, y: endPixelY } = worldToPixel(p2.x, p2.y);
ctx.lineTo(endPixelX, endPixelY);
ctx.stroke();
}
}
// *** (ПЕРЕЙМЕНОВАНО) Малює параметричні (B-spline, Catmull-Rom) ***
function drawParametricSpline(curvePoints, color) {
ctx.lineWidth = 2;
ctx.strokeStyle = color;
ctx.beginPath();
let first = true;
for(const point of curvePoints) {
const { x: pixelX, y: pixelY } = worldToPixel(point.x, point.y);
if (first) {
ctx.moveTo(pixelX, pixelY);
first = false;
} else {
ctx.lineTo(pixelX, pixelY);
}
}
ctx.stroke();
}
// --- Функції відображення рівнянь ---
function displayEquations(segments) {
equationsList.innerHTML = '';
segments.forEach((seg, i) => {
const color = segmentColors[i % segmentColors.length];
const p1 = points[i];
const p2 = points[i+1];
const eqDiv = document.createElement('div');
eqDiv.style.borderLeft = `4px solid ${color}`;
eqDiv.style.backgroundColor = `${color}1A`;
const x_i_str = p1.x.toFixed(2);
let eqnStr = `<b>S<sub>${i}</sub>(x) = </b>`;
if (seg.type === 'cubic') {
const { a, b, c, d } = seg.coeffs;
const f = (val) => (val >= 0 ? ' + ' : ' - ') + Math.abs(val).toFixed(4);
eqnStr += `${a.toFixed(4)}(x - ${x_i_str})³`;
eqnStr += `${f(b)}(x - ${x_i_str})²`;
eqnStr += `${f(c)}(x - ${x_i_str})`;
eqnStr += `${f(d)}`;
} else if (seg.type === 'quadratic') {
const { a, b, c } = seg.coeffs;
const f = (val) => (val >= 0 ? ' + ' : ' - ') + Math.abs(val).toFixed(4);
eqnStr += `${a.toFixed(4)}(x - ${x_i_str})²`;
eqnStr += `${f(b)}(x - ${x_i_str})`;
eqnStr += `${f(c)}`;
}
eqDiv.innerHTML = `${eqnStr} <i>[для x від ${x_i_str} до ${p2.x.toFixed(2)}]</i>`;
equationsList.appendChild(eqDiv);
});
}
// *** ФУНКЦІЯ: Опис для B-Spline (без змін) ***
function displayEquationsBSpline(points) {
equationsList.innerHTML = '';
const n = points.length;
const eqDiv = document.createElement('div');
eqDiv.style.borderLeft = `4px solid #007bff`; // Синій
eqDiv.style.backgroundColor = `#007bff1A`;
eqDiv.style.fontFamily = 'Arial, sans-serif';
eqDiv.innerHTML = `<b>B-Сплайн (Апроксимація)</b>
<p class="mathjax-explanation">
Це <b>апроксимуюча</b> (згладжуюча) крива. Вона не проходить через контрольні точки (окрім, можливо, кінцевих), а лише "приблизно" слідує за ними, формуючи гладкий шлях.
</p>
<p class="mathjax-explanation">
Кожен кубічний сегмент $\\mathbf{S}_i(t)$ (де параметр $t \\in [0, 1]$) обчислюється, використовуючи 4 контрольні точки: $\\mathbf{P}_{i-1}, \\mathbf{P}_i, \\mathbf{P}_{i+1}, \\mathbf{P}_{i+2}$.
</p>
<p class="mathjax-explanation">
Тут $\\mathbf{P}_i = (x_i, y_i)$ — це ваші контрольні точки (вектори). Рівняння сегмента:
</p>
$$ \\mathbf{S}_i(t) = N_0(t)\\mathbf{P}_{i-1} + N_1(t)\\mathbf{P}_i + N_2(t)\\mathbf{P}_{i+1} + N_3(t)\\mathbf{P}_{i+2} $$
<p class="mathjax-explanation">
$N_0...N_3$ — це "базисні функції" (або "функції змішування"). Для стандартного <i>кубічного рівномірного B-сплайна</i> вони виглядають так:
</p>
$$ N_0(t) = \\frac{1}{6}(1-t)^3 = \\frac{1}{6}(-t^3 + 3t^2 - 3t + 1) $$
$$ N_1(t) = \\frac{1}{6}(3t^3 - 6t^2 + 4) $$
$$ N_2(t) = \\frac{1}{6}(-3t^3 + 3t^2 + 3t + 1) $$
$$ N_3(t) = \\frac{1}{6}(t^3) $$
<p class="mathjax-explanation">
Якщо згрупувати за степенями $t$, ми отримаємо ту ж поліноміальну форму $\\mathbf{S}_i(t) = \\mathbf{c}_0 + \\mathbf{c}_1 t + \\mathbf{c}_2 t^2 + \\mathbf{c}_3 t^3$:
</p>
$$ \\mathbf{c}_0 = \\frac{1}{6} (\\mathbf{P}_{i-1} + 4\\mathbf{P}_i + \\mathbf{P}_{i+1}) $$
$$ \\mathbf{c}_1 = \\frac{1}{6} (-3\\mathbf{P}_{i-1} + 3\\mathbf{P}_{i+1}) $$
$$ \\mathbf{c}_2 = \\frac{1}{6} (3\\mathbf{P}_{i-1} - 6\\mathbf{P}_i + 3\\mathbf{P}_{i+1}) $$
$$ \\mathbf{c}_3 = \\frac{1}{6} (-\\mathbf{P}_{i-1} + 3\\mathbf{P}_i - 3\\mathbf{P}_{i+1} + \\mathbf{P}_{i+2}) $$`;
equationsList.appendChild(eqDiv);
if (window.MathJax) { MathJax.typesetPromise([eqDiv]); }
}
// *** ОНОВЛЕНА ФУНКЦІЯ: Опис для Catmull-Rom (з Alpha) ***
function displayEquationsCatmullRom(points, alpha) {
equationsList.innerHTML = '';
const n = points.length;
const alphaStr = alpha.toFixed(2);
const eqDiv = document.createElement('div');
eqDiv.style.borderLeft = `4px solid #28a745`; // Зелений
eqDiv.style.backgroundColor = `#28a7451A`;
eqDiv.style.fontFamily = 'Arial, sans-serif';
eqDiv.innerHTML = `<b>Сплайн Катмулла-Рома (Інтерполяція)</b>
<p class="mathjax-explanation">
Це <b>інтерполююча</b> параметрична крива. Вона складається з кубічних сегментів $S_i(t)$, що "зшиті" з $C^1$ гладкістю (нахили збігаються у вузлах).
</p>
<p class="mathjax-explanation">
Кожен сегмент $S_i(t)$ (що йде від $\\mathbf{P}_i$ до $\\mathbf{P}_{i+1}$) обчислюється, використовуючи 4 точки: $\\mathbf{P}_{i-1}, \\mathbf{P}_i, \\mathbf{P}_{i+1}, \\mathbf{P}_{i+2}$.
</p>
<p class="mathjax-explanation">
Тут $\\mathbf{P}_i = (x_i, y_i)$ — це ваші точки (вектори). Рівняння сегмента, де $t \\in [0, 1]$:
</p>
$$ \\mathbf{S}_i(t) = \\mathbf{c}_0 + \\mathbf{c}_1 t + \\mathbf{c}_2 t^2 + \\mathbf{c}_3 t^3 $$
<p class="mathjax-explanation">
Де $\\alpha = ${alphaStr}$ (ваша "напруженість"). Коефіцієнти (вектори $\\mathbf{c}$) залежать від 4-х точок:
</p>
$$ \\mathbf{c}_0 = \\mathbf{P}_i $$
$$ \\mathbf{c}_1 = \\alpha (\\mathbf{P}_{i+1} - \\mathbf{P}_{i-1}) $$
$$ \\mathbf{c}_2 = (2\\alpha)\\mathbf{P}_{i-1} + (-3 + \\alpha)\\mathbf{P}_i + (3 - 2\\alpha)\\mathbf{P}_{i+1} - \\alpha\\mathbf{P}_{i+2} $$
$$ \\mathbf{c}_3 = (-\\alpha)\\mathbf{P}_{i-1} + (2 - \\alpha)\\mathbf{P}_i + (-2 + \\alpha)\\mathbf{P}_{i+1} + \\alpha\\mathbf{P}_{i+2} $$
<p class="mathjax-explanation" style="font-size: 0.9em; color: #555;">
<i>(Стандартний Catmull-Rom $\\alpha=0.5$. $\\alpha=0$ - ослаблена (Tension=0). $\\alpha=1$ - "перетягнута" (Tension=1).)</i>
</p>`;
equationsList.appendChild(eqDiv);
if (window.MathJax) { MathJax.typesetPromise([eqDiv]); }
}
// === Математика сплайнів ===
// (solveTridiagonal, calculateCubicSpline, calculateQuadraticSpline залишаються без змін)
function solveTridiagonal(A, B, C, D) {
const n = B.length;
const C_prime = new Array(n).fill(0);
const D_prime = new Array(n).fill(0);
const X = new Array(n).fill(0);
C_prime[0] = C[0] / B[0];
D_prime[0] = D[0] / B[0];
for (let i = 1; i < n; i++) {
const m = B[i] - A[i] * C_prime[i - 1];
C_prime[i] = C[i] / m;
D_prime[i] = (D[i] - A[i] * D_prime[i - 1]) / m;
}
X[n - 1] = D_prime[n - 1];
for (let i = n - 2; i >= 0; i--) {
X[i] = D_prime[i] - C_prime[i] * X[i + 1];
}
return X;
}
function calculateCubicSpline(pts, boundaryCond) {
const n = pts.length;
if (n < 2) return [];
const h = new Array(n - 1);
const delta = new Array(n - 1);
for (let i = 0; i < n - 1; i++) {
h[i] = pts[i + 1].x - pts[i].x;
if (h[i] <= 0) {
throw new Error("Точки мають бути унікальними та відсортованими по X.");
}
delta[i] = (pts[i + 1].y - pts[i].y) / h[i];
}
const A = new Array(n).fill(0);
const B = new Array(n).fill(0);
const C = new Array(n).fill(0);
const D = new Array(n).fill(0);
for (let i = 1; i < n - 1; i++) {
A[i] = h[i - 1];
B[i] = 2 * (h[i - 1] + h[i]);
C[i] = h[i];
D[i] = 6 * (delta[i] - delta[i - 1]);
}
if (boundaryCond === 'natural') {
B[0] = 1; C[0] = 0; D[0] = 0;
A[n - 1] = 0; B[n - 1] = 1; D[n - 1] = 0;
} else if (boundaryCond === 'clamped') {
const f_prime_0 = parseFloat(startSlope.value);
const f_prime_n_1 = parseFloat(endSlope.value);
B[0] = 2 * h[0];
C[0] = h[0];
D[0] = 6 * (delta[0] - f_prime_0);
A[n - 1] = h[n - 2];
B[n - 1] = 2 * h[n - 2];
D[n - 1] = 6 * (f_prime_n_1 - delta[n - 2]);
}
const M = solveTridiagonal(A, B, C, D);
const segments = [];
for (let i = 0; i < n - 1; i++) {
const d = pts[i].y;
const c = delta[i] - h[i] * (2 * M[i] + M[i + 1]) / 6;
const b = M[i] / 2;
const a = (M[i + 1] - M[i]) / (6 * h[i]);
segments.push({
type: 'cubic',
coeffs: { a, b, c, d },
xi: pts[i].x,
eval: function(x) {
const t = x - this.xi;
return this.coeffs.a * t**3 + this.coeffs.b * t**2 + this.coeffs.c * t + this.coeffs.d;
}
});
}
return segments;
}
function calculateQuadraticSpline(pts, boundaryCond) {
const n = pts.length;
if (n < 2) return [];
const h = new Array(n - 1);
const delta = new Array(n - 1);
for (let i = 0; i < n - 1; i++) {
h[i] = pts[i + 1].x - pts[i].x;
if (h[i] <= 0) {
throw new Error("Точки мають бути унікальними та відсортованими по X.");