• Aucun résultat trouvé

Converting Between Units of Measurement

Dans le document What's Not in This Book (Page 159-164)

Figure 4-3. Using trigonometry to determine a point along a circle's perimeter

Recipe 4.15. Converting Between Units of Measurement

Problem

You want to convert between Fahrenheit and Celsius, pounds and kilograms, or other units of measure.

Solution

Use the Unit and Converter classes.

Discussion

There are various systems of measurement used throughout the world. For example, temperature is commonly measured in both Fahrenheit and Celsius. Weight is sometimes measured in pounds, but quite frequently, a metric system that uses kilograms is employed instead. And distances are likely to be measured in miles instead of kilometers, or inches

instead of centimeters. For these reasons, you may need to convert from one unit of measure to another.

Technically, pounds are a measurement of weight, while kilograms are a measurement of mass. Weight is a force that changes as the acceleration due to gravitational changes, whereas mass is constant regardless of the effects of gravity. The effect is that an object's weight on the moon and Earth differ since there are different effects, thanks to gravity, but the mass of an object remains the same. However, on the surface of the Earth, mass and weight often are used interchangeably.

Each of these conversions has its own algorithm. For example, to convert from Centigrade to Fahrenheit, you should multiply by 9, divide by 5, and then add 32 (to convert from Fahrenheit to Centigrade, subtract 32, multiply by 5, and then divide by 9). Likewise, you can multiply by 2.2 to convert pounds to kilograms, and you can divide by 2.2 to convert kilograms to pounds.

(We also saw in Recipe 4.12 how to convert angles from degrees to radians and vice versa.) As with converting between different units of measure with angles, you can use the Unit and Converter classes to convert between other types of units. The Unit class has support for quite a range of categories of measurement (angles, temperature, volume, etc.). You can retrieve an array of supported categories using the static Unit.getCategories( ) method:

// Display the categories that are supported.

trace(Unit.getCategories( ));

For each category, there are a variety units are supported. For example, in the angle category

degrees, radians, and gradians are supported. You can retrieve a list of supported units for a given category using the static Unit.getUnits( ) method. Pass the method a category name to retrieve an array of units supported for that group. If you omit the parameter, then the entire list of supported units is returned:

// Display the units supported in the temperature category.

trace(Unit.getUnits("temperature"));

You can use the built-in Unit constants for any supported unit of measurement, just as in Recipe 4.12. Then you can retrieve a Converter object by using the getConverterTo( ) or

getConverterFrom( ) methods. The following example creates a Converter to calculate the Fahrenheit equivalents of Celcius measurements:

var converter:Converter = Unit.CELCIUS.getConverterTo(Unit.FAHRENHEIT);

Then, of course, you can use the convert( ) (and/or the convertWithLabel( )) method to convert values:

trace(converter.convert(0)); // Displays: 32

See Also

For an example of using a hardcoded function to perform a single type of unit conversion, refer to Recipe 4.12, which includes a function to convert angles from degrees to radians and another function that does the opposite.

Chapter 5. Arrays

Section 5.0. Introduction

Recipe 5.1. Adding Elements to the Start or End of an Array Recipe 5.2. Looping Through an Array

Recipe 5.3. Searching for Matching Elements in an Array Recipe 5.4. Removing Elements

Recipe 5.5. Inserting Elements in the Middle of an Array Recipe 5.6. Converting a String to an Array

Recipe 5.7. Converting an Array to a String Recipe 5.8. Creating a Separate Copy of an Array Recipe 5.9. Storing Complex or Multidimensional Data Recipe 5.10. Sorting or Reversing an Array

Recipe 5.11. Implementing a Custom Sort

Recipe 5.12. Randomizing the Elements of an Array Recipe 5.13. Getting the Minimum or Maximum Element Recipe 5.14. Comparing Arrays

Recipe 5.15. Creating an Associative Array

Recipe 5.16. Reading Elements of an Associative Array

5.0. Introduction

Arrays are essential to successful ActionScript programming.

An array provides a way of grouping related data together, and then organizing and processing that data. The concept of an array should not be foreign to you. In fact, the concept is used all the time in everyday life. You can view a simple grocery list or to-do list as an array. Your address book is an array containing people's names, addresses, birthdates, and so on. Libraries keep track of books using an indexing system whereby each book becomes, conceptually, an element in a library's array.

In ActionScript, there are two kinds of arrays: integer-indexed and associative. Both array types group related data, but they use different means of accessing the data.

Integer-indexed array

Uses integers (numbers) as unique identifiers for each element in the array. Such arrays are ordered by index (i.e., element number), starting from 0. Each element occupies a numbered slot in the array. Integer-indexed arrays are ideal for sets of data that you want to work with in sequential order.

Associative array

Uses string keys to access each value. You can read more about associative arrays in Recipe 5.15.

Integer-indexed arrays are the focus of the majority of the recipes in this chapter, and unless otherwise specified, the term "array" refers to an integer-indexed array.

Of course, before you can use an array, you first need to know how to create one. There are two ways to construct a new array in ActionScript: with the constructor function or as an array literal. All arrays are members of the Array class. You can use the Array( ) constructor function to instantiate new array objects in one of three ways:

// Create an empty array.

var array:Array = new Array();

// Create an array with elements undefined elements.

var array:Array = new Array(elements);

// Create an array with specified elements.

var array:Array = new Array(element0,...elementN);

The array literal notation also creates a new array, but without using the constructor function.

Literal notation is convenient for specifying elements at the time of creation, such as:

var letters:Array = ["a", "b", "c"];

Some methods of the Array class modify the existing array on which the method is called, and others return a new array (offering an indirect way to create arrays).

You retrieve and set array elements using the array-access operator ( square brackets) and the index of the element you wish to get or set, for example:

// Set the fifth element of the array called items to "apples"

// (array indexes start at 0).

items[4] = "apples";

// Display the fifth element in the Output window.

trace(items[4]); // Displays: apples

ActionScript doesn't care what kind of values you store in an array. You can store strings, numbers, Booleans, or references to any kind of objects. And, unlike stricter programming languages, you can even store different datatypes in a single array. For example, this array stores a string, an integer, a Boolean, and an object:

var data:Array = ["a", 2, true, new Object()];

Unlike many languages, ActionScript doesn't force you to specify the number of elements in an array when it is declared.

Recipe 5.1. Adding Elements to the Start or End of an

Dans le document What's Not in This Book (Page 159-164)