﻿ 16 Useful Mathematical Formulas In ActionScript 3 - Ntt.cc

## 16 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/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

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 {
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 {
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; Enjoy this Post? Subscribe to Ntt.cc RSS Feed Email Feed Follow us

### 13 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:

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.

• Ntt.cc says:

Great!
Many thanks, szataniol. 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

7. reelfernandes says:

Great stuff! Finding the distance between 2 points is easier with the Point class.

dist = Point.distance( p1, p2 );

I haven’t done a performance comparison, that might be interesting = )

8. 73C says:

nice.
would be even nicer to see some examples how it would look like in the end. or an explanation how it should look or what it does.
what means Flexible Partial Shifts Formulas e.g.

9. Ram says:

Hi ……

Its a great stuff to AS developers..

ThankU……………..

10. Jon B says:

I don’t undserstand the “Flexible Partial Shifts Formula” – what does it do? I tried implementing it and couldn’t get much to happen, any chance somebody could explain or show and example of it’s usage?