每天學一點Flash(49) 三角函數在Flash中的應用

來源:互聯網
上載者:User

 假設半徑為R, A 和B 都是已知的點,座標分別為A(x1,y2),B(x2,y2) 求C 和D 點座標?

 

一個數學座標系分為四個象限,右上為第一象限 左上第二象限,左下第三象限,右下第四象限 。為了

求出滑鼠座標點A(x1,y1)和中心點B(x2,y2)連線的垂直中線和圓的兩個交點C和D,這裡需要藉助

幾個重要的三角函數。

Math.atan2,如果知道兩點的座標就可以求出反餘切的角度。

作AB 的連線的垂直分線CD,和半徑為R的交於兩點C和D,繼續作A的垂直線交於座標系X軸的D點,利用

Flash 的Math.atan2利用已知的條件角DBC的角度就是

90度減去反餘切的角度。藉助這個角度,通過餘弦和正弦的函數求出C點座標,同理D的座標也是一樣的

求法。

AB和CD垂直,知道90度的角

Flash as 2.0 代碼.

var R:Number=100;//定義半徑為5
var x1:Number=Stage.width/2;
var y1:Number=Stage.height/2;
var rot:Number=0;
onEnterFrame=function(){
clear();
lineStyle(1, 0xFF00FF, R);
moveTo(x1, y1);//中心點座標
lineTo(_root._xmouse,_root._ymouse);
rot=Math.PI/2-Math.atan2(_root._ymouse-y1, _root._xmouse-x1);

//case 1第一象限
if(_root._xmouse >=x1 && _root._ymouse <=y1)
{   
 lineTo(x1+R*Math.cos(rot),y1-R*Math.sin(rot)); 
 lineTo(x1-R*Math.cos(rot),y1+R*Math.sin(rot)); 
 lineTo(_root._xmouse,_root._ymouse);
 
}
//case2第二象限
if(_root._xmouse<=x1 && _root._ymouse<=y1)
{    
 lineTo(x1+R*Math.cos(rot),y1-R*Math.sin(rot));
 lineTo(x1-R*Math.cos(rot),y1+R*Math.sin(rot));
 lineTo(_root._xmouse,_root._ymouse);
 
}
//case 3 第三象限
if(_root._xmouse<=x1 && _root._ymouse>=y1)
{
 lineTo(x1+R*Math.cos(rot),y1-R*Math.sin(rot));
 lineTo(x1-R*Math.cos(rot),y1+R*Math.sin(rot));//
 lineTo(_root._xmouse,_root._ymouse);
 
}

//case4 第四象限
if(_root._xmouse>=x1 && _root._ymouse>=y1)
{  trace(Math.atan2(_root._ymouse-y1, _root._xmouse-x1));
 lineTo(x1+R*Math.cos(rot),y1-R*Math.sin(rot));
 lineTo(x1-R*Math.cos(rot),y1+R*Math.sin(rot));
 lineTo(_root._xmouse,_root._ymouse);
 
}

}

  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.