<< Chapter < Page Chapter >> Page >

The output for the opposite side

When you open your html file in your browser, the output shown in Figure 3 should appear in your browser window.

Figure 4 . Output for script in Listing 3.
opposite = 3.999994640742543 hypotenuse = 5

Computing length of opposite side with the Google calculator

We could also compute the length of the opposite side using the Google calculator.

The length of the opposite side -- sample computation

Enter the following into the Google search box:

5*sin(53.1301024 degrees)

The following will appear immediately below the search box:

5 * sin(53.1301024 degrees) = 4

This is the length of the opposite side for the given angle and the given length of the hypotenuse.

Interesting equations

We learned earlier that the sine of the angle is equal to the ratio of the opposite side and the hypotenuse. We also learned that the angle is thearcsine of that ratio.

If we know any two of those values ( angle , opp , hyp ), we can find the third (with a little algebraic manipulation) as shown in Figure 5 .

Figure 5 . Interesting sine equations.
sine(angle) = opp/hyp angle = arcsine(opp/hyp)opp = hyp * sine(angle) hyp = opp/sine(angle)

Getting back to Listing 3

After defining the radian/degree conversion functions, Listing 3 declares and initializes variables representing the length of the hypotenuse and theangle in degrees. (Note that the angle in degrees was truncated to four significant digits, which may introduce a slight inaccuracy into thecomputations.)

Get and use the sine of the angle

That angle is converted to radians and passed as a parameter to the Math.sin method, which returns the value of the sine of the angle.

The value for the sine of the angle is then used in an algebraic equation to compute the length of the opposite side, which is displayed in Figure 4 . (This equation is one of the equations shown in Figure 5 .)

Looks very close to me

As you can see, the computed value for the opposite side shown in Figure 4 is extremely close to the known value of 4 units.

Re-compute the length of the hypotenuse

After that, the value of the hypotenuse is re-computed (as though it were the unknown in the problem) using the value of the sine and the recently computedvalue of the opposite side. (Once again, one of the equations from Figure 5 is used to perform the computation.) The output length for the hypotenuse is shown in Figure 4 , and it matches the known value.

Example usage of Math.asin and Math.sin methods

Listing 2 and Listing 3 provide examples of how to use the JavaScript Math.asin and Math.sin methods to find the angle, the opposite side, or the hypotenuse of a right triangle when the other two areknown as shown by the equations in Figure 5 .

The cosine and arccosine of an angle

You are going to find the discussion in this section to be very similar to the discussion in the previous section on the sine and the arcsine of an angle.

Once again, although the cosine of an angle is based on very specific geometric considerations involving circles (see (External Link) ), for our purposes, the cosine of an angle is simply a ratio between the lengths of two different sides of a righttriangle.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask