Recipe1.14.Finding the Length of Any Three Sides of a Right Triangle


Recipe 1.14. Finding the Length of Any Three Sides of a Right Triangle

Problem

You need to calculate the length of one side of a triangle when either the lengths of two sides are known or one angle and the length of a side are known.

Solution

Use the Math.Sin, Math.Cos, and Math.Tan methods of the Math class to find the length of one side. The equations for these methods are as follows:

 double theta = 40; double hypotenuse = 5; double oppositeSide = 0; double adjacentSide = 0; oppositeSide = Math.Sin(theta) * hypotenuse; oppositeSide = Math.Tan(theta) * adjacentSide; adjacentSide = Math.Cos(theta) * hypotenuse; adjacentSide = oppositeSide / Math.Tan(theta); hypotenuse = oppositeSide / Math.Sin(theta); hypotenuse = adjacentSide / Math.Cos(theta); 

where theta (Q) is the known angle, the oppositeSide variable is equal to the length of the side opposite to the angle theta, and the adjacentSide variable is equal to the length of the side adjacent to the angle theta. The hypotenuse variable is equal to the length of the hypotenuse of the triangle. See Figure 1-1.

Figure 1-1. A right triangle


In addition to these three static methods, the length of the hypotenuse of a right triangle can be calculated using the Pythagorean theorem. This theorem states that the hypotenuse of a right triangle is equal to the square root of the sum of the squares of the other two sides. This equation can be realized using the Math.Sqrt static method, as follows:

 double hypotenuse = Math.Sqrt((xSide * xSide) + (ySide * ySide));  

where xSide and ySide are the lengths of the two sides that are not the hypotenuse of the triangle.

Discussion

Finding the length of a side of a right triangle is easy when an angle and the length of one of the sides are known. Using the trigonometric functions sine, cosine, and tangent, you can derive the lengths of either of the two unknown sides. The equations for sine, cosine, and tangent are defined here:

 sin(Theta) = oppositeSide / hypotenuseSide cos(Theta) = adjacentSide / hypotenuseSide tan(Theta) = oppositeSide / adjacentSide 

where theta is the value of the known angle. Rearranging these equations allows you to derive the following equations:

 oppositeSide = sin(theta) * hypotenuse; oppositeSide = tan(theta) * adjacentSide; adjacentSide = cos(theta) * hypotenuse; adjacentSide = oppositeSide / tan(theta); hypotenuse = oppositeSide / sin(theta); hypotenuse = adjacentSide / cos(theta); 

These equations give you two methods to find the length of each side of the triangle.

When none of the angles is known but the lengths of two of the sides are known, use the Pythagorean theorem to determine the length of the hypotenuse. This theorem is defined as follows:

 Math.Sqrt(hypotenuse * hypotenuse) = Math.Sqrt((xSide * xSide) +                                                (ySide * ySide)) 

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.



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