16 Useful Mathematical Formulas In ActionScript 3
From http://ntt.cc/2010/07/06/16-useful-mathematical-formulas-in-actionscript-3.html
1. Distance Between Two Points
[js]dx = x2 – x1;
dy = y2 – y1;
dist = Math.sqrt(dx*dx + dy*dy);[/js]
2. Inching Formulas
[js]sprite.x += (targetX - sprite.x) * easing;//easing: inching coefficient
sprite.y += (targetY - sprite.y) * easing;[/js]
3. Elastic Formulas
[js] vx += (targetX - sprite.x) * spring; //spring: elastic coefficient
vy += (targetY - sprite.y) * spring;
sprite.x += (vx *= friction); //friction: friction force
sprite.y += (vy *= friction);[/js]
4. Flexible Partial Shifts Formulas
[js]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;[/js]
5. Rotation With Mouse Formulas
[js]dx = mouseX - sprite.x;
dy = mouseY - sprite.y;
sprite.rotation = Math.atan2(dy, dx) * 180 / Math.PI;[/js]
6. Waveform Formulas
[js]public function onEnterFrame1(event:Event):void {
ball.y=centerScale+Math.sin(angle)*range;
angle+=speed;
}[/js]
7. Heartthrob Formulas
[js]public function onEnterFrame1(event:Event):void {
ball.scaleX=centerScale+Math.sin(angle)*range;
ball.scaleY=centerScale+Math.sin(angle)*range;
angle+=speed;
}[/js]
8. Circle Rotation Formulas
[js]public function onEnterFrame(event:Event):void {
ball.x=centerX+Math.cos(angle)*radius;
ball.y=centerY+Math.sin(angle)*radius;
angle+=speed;
}[/js]
9. Get Circle Area
[js]public function getArea():Number
{
// The formula is Pi times the radius squared.
return Math.PI * Math.pow((width / 2), 2);
}[/js]
10. Get Circumference Ratio
[js]public function getCircumference():Number
{
// The formula is Pi times the diameter.
return Math.PI * width;
}[/js]
11. Elliptic Rotation Formulas
[js]public function onEnterFrame(event:Event):void {
ball.x=centerX+Math.cos(angle)*radiusX;
ball.y=centerY+Math.sin(angle)*radiusY;
angle+=speed;
}[/js]
12. Color operations
[js]var t:uint=0×77ff8877
var s:uint=0xff000000
var h:uint=t&s
var m:uint=h>>>24
trace(m);[/js]
13. Hex to Decimal
[js]trace(hexValue);[/js]
14. Decimal to Hex
[js]decimalValue.toString(16);[/js]
15. Pick Up Color
[js]red = color24 >> 16;
green = color24 >> 8 & 0xFF;
blue = color24 & 0xFF;
alpha = color32 >> 24;
red = color32 >> 16 & 0xFF;
green = color32 >> 8 & 0xFF;
blue = color232 & 0xFF;[/js]
16. Color Bit Arithmetic
[js]color24 = red << 16 | green << 8 | blue;
color32 = alpha << 24 | red << 16 | green << 8 | blue;[/js]