
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Finding the Angles of a Right Triangle
|
19
Simplifying this equation into a syntax usable by C#, you obtain the following code:
double hypotenuse = Math.Sqrt((xSide * xSide) +
(ySide * ySide));
where hypotenuse is equal to the length of the hypotenuse, and xSide and ySide are
the lengths of the other two sides.
See Also
See the “Math Class” topic in the MSDN documentation.
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,orMath.Asin static methods of the Math class. The fol-
lowing 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