Reputation: 2261
I calculate the angles of a triangle, and I don't understand why I get a negative angle for some acute angle. For example:
var sin = Math.Sin(4.45);
var radians = Math.Atan(sin);
var angle = radians * (180 / Math.PI);
it return sin = -0.965
and angle = -44
.
When scientific calculator show sin = 0.0775
My triangle has such lengths 6.22
, 6.07
and 1.4
then there isn't option to had negative angle.
Upvotes: 6
Views: 855
Reputation: 186668
Let's compute angles of the triangle with a help of Law of cosines:
a**2 + b**2 - 2 * a * b * cos(gamma) == c**2
so
gamma = acos((a * a + b * b - c * c) / (2 * a * b))
beta = acos((a * a + c * c - b * b) / (2 * a * c))
alpha = acos((c * c + b * b - a * a) / (2 * c * b))
now put triangle lengths
a = 6.22
b = 6.07
c = 1.40
into formulae above and you'll get angles (in radians, if you use c# Math.Acos
)
alpha = 1.5639 = 89.6 degrees
beta = 1.3506 = 77.4 degrees
gamma = 0.2270 = 13.0 degrees
------------------------------
180.0 degrees (let's check ourselves)
Another check is Law of sines
a / sin(alpha) == b / sin(beta) == c / sin(gamma) == 6.2201
Upvotes: 4
Reputation: 16701
Math.Sin
operates on radians. You need to convert degrees into radians.
To convert degrees to radians multiply the angle by 𝜋/180
:
var sin = Math.Sin(4.45*Math.PI/180);
// output 0.07758909147106598
And the rest of your code should remain the same.
Note: if you just want to convert an angle in degrees to angle in radians you can use the formula above:
var degrees = 4.45;
var radians = degrees * Math.PI/180;
Upvotes: 10