# 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;
oppositeSide = Math.Sin(theta) * hypotenuse;
hypotenuse = oppositeSide / Math.Sin(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
```

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

```oppositeSide = sin(theta) * hypotenuse;
hypotenuse = oppositeSide / sin(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 the "Math Class" topic in the MSDN documentation.

### 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.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.