Skip to content

16 Useful Mathematical Formulas In ActionScript 3

자주 사용하는 공식들만 잘 정리해서 올라온 글이 있어서 스크랩 해 둡니다. 매번 사용 할 때마다 까먹고 또 찾게 되는 공식들을 한군데 모아 놓으니 더 이상 공식찾아 헤맬 필요가 없어지겠네요.

원본 – http://ntt.cc/2010/07/06/16-useful-mathematical-formulas-in-actionscript-3.html

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;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.