Label

  • ActionScript 2
  • AD Player 2
  • Algorithm 2
  • AS3 16
  • Audio 1
  • Audio Description Player 1
  • Bitmap 1
  • BitmapData 1
  • Browser 1
  • C# Experience 4
  • Chart 1
  • Flash 10
  • Game 1
  • Geometry 1
  • JavaScript 2
  • Mathematics 6
  • Music Online 1
  • MVC 1
  • Performance 1
  • PureMVC 1
  • Slide 1
  • Talks 1
  • Tool 1
  • Tool Player 1
Toggle Side
Home   »   Mathematics   »   Useful mathematical formulas in ActionScript 3

Useful mathematical formulas in ActionScript 3

We have listed 12 very simple,basic but useful function source in Flex in the previous article. The mathematical calculation will be used regularly in our Flash applications, we can easily find the open source libraries like 30+ useful as3 open source projects, but it’s superfluous when we only need calculate the distance between two points, or only want to get the circle area. Here is the list of 18 useful AS3 arithmetic & formulas. Have any proudly and recommendable? Leave your comments to share please.

1. Distance Between Two Points




dx = x2 – x1;
dy = y2 – y1;
dist = Math.sqrt(dx*dx + dy*dy);

2. Inching Formulas
sprite.x += (targetX – sprite.x) * easing;//easing: inching coefficient
sprite.y += (targetY – sprite.y) * easing;

3. Elastic Formulas
vx += (targetX – sprite.x) * spring; //spring: elastic coefficient
vy += (targetY – sprite.y) * spring;
sprite.x += (vx *= friction); //friction: friction force
sprite.y += (vy *= friction);
4. Flexible Partial Shifts Formulas
var dx:Number = sprite.x – fixedX;
var dy:Number = sprite.y – fixedY;
var angle:Number = Math.atan2(dy, dx);
var targetX:Number = fixedX + Math.cos(angle) * springLength;
var targetY:Number = fixedX + Math.sin(angle) * springLength;

5. Rotation With Mouse Formulas
dx = mouseX – sprite.x;
dy = mouseY – sprite.y;
sprite.rotation = Math.atan2(dy, dx) * 180 / Math.PI;

6. Waveform Formulas
public function onEnterFrame1(event:Event):void {
ball.y=centerScale+Math.sin(angle)*range;
angle+=speed;
}

7. Heartthrob Formulas
public function onEnterFrame1(event:Event):void {
ball.scaleX=centerScale+Math.sin(angle)*range;
ball.scaleY=centerScale+Math.sin(angle)*range;
angle+=speed;
}

8. Circle Rotation Formulas
public function onEnterFrame(event:Event):void {
ball.x=centerX+Math.cos(angle)*radius;
ball.y=centerY+Math.sin(angle)*radius;
angle+=speed;
}

9. Get Circle Area
public function getArea():Number
{
// The formula is Pi times the radius squared.
return Math.PI * Math.pow((width / 2), 2);
}

10. Get Circumference Ratio
public function getCircumference():Number
{
// The formula is Pi times the diameter.
return Math.PI * width;
}


11. Elliptic Rotation Formulas
public function onEnterFrame(event:Event):void {
ball.x=centerX+Math.cos(angle)*radiusX;
ball.y=centerY+Math.sin(angle)*radiusY;
angle+=speed;
}

12. Color operations
var t:uint=0×77ff8877
var s:uint=0xff000000
var h:uint=t&s
var m:uint=h>>>24
trace(m);

13. Hex to Decimal
trace(hexValue);

14. Decimal to Hex
decimalValue.toString(16);

15. Pick Up Color
red = color24 >> 16;
green = color24 >> 8 & 0xFF;
blue = color24 & 0xFF;
alpha = color32 >> 24;
red = color32 >> 16 & 0xFF;
green = color32 >> 8 & 0xFF;
blue = color232 & 0xFF;


16. Color Bit Arithmetic
color24 = red << 16 | green << 8 | blue;
color32 = alpha << 24 | red << 16 | green << 8 | blue;


17. Percent of a Number
/**
* Comparing the present with a series of numbers, provided the per cent over the range of those
* @param start
* @param end
* @param current
* @return percent of the number 0 - 100 if the start
*/
public static function coPercent(c:Number, s:Number = 0, e:Number = 100):Number {
return (((e - s) - (e - c))/(e - s)) * 100;
}
//50 -> 400 ~ 100%
//50 -> 200 ~ ? %
// cal: 42.8%
//percent = NumberUtilities.coPercent(200, 50, 400);
//trace(percent); // 42.857142857142854





18. Number of a percent
/**
* Percentage change from the current number
* @param percent
* @param start
* @param end
* @return
*/
public static function dePercent(p:Number, s:Number = 0, e:Number = 100):Number {
return (e - ((e - s) - ((p/100) * (e - s))));
}
//100% ~ 50 - 400
//42.8%~ 50 - ?
// cal: 200
//number = NumberUtilities.dePercent(42.8, 50, 400);
//trace(number); // ~200



For demo 17 - 18:



19. Number of factorial
/**
* The term n! (pronounced n factorial) means multiply together all the whole numbers from 1 to n
* @param n, remember 0! = 1
* @return n!
*/
public function factorial(n:Number):Number{
var k:Number=1;
for (var i:int = 1; i <= n; i++) k = k * i;
return k;
}
//5! = 1 x 2 x 3 x 4 x 5 = 120
20. Number of factorial
/**
* The number of ways of selecting r objects from a total of n is written as
* @param n
* @param r
* @return
*/
public function combinatorics(n:Number, r:Number):Number {
return factorial(n) / (factorial(r) * factorial(n - r));
}
//5C0 = 1
//5C1 = 5
//5C2 = 10
//5C3 = 10
//5C4 = 5
//5C5 = 1

21. Pascal's Triangle
/**
* Pascal's Triangle
* @param n
* @param r
* @return
*/
public function pascal(n:Number, r:Number):Number{
if (n > 1 && r > 0 && r < n) {
var a:Number = pascal(n - 1, r - 1);
var b:Number = pascal(n - 1, r);
var c:Number = a + b;
return c;
}
return 1;
}
//(1 + x)3 = 3C0 + 3C1x + 3C2x2 + 3C3x3

0 1 2 3 4 5 6
0 1 1 1 1 1 1 1
1 1 2 3 4 5 6
2 1 3 6 10 15
3 1 4 10 20
4 1 5 15
5 1 6
6 1

Labels: AS3 ,  Mathematics   at:  6:32 AM     Email This BlogThis! Share to X Share to Facebook

0 comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments ( Atom)
© Hung Le 2007-2016. Powered by Blogger