計算兩點角度
這問題我在Flex 碰到過
寫Silverlight也寫過
寫 Javascript 又遇到
看來我得常常跟我討厭得數學糾纏
所以在blog 記錄一下
這是用javascript 寫得
function ConvertPositionAngel(soucePoint,targetPoint)
{
var res=(Math.atan2(targetPoint.y-soucePoint.y,targetPoint.x-soucePoint.x)) / Math.PI * 180.0;
return (res>=0 && res <=180)?res+=90:((res<0>=-90)? res+=90: res+=450);
}
用法很簡單
soucePoint 是你的圓點座標
targetPoint 是你要取相對soucePoint 角度的座標點
所以呼叫範例是
var sP={x:100,y:100};
var tP={x:50,y:100};
alert(ConvertPositionAngel(sP,tP));
這樣印出來會是 270 度
有沒有很溫馨...^^
標籤:
Javascript
|
This entry was posted on 上午10:07
and is filed under
Javascript
.
You can follow any responses to this entry through
the RSS 2.0 feed.
You can leave a response,
or trackback from your own site.
0 意見:
張貼留言