The Pythagorean theorem

The final topic we're going to cover in this chapter is the Pythagorean theorem. The theorem deals once again with the measurement of triangles, and looks like this:

Let's take another look at our right triangle, shown in Figure 6-32.

An illustration of the theorem is shown in Figure 6-33. If you calculate the area of the squares on each leg of the triangle and add them together, you will get the area of the square on the hypotenuse.

Figure 6-32. A triangle with sides labeled

Plus Equals

Figure 6-33. An illustration of the Pythagorean theorem. The area of side a2 plus the area of side b2 is equal to the area of side c2.

Figure 6-32. A triangle with sides labeled

Figure 6-33. An illustration of the Pythagorean theorem. The area of side a2 plus the area of side b2 is equal to the area of side c2.

Figure 6-34. The Pythagorean theorem can be used to determine distances between objects.

I know at this point your vision may be getting blurry and your mind weary, and you may be wondering how a triangle and square roots will be of any value to you in Silverlight. However, the Pythagorean theorem can be used to determine distances between two points, which can be applied in many interesting ways. Take a look at Figure 6-34. Here we have two objects, each one with its own set of coordinates.

If this chapter has done its job, you should be seeing right triangles when you close your eyes. If you think you see a right triangle in Figure 6-34, you are correct. The distance line forms the hypotenuse of a right triangle. If we fill in the triangle's legs and some values for the position of the two objects, we get something like what is shown in Figure 6-35.

As you can see, once we know two points, we can determine the third, as well as the distances of the two legs of the triangle. Distance X and Distance Y in the figure are typically coded as DX and DY, or can be stored as Point data types, D.X and D.Y.

Triangles With Measures
Figure 6-35. The right triangle formed between two points, along with associated measurements

The calculation for distance, then, would look like the following:

This code determines the x and y distances, squares them, sums them, and calculates the square root. If we plug in the numbers from Figure 6-35, we get the following:

Distance = Math.Sqrt(50 *50+50* 50); Distance = Math.Sqrt(2500 + 2500); Distance = Math.Sqrt(5000); Distance = 70.7l[...]

+1 0

Average user rating: 5 stars out of 1 votes

Post a comment

  • Receive news updates via email from this site