Recipe 5.12 Converting Angle Measurements

5.12.1 Problem

You want to work with angle values in ActionScript, but you must convert to the proper units.

5.12.3 Discussion

The _rotation property of a movie clip object is measured in degrees. Every other angle measurement in ActionScript, however, uses radians, not degrees. This can be a problem in two ways. First of all, if you want to set the _rotation property based on the output of one of ActionScript's trigonometric methods, you must convert the value from radians to degrees. Second, humans generally prefer to work in degrees, which we must convert to radians before feeding to any of the trigonometric methods. Fortunately, the conversion between radians and degrees is simple. You should add the following degToRad( ) and radToDeg( ) methods to your Math.as file for converting from degrees to radians and vice versa. Note that they are attached directly to the top-level Math object, making them available throughout your movie.

```// Convert degrees to radians by multiplying by p and dividing by 180.
return (Math.PI * deg) / 180;
};

// Convert radians to degrees by multiplying by 180 and dividing by p.
return (rad * 180) / Math.PI;
};```

This following code demonstrates how the methods work:

```trace(Math.degToRad(90));         // Displays: 1.5707963267949 (which is p/2)

These two methods are invaluable when you want to use the trigonometric methods:

```// Use degToRad(  ) to convert degrees to radians before passing the value to
// Math.cos(  ) (which expects radians).

// Get the angle (in radians) for a cosine of .75 using the inverse cosine.
angle = Math.acos(.75);
// Set MovieClip._rotation to the degree equivalent of the angle in radians.

Recipe 5.15 Preface Part I: Local Recipes Chapter 1. ActionScript Basics Chapter 2. Runtime Environment Chapter 3. Color Chapter 4. Drawing and Masking Chapter 5. Numbers and Math Introduction Recipe 5.1 Representing Numbers in Different Bases Recipe 5.2 Converting Between Different Number Systems Recipe 5.3 Rounding Numbers Recipe 5.4 Inserting Leading or Trailing Zeros Recipe 5.5 Formatting Numbers for Display Recipe 5.6 Formatting Currency Amounts Recipe 5.7 Generating a Random Number Recipe 5.8 Simulating a Coin Flip Recipe 5.9 Simulating Dice Recipe 5.10 Simulating Playing Cards Recipe 5.11 Generating a Unique Number Recipe 5.12 Converting Angle Measurements Recipe 5.13 Calculating the Distance Between Two Points Recipe 5.14 Determining Points Along a Circle Recipe 5.15 Converting Between Units of Measurement Recipe 5.16 Calculating Asset Appreciation (Future Value) Recipe 5.17 Calculating Retirement Savings Recipe 5.18 Calculating the Loan (Mortgage) You Can Afford Recipe 5.19 Calculating Loan Amortization or Annuities Chapter 6. Arrays Chapter 7. Movie Clips Chapter 8. Text Chapter 9. Strings Chapter 10. Dates and Times Chapter 11. Forms Chapter 12. Objects and Custom Components Chapter 13. Programming Sound Part II: Remote Recipes Part III: Applications Appendix A. Unicode Escape Sequences for Latin 1 Characters Colophon
{"ft_lang_label":"__label__en","ft_lang_prob":0.8034508,"math_prob":0.929699,"size":1877,"snap":"2019-26-2019-30","text_gpt3_token_len":434,"char_repetition_ratio":0.14148425,"word_repetition_ratio":0.0069444445,"special_character_ratio":0.24933404,"punctuation_ratio":0.16056338,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98746127,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-06-27T09:16:53Z\",\"WARC-Record-ID\":\"<urn:uuid:7e4361e7-feb3-4e0b-8d93-d86dd7d347f9>\",\"Content-Length\":\"197065\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:380c900b-9490-4ccc-bec1-2b1501712889>\",\"WARC-Concurrent-To\":\"<urn:uuid:e2497c16-4e1e-44b2-a716-938ea0a188b0>\",\"WARC-IP-Address\":\"95.47.114.200\",\"WARC-Target-URI\":\"http://etutorials.org/Programming/actionscript/Part+I+Local+Recipes/Chapter+5.+Numbers+and+Math/Recipe+5.12+Converting+Angle+Measurements/\",\"WARC-Payload-Digest\":\"sha1:VXIW52GA5FFGUCRWZVAOIRGT7WVFDDUY\",\"WARC-Block-Digest\":\"sha1:UQ47Q53OBYNCF2RXGUERFN7U6EOQBYOS\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-26/CC-MAIN-2019-26_segments_1560628001014.85_warc_CC-MAIN-20190627075525-20190627101525-00266.warc.gz\"}"}