Flash / Flex / ActionScript/Development/Math

Материал из Web эксперт
Версия от 08:15, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Содержание

All trig functions operate on radians, an angular unit in which radians measure a full revolution.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
         var valInRadians;
         var valInDegrees;
         valInRadians = valInDegrees / 180 * Math.PI;
         valInDegrees = valInRadians / Math.PI * 180;
    }
  }
}



atan2(), takes two parameters (an X- and a Y-coordinate), and returns the angle formed by the right triangle measured in radians: Math.atan2(x, y)

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
       trace(Math.atan2(2,3));
    }
  }
}



Finding Absolute Values

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var nValue:int = 10;
        
        if(nValue < 10 && nValue > -10) {
          // Code goes here.
        } 
        //can be rewritten in the following way using the abs() method: 
        if(Math.abs(nValue) < 10) {
          // Code goes here.
        }
    }
  }
}



Finding the Greater or Lesser of Two Numbers

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace(Math.min(25, 2));  // Displays: 2
        trace(Math.max(25, 2));  // Displays: 25

    }
  }
}



Generate a random number within a range that does not start with 0

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var nRandomFloat:Number = (Math.random() * 10) + 20;
    }
  }
}



Generate random number in a range

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
       trace(randomInRange(2,5));
    }
     function randomInRange(min:Number, max:Number):Number {
        var scale:Number = max - min;
        return Math.random() * scale + min;
     }
  }
}



Generating Random Numbers: The random() returns a floating-point value between 0 and 0.999999, inclusive.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var randomFloat:Number = Math.random() * 2;
        
        var nRandomFloat:Number = Math.random() * 45;
        
        trace(nRandomFloat);
    }
  }
}



If you are working with one six-sided die, you want to generate a random number between 1 and 6 each time

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
          var nValue:Number = Math.floor(Math.random() * 6) + 1;
          trace(nValue);
    }
  }
}



Math Class Arithmetic

 
Method Call      Returns 
Math.pow(a, b)      a raised to the b power (ab)
Math.exp(a)      e raised to the a power (ea)
Math.floor(a)      a rounded down
Math.ceil(a)      a rounded up
Math.round(a)      a rounded to the nearest digit
Math.max(a, b, c...)      Maximum of the set a, b, c &acirc;�� 
Math.min(a, b, c...)      Minimum of the set a, b, c &acirc;�� 
Math.sqrt(a)      Square root of a 
Math.abs(a)      Absolute value of a 
Math.log(a)      Logarithm (base 10) of a 
Math.ln(a)      Natural logarithm (base e) of a



Math class includes the constant Math.PI for the number, ratio of a circle"s circumference to its diameter.

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace(Math.PI);
    }
  }
}



Math Class Trigonometry

 
Method Call      Returns 
Math.sin(a)      Sine of an angle measuring a radians
Math.cos(a)      Cosine of an angle measuring a radians
Math.tan(a)      Tangent of an angle measuring a radians
Math.asin(a)      Angle in radians whose sine is a (arcsine of a)
Math.acos(a)      Angle in radians whose cosine is a (arccosine of a)
Math.atan(a)      Angle in radians whose tangent is a (arctangent of a)
Math.atan2(y, x)      Angle which, drawn from the origin, intersects the point (x, y) (arctangent of y/x)



Math Constants

 
Property      Value      Description 
E             ~2.718      Base of natural logarithm 
LN10          ~2.302      Natural logarithm of 10 
LN2           ~0.693      Natural logarithm of 2 
LOG10E        ~0.434      Base-10 logarithm of E 
LOG2E         ~1.442      Base-2 logarithm of E 
PI            ~3.142      Pi 
SQRT1_2       ~0.707      Square root of 1/2 
SQRT2         ~1.414      Square root of 2



package{

 
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var nPathRadius:Number = 100;
        var nDegrees:Number = 0; 
        
        var nRadians:Number = nDegrees * (Math.PI / 180); 
        
        var nX:Number = nPathRadius * Math.sin(nRadians);
        var nY:Number = nPathRadius * Math.cos(nRadians); 
        
        trace(nX);
        trace(nY);
        
    }
  }
}



Random integer values

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var nRandomInteger:Number = Math.floor(Math.random()*10);
        trace(nRandomInteger);
        
        nRandomInteger = Math.floor(Math.random()*10) + 1;
        trace(nRandomInteger);
    }
  }
}



Rounding and Truncating Numbers: round(), ceiling(), and floor()

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace(Math.round(5.75));  // Displays: 6
        trace(Math.round(93.3));  // Displays: 93
    }
  }
}



The calculation for finding compound interest: newValue = originalValue * (1 + rate/cp)^(cp*t)

 
cp is the number of compounding periods per year (12 if it is compounded every month)
t is the number of years. 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var nOrig: int = 10;
        var nRate:int = 2;
        var nCp: int = 3;
        var nT: int = 3;
        
        var nNew = nOrig * Math.pow((1 + nRate/nCp), (nCp*nT));
        trace(nNew);
    }
  }
}



The ceil() method returns the next-highest integer value of the number passed it as an argument.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace(Math.ceil(5.75));  // Displays: 6
        trace(Math.ceil(93.3));  // Displays: 94
        trace(Math.ceil(93));  // Displays: 93
    }
  }
}



The exp() method requires one parameter: a number. It then raises e (Math.E) to the power of that number.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        if(Math.exp(5) == Math.pow(Math.E, 5)){
          trace("=");    
        }
    }
  }
}



The floor()returns the next-lowest integer.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace(Math.floor(5.75));  // Displays: 5
        trace(Math.floor(93.3));  // Displays: 93
        trace(Math.floor(93));  // Displays: 93
    }
  }
}



To round a number to the nearest decimal place:

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace (Math.round(90.337 / .01) * .01);   // Displays: 9.34
    }
  }
}



Use Math.ceil( ) to round a number up.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace(Math.ceil(401.01));    // Displays: 402
    }
  }
}



Use Math.floor( ) to round a number down

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace(Math.floor(204.99));   // Displays: 204
    }
  }
}



Use Math.round( ) to round a number to the nearest integer

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace(Math.round(204.499));  // Displays: 204
        trace(Math.round(401.5));    // Displays: 402
    }
  }
}



Use round(), ceiling(), and floor() to round or truncate to decimal place values.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var nValue:Number = 6.39639;
        nValue *= 100;
        nValue = Math.floor(nValue);
        nValue /= 100;
        trace(nValue);  // Displays: 6.39
    }
  }
}



Use the identical math to round a number to the nearest multiple of an integer.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace (Math.round(92.5 / 5)  *  5);   // Displays: 95
    }
  }
}