<< Chapter < Page Chapter >> Page >
Listing 2 . Arcsin of 3-4-5 triangle.
<!-- File JavaScript02.html --><html><body><script language="JavaScript1.3">function toRadians(degrees){ return degrees*Math.PI/180}//end function toRadians //============================================//function toDegrees(radians){ return radians*180/Math.PI}//end function toDegrees //============================================//var opp = 4 var hyp = 5var ratio = opp/hyp var angRad = Math.asin(ratio)var angDeg = toDegrees(angRad) document.write("radians = " + angRad + "<br/>") document.write("degrees = " + angDeg)</script></body></html>

The output for the angle

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

Figure 3 . Output for script in Listing 2.
radians = 0.9272952180016123 degrees = 53.13010235415598

Did you measure the angle to be 53 degrees with your protractor. If so, congratulations. If not, you should probably take another look at it.

Define conversion functions

The code in Listing 2 begins by defining the functions named toRadians and toDegrees that we developed earlier in Listing 1 . (In this case, we will only need the function named toDegrees so I could have omitted the code for the function named toRadians .)

Declare and initialize variables

Then the code in Listing 2 declares and initializes variables to represent the lengths of the opposite side and thehypotenuse for the triangle ( opp and hyp ). Then it computes and saves the ratio of the two. (We learned earlier thatthe ratio is the value of the sine of the angle at the origin even though we don't know the value of the angle.)

The built-in Math.asin method

JavaScript has a built-in method named Math.asin that receives the sine value for an unknown angle and returns the value of thecorresponding angle in radians. (The Math.asin method has the same purpose at the word arcsin in the Google calculator.)

The returned value is an angle between -PI/2 and PI/2 radians. (I will have more to say about this later.)

Listing 2 calls the Math.asin method, passing the ratio (sine of the angle) as a parameter, and stores the returned value in a variablenamed angRad .

Then Listing 2 calls the toDegrees method, passing the value of angRad as a parameter and stores the returned value in a variable named angDeg .

Finally, Listing 2 calls the document.write method twice in success to display the angle values shown in Figure 3 .

Another exercise with a different viewpoint

Now let's approach things from a different viewpoint. Assume that

  • you know the value of the angle in degrees,
  • you know the length of the hypotenuse, and
  • you need to find the length of the opposite side.

Assume also that for some reason you can't simply measure the length of the opposite side. Therefore, you must calculate it. This is a common situation inphysics, so let's see if we can write a script that will perform that calculation for us.

Create an html file containing the code shown in Listing 3 and open the file in your browser.

Listing 3 . Finding length of the opposite side.
<!-- File JavaScript03.html --><html><body><script language="JavaScript1.3">function toRadians(degrees){ return degrees*Math.PI/180}//end function toRadians //============================================//function toDegrees(radians){ return radians*180/Math.PI}//end function toDegrees //============================================//var hyp = 5 var angDeg = 53.13var angRad = toRadians(angDeg) var sine = Math.sin(angRad)var opp = hyp * sine document.write("opposite = " + opp + "<br/>") hyp = opp/sinedocument.write("hypotenuse = " + hyp + "<br/>")</script></body></html>

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