<< Chapter < Page | Chapter >> Page > |
An exercise involving degrees and radians
Let's do a short exercise involving degrees and radians. Please create an html file containing the code shown in Listing 1 and open it in your browser.
Listing 1 . Conversions between radians and degrees. |
---|
<!-- File JavaScript01.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 degrees = 90
var radians = toRadians(degrees)document.write("degrees = " + degrees +
" : radians = " + radians + "<br/>")
radians = 1degrees = toDegrees(radians)
document.write("radians = " + radians +" : degrees = " + degrees + "<br/>")
radians = Math.PIdegrees = toDegrees(radians)
document.write("radians = " + radians +" : degrees = " + degrees)</script></body></html> |
Output for script in Listing 1
When you open the file in your browser, the text shown in Figure 1 should be displayed in the browser window.
Figure 1 . Output for script in Listing 1. |
---|
degrees = 90 : radians = 1.5707963267948965
radians = 1 : degrees = 57.29577951308232radians = 3.141592653589793 : degrees = 180 |
The toRadians and toDegrees functions
Because it will frequently be necessary for us to convert between degrees and radians, I decided to write two functions that we will use to make those conversions. Thatwill eliminate the need for us to stop and think about the conversion (and possibly get it backwards) when writingcode. We will simply call the function that performs the conversion in the required direction.
The toRadians function expects to receive an input parameter describing an angle in degreesand returns the value for that same angle in radians.
The toDegrees function expects to receive an input parameter describing an angle in radiansand returns the value for that same angle in degrees.
Global variables named degrees and radians
The code in Listing 1 begins by declaring global variables named degrees and radians . The variable named degrees is initialized to 90 degrees.
The toRadians function is called to convert that value of degrees to radians. The returned value in radians is stored in the variable named radians .
Display contents of both variables
Then the document.write method is called to display the valuescontained in both variables, producing the first line of output text shown in Figure 1 .
Modify variable contents, convert, and display again
Following that, a value of 1 is assigned to the variable named radians . The toDegrees function is called to convert that value to degrees, and the result is stored in the variable named degrees .
Once again, the document.write method is called to display the current contents of both variables, producing the second line of output textshown in Figure 1 .
One more time
Finally, the mathematical constant, PI, is stored in the variable named radians . Then that value is converted to degrees and stored in the variable named degrees . The current values in both variables are displayed, producing the last line of output text shown in Figure 1 .
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?