2009年3月16日 星期一

重要公式 - 三角學應用

基本三角函數計算:
角的正弦值(Sine) = 對邊 / 斜邊
角的餘弦值(Cosine) = 鄰邊 / 斜邊
角的正切值(Tangent) = 對邊 / 鄰邊


角度制與弧度制的相互轉換:
弧度 = 角度 * Math.PI / 180
角度 = 弧度 * 180 / Math.PI


向滑鼠指標旋轉(或向某點旋轉):
// substitute mouseX, mouseY with the x, y
// point to rotate to

dx = mouseX - sprite.x;
dy = mouseY - sprite.y;
sprite.rotation = Math.atan2(dy, dx) * 180 / Math.PI;
// calculate the  velocity of x and y
vx = Math.cos(angle) * speed;
vy = Math.sin(angle) * speed:
sprite.x += vx;
sprite.y += vy;

建立波形:
// assign value to x, y or other property of sprite
// or movie clip, use as drawing coordinates,
// etc.

public function onEnterFrame(evt:Event):void{
value = center + Math.sin(angle) * range;
angle += speed;
}


建立圓形:
// assign position to x and y of sprite
// or movie clip, use as drawing coordinates,
// etc.

public function onEnterFrame(evt:Event):void{
xposition = centerX + Math.cos(angle) * radius;
yposition = centerY + Math.sin(angle) * radius;
angle += speed;
}


建立橢圓形
// assign position to x and y of sprite
// or movie clip, use as drawing coordinates,
// etc.

public function onEnterFrame(evt:Event):void{
xposition = centerX + Math.cos(angle) * radiusX;
yposition = centerY + Math.sin(angle) * radiusY;
angle += speed;
}


計算兩點間距離:
// points are x1, y1 and x2, y2
// can be sprite / movie clip positions,
// mouse coordinates, etc.

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



參考書目:Keith Peters.《Function Actionscript 3.0 Animation》.Friends of ED

沒有留言:

張貼留言