-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrojuhelnik.js
More file actions
501 lines (428 loc) · 17.7 KB
/
Copy pathtrojuhelnik.js
File metadata and controls
501 lines (428 loc) · 17.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
let bylVypocitan = false;
let zobrazeniMod = 'kuzel';
function vypocitat() {
const a = Number(document.getElementById('stranaA').value);
const b = Number(document.getElementById('stranaB').value);
const c = Number(document.getElementById('stranaC').value);
const alfa = Number(document.getElementById('uhelAlfa').value);
const beta = Number(document.getElementById('uhelBeta').value);
let delkyInfo = document.getElementById('delkyInfo');
let uhlyInfo = document.getElementById('uhlyInfo');
let chybovaHlaska = document.getElementById('chybovaHlaska');
// Kontrola správnosti zadání
if (zobrazeniMod !== 'kuzel') {
let pocetUhlu = [alfa, beta].filter(x => x > 0).length;
if (pocetUhlu > 1) {
chybovaHlaska.textContent = "Nelze zadat dva úhly - jeden úhel je vždy 90°";
return;
}
}
// Reset chybové hlášky
chybovaHlaska.textContent = '';
// Synchronizace hodnot pro kužel
if (zobrazeniMod === 'kuzel') {
const kuzelVyska = Number(document.getElementById('kuzelVyska').value);
const kuzelUhel = Number(document.getElementById('kuzelUhel').value);
if (kuzelVyska) document.getElementById('stranaA').value = kuzelVyska;
if (kuzelUhel) document.getElementById('uhelBeta').value = kuzelUhel;
}
// 1. Známe dvě odvěsny (a,b)
if (a && b && !c && !alfa && !beta) {
const vypC = Math.sqrt(a*a + b*b);
const vypAlfa = Math.asin(a/vypC) * 180/Math.PI;
const vypBeta = Math.asin(b/vypC) * 180/Math.PI;
aktualizujVysledky({c: vypC, alfa: vypAlfa, beta: vypBeta});
delkyInfo.textContent = "Vypočteno z odvěsen a,b";
}
// 2. Známe přeponu a jednu odvěsnu (c,a)
else if (a && !b && c && !alfa && !beta) {
if (c <= a) {
alert("Přepona musí být delší než odvěsna!");
return;
}
const vypB = Math.sqrt(c*c - a*a);
const vypAlfa = Math.asin(a/c) * 180/Math.PI;
const vypBeta = 90 - vypAlfa;
aktualizujVysledky({b: vypB, alfa: vypAlfa, beta: vypBeta});
delkyInfo.textContent = "Vypočteno z c,a";
}
// 3. Známe odvěsnu a přilehlý úhel (a,beta)
else if (a && !b && !c && !alfa && beta) {
if (beta >= 90) {
alert("Úhel β musí být menší než 90°!");
return;
}
const vypB = a * Math.tan(beta * Math.PI/180);
const vypC = a / Math.cos(beta * Math.PI/180);
const vypAlfa = 90 - beta;
aktualizujVysledky({b: vypB, c: vypC, alfa: vypAlfa});
uhlyInfo.textContent = "Vypočteno z a,β";
}
// 4. Známe odvěsnu a protilehlý úhel (a,alfa)
else if (a && !b && !c && alfa && !beta) {
if (alfa >= 90) {
alert("Úhel α musí být menší než 90°!");
return;
}
const vypC = a / Math.sin(alfa * Math.PI/180);
const vypB = Math.sqrt(vypC*vypC - a*a);
const vypBeta = 90 - alfa;
aktualizujVysledky({b: vypB, c: vypC, beta: vypBeta});
uhlyInfo.textContent = "Vypočteno z a,α";
}
// 5. Známe přeponu a úhel (c,alfa)
else if (!a && !b && c && alfa && !beta) {
if (alfa >= 90) {
alert("Úhel α musí být menší než 90°!");
return;
}
const vypA = c * Math.sin(alfa * Math.PI/180);
const vypB = c * Math.cos(alfa * Math.PI/180);
const vypBeta = 90 - alfa;
aktualizujVysledky({a: vypA, b: vypB, beta: vypBeta});
delkyInfo.textContent = "Vypočteno z c,α";
}
else {
if (zobrazeniMod === 'kuzel') {
// Pro kužel nehlásíme chybu, jen překreslíme
bylVypocitan = true;
prekresliTrojuhelnik();
return;
}
// Pro trojúhelník - kontrola vstupů
let pocetZadanych = [a, b, c, alfa, beta].filter(x => x > 0).length;
if (pocetZadanych !== 2) {
chybovaHlaska.textContent = "Pro výpočet je potřeba zadat právě dva údaje";
return;
}
alert("Zadejte právě dva údaje pro výpočet!");
return;
}
bylVypocitan = true;
prekresliTrojuhelnik();
}
function aktualizujVysledky({a, b, c, alfa, beta} = {}) {
if (a) document.getElementById('stranaA').value = a.toFixed(2);
if (b) document.getElementById('stranaB').value = b.toFixed(2);
if (c) document.getElementById('stranaC').value = c.toFixed(2);
if (alfa) document.getElementById('uhelAlfa').value = alfa.toFixed(2);
if (beta) document.getElementById('uhelBeta').value = beta.toFixed(2);
if (zobrazeniMod === 'kuzel') {
// Aktualizace propojených polí kužele
document.getElementById('kuzelVyska').value = document.getElementById('stranaA').value;
document.getElementById('kuzelUhel').value = document.getElementById('uhelBeta').value;
// Výpočet spodního průměru D2
const d1 = Number(document.getElementById('prumer').value) || 0;
const stranaB = Number(document.getElementById('stranaB').value) || 0;
const d2 = d1 + (2 * stranaB);
document.getElementById('spodniPrumer').value = d2.toFixed(2);
// Pokud není zadán průměr, použijeme vypočtenou stranu b
if (!d1 && stranaB) {
document.getElementById('prumer').value = stranaB;
}
}
}
function resetovat() {
['stranaA','stranaB','stranaC','uhelAlfa','uhelBeta','prumer'].forEach(id => {
const element = document.getElementById(id);
if (element) {
element.value = '';
element.style.backgroundColor = '';
}
});
document.getElementById('delkyInfo').textContent = '';
document.getElementById('uhlyInfo').textContent = '';
document.getElementById('chybovaHlaska').textContent = '';
prekresliTrojuhelnik();
['stranaA', 'stranaB', 'stranaC', 'uhelAlfa', 'uhelBeta'].forEach(id => {
document.getElementById(id).style.backgroundColor = '';
});
bylVypocitan = false;
prekresliTrojuhelnik();
}
function prepniZobrazeni(mod) {
zobrazeniMod = mod;
document.getElementById('kuzelParams').style.display = mod === 'kuzel' ? 'block' : 'none';
// Zachováme všechny hodnoty a překreslíme
prekresliTrojuhelnik();
}
function prekresliTrojuhelnik() {
const canvas = document.getElementById('platno');
// Přizpůsobení velikosti canvasu pro mobilní zařízení
const containerWidth = canvas.parentElement.clientWidth - 20;
canvas.width = containerWidth;
canvas.height = Math.min(containerWidth * 0.75, window.innerHeight * 0.4);
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
const a = Number(document.getElementById('stranaA').value);
const b = Number(document.getElementById('stranaB').value);
if (!bylVypocitan) {
if (zobrazeniMod === 'kuzel') {
nakresliVzorovyKuzel(ctx, canvas.width, canvas.height);
} else {
nakresliVzorovyTrojuhelnik(ctx, canvas.width, canvas.height);
}
return;
}
if (!a || !b) return;
if (zobrazeniMod === 'kuzel') {
const prumer = Number(document.getElementById('prumer').value);
if (prumer) {
nakresliKuzel(ctx, canvas.width, canvas.height);
} else {
// Pokud není zadán průměr, použijeme hodnotu strany b
document.getElementById('prumer').value = b;
nakresliKuzel(ctx, canvas.width, canvas.height);
}
} else {
// Výpočet měřítka pro přizpůsobení trojúhelníku
const margin = 50;
const scaleX = (canvas.width - 2 * margin) / b;
const scaleY = (canvas.height - 2 * margin) / a;
const scale = Math.min(scaleX, scaleY);
// Centrování trojúhelníku
const startX = (canvas.width - b * scale) / 2;
const startY = canvas.height - margin;
// Vykreslení trojúhelníku
ctx.beginPath();
ctx.moveTo(startX, startY);
ctx.lineTo(startX + b * scale, startY);
ctx.lineTo(startX, startY - a * scale);
ctx.closePath();
ctx.fillStyle = '#e0e0ff';
ctx.fill();
ctx.stroke();
// Vykreslení pravého úhlu
const velikostCtverecku = 20 * scale;
ctx.beginPath();
ctx.moveTo(startX + velikostCtverecku, startY);
ctx.lineTo(startX + velikostCtverecku, startY - velikostCtverecku);
ctx.lineTo(startX, startY - velikostCtverecku);
ctx.stroke();
// Výpočet hodnot
const c = Math.sqrt(a*a + b*b);
const alfa = Math.asin(a/c) * 180/Math.PI;
const beta = Math.asin(b/c) * 180/Math.PI;
// Vykreslení popisků s přizpůsobenou velikostí písma
const fontSize = Math.max(12, Math.min(16, scale * 14));
ctx.font = `${fontSize}px Arial`;
ctx.fillStyle = 'black';
// Popisky stran
ctx.fillText(`a = ${a}`, startX - 40, startY - a * scale/2);
ctx.fillText(`b = ${b}`, startX + b * scale/2, startY + 25);
ctx.fillText(`c = ${c.toFixed(1)}`, startX + b * scale/3, startY - a * scale/3);
// Popisky úhlů
ctx.fillText(`α = ${alfa.toFixed(1)}°`, startX + b * scale - 40, startY - 10);
ctx.fillText(`β = ${beta.toFixed(1)}°`, startX + 10, startY - a * scale + 20);
ctx.fillText(`90°`, startX + 25, startY - 25);
}
}
function nakresliKuzel(ctx, width, height) {
const a = Number(document.getElementById('stranaA').value);
const b = Number(document.getElementById('stranaB').value);
const prumer = Number(document.getElementById('prumer').value) || b;
if (!a || !b) return;
const margin = 50;
const scale = Math.min((width - 2 * margin) / (prumer + b), (height - 2 * margin) / a);
// Centrování
const startX = width / 2;
const startY = height - margin;
// Vykreslení obdélníku (rozvinutá část)
ctx.beginPath();
ctx.moveTo(startX - prumer/2 * scale, startY);
ctx.lineTo(startX + prumer/2 * scale, startY);
ctx.lineTo(startX + prumer/2 * scale, startY - a * scale);
ctx.lineTo(startX - prumer/2 * scale, startY - a * scale);
ctx.closePath();
ctx.fillStyle = '#e0e0ff';
ctx.fill();
ctx.stroke();
// Vykreslení trojúhelníků po stranách
// Levý trojúhelník
ctx.beginPath();
ctx.moveTo(startX - prumer/2 * scale, startY);
ctx.lineTo(startX - prumer/2 * scale - b * scale, startY);
ctx.lineTo(startX - prumer/2 * scale, startY - a * scale);
ctx.closePath();
ctx.fillStyle = '#d0d0ff';
ctx.fill();
ctx.stroke();
// Pravý trojúhelník
ctx.beginPath();
ctx.moveTo(startX + prumer/2 * scale, startY);
ctx.lineTo(startX + prumer/2 * scale + b * scale, startY);
ctx.lineTo(startX + prumer/2 * scale, startY - a * scale);
ctx.closePath();
ctx.fillStyle = '#d0d0ff';
ctx.fill();
ctx.stroke();
// Čárkovaná osa uprostřed
ctx.setLineDash([5, 5]);
ctx.beginPath();
ctx.moveTo(startX, startY);
ctx.lineTo(startX, startY - a * scale);
ctx.stroke();
ctx.setLineDash([]);
// Popisky
ctx.fillStyle = 'black';
const fontSize = Math.max(12, Math.min(16, scale * 14));
ctx.font = `${fontSize}px Arial`;
ctx.fillText(`a = ${a}`, startX - 20, startY - a * scale/2 - 10);
ctx.fillText(`b = ${b}`, startX + prumer/2 * scale + b * scale/2, startY + 20);
ctx.fillText(`D₁ = ${prumer}`, startX - prumer/4 * scale, startY - a * scale - 10);
ctx.fillText(`D₂ = ${(prumer + 2*b).toFixed(1)}`, startX - prumer/4 * scale, startY + 40);
}
function nakresliVzorovyTrojuhelnik(ctx, width, height) {
const a = 100;
const b = 150;
// Výpočet měřítka pro vzorový trojúhelník
const margin = 50;
const scaleX = (width - 2 * margin) / b;
const scaleY = (height - 2 * margin) / a;
const scale = Math.min(scaleX, scaleY) * 0.8;
const startX = (width - b * scale) / 2;
const startY = height - margin;
// Vykreslení trojúhelníku
ctx.beginPath();
ctx.moveTo(startX, startY);
ctx.lineTo(startX + b * scale, startY);
ctx.lineTo(startX, startY - a * scale);
ctx.closePath();
ctx.fillStyle = '#e0e0ff';
ctx.fill();
ctx.stroke();
// Popisky
ctx.fillStyle = 'black';
const fontSize = Math.max(14, scale * 12);
ctx.font = `${fontSize}px Arial`;
ctx.fillText('a', startX - 20, startY - a * scale/2);
ctx.fillText('b', startX + b * scale/2, startY + 25);
ctx.fillText('c', startX + b * scale/3, startY - a * scale/3);
ctx.fillText('α', startX + b * scale - 30, startY - 10);
ctx.fillText('β', startX + 10, startY - a * scale + 20);
ctx.fillText('90°', startX + 25, startY - 25);
}
function nakresliVzorovyKuzel(ctx, width, height) {
const a = 100; // výška
const b = 150; // strana trojúhelníku
const prumer = b; // výchozí průměr
const margin = 50;
const scale = Math.min((width - 2 * margin) / (prumer + b), (height - 2 * margin) / a) * 0.8;
// Centrování
const startX = width / 2;
const startY = height - margin;
// Obdélník (rozvinutá část)
ctx.beginPath();
ctx.moveTo(startX - prumer/2 * scale, startY);
ctx.lineTo(startX + prumer/2 * scale, startY);
ctx.lineTo(startX + prumer/2 * scale, startY - a * scale);
ctx.lineTo(startX - prumer/2 * scale, startY - a * scale);
ctx.closePath();
ctx.fillStyle = '#e0e0ff';
ctx.fill();
ctx.stroke();
// Trojúhelníky
[1, -1].forEach(side => {
ctx.beginPath();
ctx.moveTo(startX + side * prumer/2 * scale, startY);
ctx.lineTo(startX + side * (prumer/2 + b) * scale, startY);
ctx.lineTo(startX + side * prumer/2 * scale, startY - a * scale);
ctx.closePath();
ctx.fillStyle = '#d0d0ff';
ctx.fill();
ctx.stroke();
});
// Popisky
ctx.fillStyle = 'black';
ctx.font = '16px Arial';
ctx.fillText('a', startX - 20, startY - a * scale/2);
ctx.fillText('b', startX + prumer/2 * scale + b * scale/2, startY + 20);
ctx.fillText('D₁', startX - 10, startY - a * scale - 10);
ctx.fillText('D₂', startX - 10, startY + 40);
}
// Přidáme událost pro automatický výpočet při změně hodnot
function pridejEventListenery() {
['stranaA', 'stranaB', 'stranaC', 'uhelAlfa', 'uhelBeta', 'prumer', 'kuzelVyska', 'kuzelUhel'].forEach(id => {
const element = document.getElementById(id);
if (!element) return;
element.addEventListener('focus', function() {
// Automatické doplnění druhého úhlu
if (id === 'uhelAlfa' || id === 'uhelBeta') {
const alfa = Number(document.getElementById('uhelAlfa').value);
const beta = Number(document.getElementById('uhelBeta').value);
if (id === 'uhelAlfa' && beta && !alfa) {
document.getElementById('uhelAlfa').value = (90 - beta).toFixed(2);
}
if (id === 'uhelBeta' && alfa && !beta) {
document.getElementById('uhelBeta').value = (90 - alfa).toFixed(2);
}
}
if (getPocetVyplnenych() === 2) {
vypocitat();
}
});
// Synchronizace hodnot při změně
element.addEventListener('input', function() {
if (zobrazeniMod === 'kuzel') {
if (id === 'kuzelVyska') {
document.getElementById('stranaA').value = this.value;
}
if (id === 'kuzelUhel') {
document.getElementById('uhelBeta').value = this.value;
}
}
zvyrazniDalsiPolicka(id);
});
// Výpočet až při opuštění pole nebo stisknutí Enter
element.addEventListener('blur', function() {
if (getPocetVyplnenych() === 2) {
vypocitat();
}
});
element.addEventListener('keypress', function(e) {
if (e.key === 'Enter' && getPocetVyplnenych() === 2) {
vypocitat();
}
});
});
}
function getPocetVyplnenych() {
const hodnoty = {
a: Number(document.getElementById('stranaA').value),
b: Number(document.getElementById('stranaB').value),
c: Number(document.getElementById('stranaC').value),
alfa: Number(document.getElementById('uhelAlfa').value),
beta: Number(document.getElementById('uhelBeta').value)
};
// V režimu kužele počítáme i s průměrem
if (zobrazeniMod === 'kuzel') {
hodnoty.prumer = Number(document.getElementById('prumer').value);
delete hodnoty.c; // V režimu kužele nepoužíváme stranu c
}
return Object.values(hodnoty).filter(v => v > 0).length;
}
function zvyrazniDalsiPolicka(aktualniId) {
const vsechnaPolicka = ['stranaA', 'stranaB', 'stranaC', 'uhelAlfa', 'uhelBeta'];
const pocetVyplnenych = getPocetVyplnenych();
vsechnaPolicka.forEach(id => {
const element = document.getElementById(id);
if (!element.value && pocetVyplnenych === 1) {
element.style.backgroundColor = '#e8f4ff'; // Zvýraznění prázdných políček
} else {
element.style.backgroundColor = ''; // Návrat k původní barvě
}
});
}
// Upravíme inicializaci
window.onload = function() {
pridejEventListenery();
resetovat();
// Přidáme inicializaci zobrazení kužele
document.getElementById('kuzelParams').style.display = 'block';
};
// Přidáme posluchač pro změnu velikosti okna
window.addEventListener('resize', prekresliTrojuhelnik);
// Přidáme posluchač pro změnu orientace zařízení
window.addEventListener('orientationchange', function() {
setTimeout(prekresliTrojuhelnik, 100);
});