简体   繁体   中英

Calculating the angle of an orthogonal triangle using the Arcsin in C#

I have the following code:

double x = sw.bonePos[0, (int)Bones.HipCenter].x;
double z = sw.bonePos[0, (int)Bones.HipCenter].z;
double hypotenusePower2 = Math.Pow(x, 2) + Math.Pow(z, 2);

double hypotenuse = Math.Sqrt(hypotenusePower2);

double angle = Math.Asin(z / hypotenuse);

I know that x,z, hypotenuse are correct and z / hypotenuse is correct because its always between -1 and 1. So I want to find the angle using the ArcSin like this but when I am printing for example Math.Asin(1) the result is 1.5707... Am I using the wrong function? Is there any function in C# that returns the angle?

Example of input/output:

x: -0.000844396417960525 
z: 0.857428431510925 
hypotenuse: 0.857428847292063 
angle: 1.5698115260652



x: 0.0198930986225605 
z: 0.849016189575195 
hypotenus: 0.849249212854266 
angle: 1.54736984845028

The result you get is correct - asin of 1 is half of π, or approximately 1.5707 radians.

Functions returning angles usually return the results in radians. If you need the result in degrees, you need to convert the result as follows:

double degrees = angle * ( 180 / Math.Pi );

That's the right answer. The resulting angle is measured in radians. Math.Asin(1) should therefore be equal to π/2 ≈ 1.5707 radians, which matches your result.

If you wanted the value in degrees, multiply by 180/π. In this case, π/2 * 180/π would give you 90 degrees:

double degrees = radians * (180 / Math.Pi);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM