16 Useful Mathematical Formulas In ActionScript 3

calendar_share 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/Flex 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 16 useful AS3 arithmetic & formulas. Have any proudly and recommendable? Leave your comments to share please.

1. Distance Between Two Points

  1. dx = x2x1;
  2. dy = y2y1;
  3. dist = Math.sqrt(dx*dx + dy*dy);

2. Inching Formulas

  1. sprite.x += (targetX - sprite.x) * easing;//easing: inching coefficient
  2. sprite.y += (targetY - sprite.y) * easing;

3. Elastic Formulas

  1. vx += (targetX - sprite.x) * spring; //spring: elastic coefficient
  2. vy += (targetY - sprite.y) * spring;
  3. sprite.x += (vx *= friction); //friction: friction force
  4. sprite.y += (vy *= friction);

4. Flexible Partial Shifts Formulas

  1. var dx:Number = sprite.x - fixedX;
  2. var dy:Number = sprite.y - fixedY;
  3. var angle:Number = Math.atan2(dy, dx);
  4. var targetX:Number = fixedX + Math.cos(angle) * springLength;
  5. var targetY:Number = fixedX + Math.sin(angle) * springLength;

5. Rotation With Mouse Formulas

  1. dx = mouseX - sprite.x;
  2. dy = mouseY - sprite.y;
  3. sprite.rotation = Math.atan2(dy, dx) * 180 / Math.PI;

6. Waveform Formulas

  1. public function onEnterFrame1(event:Event):void {
  2. ball.y=centerScale+Math.sin(angle)*range;
  3. angle+=speed;
  4. }

7. Heartthrob Formulas

  1. public function onEnterFrame1(event:Event):void {
  2. ball.scaleX=centerScale+Math.sin(angle)*range;
  3. ball.scaleY=centerScale+Math.sin(angle)*range;
  4. angle+=speed;
  5. }

8. Circle Rotation Formulas

  1. public function onEnterFrame(event:Event):void {
  2. ball.x=centerX+Math.cos(angle)*radius;
  3. ball.y=centerY+Math.sin(angle)*radius;
  4. angle+=speed;
  5. }

9. Get Circle Area

  1. public function getArea():Number 
  2. { 
  3.     // The formula is Pi times the radius squared.
  4.     return Math.PI * Math.pow((width / 2), 2);
  5. }

10. Get Circumference Ratio

  1. public function getCircumference():Number 
  2. { 
  3.     // The formula is Pi times the diameter.
  4.     return Math.PI * width;
  5. }

11. Elliptic Rotation Formulas

  1. public function onEnterFrame(event:Event):void {
  2. ball.x=centerX+Math.cos(angle)*radiusX;
  3. ball.y=centerY+Math.sin(angle)*radiusY;
  4. angle+=speed;
  5. }

12. Color operations

  1. var t:uint=0×77ff8877
  2. var s:uint=0xff000000
  3. var h:uint=t&s
  4. var m:uint=h>>>24
  5. trace(m);

13. Hex to Decimal

  1. trace(hexValue);

14. Decimal to Hex

  1. decimalValue.toString(16);

15. Pick Up Color

  1. red = color24 >> 16;
  2. green = color24 >> 8 & 0xFF;
  3. blue = color24 & 0xFF;
  4. alpha = color32 >> 24;
  5. red = color32 >> 16 & 0xFF;
  6. green = color32 >> 8 & 0xFF;
  7. blue = color232 & 0xFF;

16. Color Bit Arithmetic

  1. color24 = red << 16 | green << 8 | blue;
  2. color32 = alpha << 24 | red << 16 | green << 8 | blue;
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

9 Responses to “16 Useful Mathematical Formulas In ActionScript 3”

  1. Pedram says:

    some of them are very useful for me.
    thanks

  2. [...] 16 Useful Mathematical Formulas In ActionScript 3 Enjoy! [...]

  3. Mark says:

    Hi, nice overview! What does ‘Color operations’ (12.) exactly traces?

    • FlexMan says:

      I think the source:
      var h:uint=t&s
      var m:uint=h>>>24
      should be the crux, the line of trace was only the test source code.
      right?

  4. szataniol says:

    I would add this:

    Rotating a 2D Vector:

    http://snipt.org/TSi

    Real difference between two angles:

    http://snipt.org/TSj

    I’ll post if I think of something else..

    Cheers
    szataniol

    • FlexMan says:

      WOW!!! Rotate 2D Vector function!

      public function rotate(angle : Number) : void {
      var sin : Number = Math.sin(angle);
      var cos : Number = Math.cos(angle);

      var oldX : Number = x;
      var oldY : Number = y;

      x = oldX * cos – oldY * sin;
      y = oldX * sin + oldY * cos;
      }

      It’s helpful for me! Thank you!!! :)

  5. szataniol says:

    Getting control to draw curve through a point:

    http://snipt.org/TXp

    This one is very useful if you want to draw a bezier curve THROUGH that point, not using is as a control point.

  6. szataniol says:

    I’m not sure if this is related with the topic, anyway here’s some simple mouse avoiding:

    http://blog.szataniol.pl/?p=60

    cheers :)
    szataniol

Leave a Reply