// Muuttujat kulmille
let α, β, γ; // α = kulma A, β = kulma B, γ = hypotenuusan kulma (90°)
const calculateAngles = (a, b, c) => {
α = Math.asin(a / c) * (180 / Math.PI); // Kulma A
β = Math.asin(b / c) * (180 / Math.PI); // Kulma B
γ = 90; // hypotenuusan kulma on 90°
};
let α, β, γ; // α = kulma A, β = kulma B, γ = hypotenuusan kulma (90°)
const calculateAngles = (a, b, c) => {
α = Math.asin(a / c) * (180 / Math.PI); // Kulma A
β = Math.asin(b / c) * (180 / Math.PI); // Kulma B
γ = 90; // hypotenuusan kulma on 90°
};
Vastaukset 6
Vanhimmat
Pystyykö muuttujan nimi olemaan ASCII:n ulkopuolelta?
Näköjään. Aika veemäistä kyllä naputella tuollaisia tavallisella näppiksellä. Kosketusnäytöllä hieman helpompaa.
Koodi on javaa, kun sen pitäisi olla vbs:ässää. Kiva varmaan etsiä noita kärpäsen paskoja (}) isommasta koodista.
Dim angles
angles = calculateAngles(Empty , 0.5, 1)
MsgBox("kulma A: " & Fix(angles(0)) & vbNewLine & "kulma B: " & Fix(angles(1)) & vbNewLine & "kulma C: " & angles(2))
angles = calculateAngles(Sqr(0.75), Empty, 1)
MsgBox("kulma A: " & Fix(angles(0)) & vbNewLine & "kulma B: " & Fix(angles(1)) & vbNewLine & "kulma C: " & angles(2))
angles = calculateAngles(Sqr(0.75), 0.5, Empty)
MsgBox("kulma A: " & Fix(angles(0)) & vbNewLine & "kulma B: " & Fix(angles(1)) & vbNewLine & "kulma C: " & angles(2))
Function calculateAngles(side_adjacent, side_opposite, side_hypotenuse)
' Muuttujat kulmille
Dim angle_A, angle_B, angle_C ' angle_A = kulma A, angle_B = kulma B, angle_C = hypotenuusan kulma (90°)
Dim ratio_opposite_hypotenuse, ratio_adjacent_hypotenuse, ratio_opposite_adjacent
Dim radians_to_degrees
radians_to_degrees = 180 / (4 * Atn(1))
If Not(IsEmpty(side_opposite)) And Not(IsEmpty(side_hypotenuse)) Then
ratio_opposite_hypotenuse = side_opposite / side_hypotenuse
angle_A = (Atn(ratio_opposite_hypotenuse / Sqr((-ratio_opposite_hypotenuse * ratio_opposite_hypotenuse) + 1))) * radians_to_degrees
angle_B = 90 - angle_A
ElseIf Not(IsEmpty(side_adjacent)) And Not(IsEmpty(side_hypotenuse)) Then
ratio_adjacent_hypotenuse = side_adjacent / side_hypotenuse
angle_A = (Atn(-ratio_adjacent_hypotenuse / Sqr((-ratio_adjacent_hypotenuse * ratio_adjacent_hypotenuse) + 1)) + 2 * Atn(1)) * radians_to_degrees
angle_B = 90 - angle_A
ElseIf Not(IsEmpty(side_opposite)) And Not(IsEmpty(side_adjacent)) Then
ratio_opposite_adjacent = side_opposite / side_adjacent
angle_A = Atn(ratio_opposite_adjacent)* radians_to_degrees
angle_B = 90 - angle_A
End If
angle_C = 90
calculateAngles = Array(angle_A, angle_B, angle_C)
End Function
Mitä tulee aloitukseen.
Rivin
β = Math.ASIN(b / c) * (180 / Math.PI); // Kulma B
Pitäisi olla
β = Math.ACOS(b / c) * (180 / Math.PI); // Kulma B
Suurin virhe on globaalien muuttujien käyttö, funktio ei palauta mitään. Myös ei tarkisteta, ovatko funktion argumentit järkeviä, positiivisia lukuja ja onko kyseessä suorakulmainen kolmio. Tässä parannettu versio:
const calculateAngles = (a, b, c) => {
if (a <= 0 || b <= 0 || c <= 0) {
throw new Error("Sivujen täytyy olla positiivisia.");
}
// Tarkistus: onko suorakulmainen kolmio?
if (Math.abs(a*a + b*b - c*c) > 1e-6) {
console.warn("Varoitus: a² + b² ≠ c², kolmio ei ole tarkasti suorakulmainen.");
}
const A = Math.asin(a / c) * (180 / Math.PI);
const B = Math.asin(b / c) * (180 / Math.PI);
const C = 90; // suorakulma
return { A, B, C };
};