Recipe1.15.Finding the Angles of a Right Triangle


Recipe 1.15. Finding the Angles of a Right Triangle

Problem

You need to calculate an angle of a triangle when the lengths of two sides are known.

Solution

Use the Math.Atan, Math.Acos, or Math.Asin static methods of the Math class. The following code calculates the angle theta and returns the value in radians:

 double theta = Math.Atan(OppositeSide / AdjacentSide); theta = Math.Acos(AdjacentSide / Hypotenuse); theta = Math.Asin(OppositeSide / Hypotenuse); 

To get the angle in degrees, use the following code:

 double theta = Math.Atan(oppositeSide / adjacentSide) * (180 / Math.PI); theta = Math.Acos(adjacentSide / hypotenuse) * (180 / Math.PI); theta = Math.Asin(oppositeSide / hypotenuse) * (180 / Math.PI); 

where theta is the known angle value, oppositeSide is equal to the length of the side opposite to the angle, and adjacentSide is equal to the length of the side adjacent to the angle. The hypotenuse is the length of the hypotenuse of the triangle. See Figure 1-1 in Recipe 1.14 for a graphical representation of these sides of a right triangle and Recipes 1.2 and 1.3 for converting between degrees and radians.

Discussion

In some cases, you need to determine an angle of a right triangle when only the lengths of two sides are known. The three trigonometric functions arcsine, arccosine, and arctangent allow you to find any angle of a right triangle, given this information. The static methods Math.Atan, Math.Acos, and Math.Asin on the Math class provide the functionality to implement these trigonometric operations.

See Also

See Recipe 1.14; see the "Math Class" topic in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net