2017-09-16 102 views
0

得到了一個簡單的問題。我有一個應用程序,平原的標誌隨機飛過。 (他們每X秒得到一個左邊距和上邊距的隨機新位置)。接下來要旋轉它們的挑戰是爲了讓飛機的頭部朝向它們飛行的角度。在隨機給定的邊距左邊距和邊距頂點之後計算角度

Did not really find a solution how how do that。我們只知道:

MARGIN-LEFT

MARGIN-TOP

但這些數值不斷變化的飛機飛隨機。任何想法或建議?

UPDATE(爲了更明顯),這裏是一個代碼塊:

var nh = Math.floor(Math.random() * $(window).height() - 50); 
    var nw = Math.floor(Math.random() * $(window).width() - 50); 
    $('#plane').animate({ top: nh, left: nw}, 10000, function(){}); 
    $('#plane').css({"transform" : "rotate(" + Math.atan2(nh,nw)/Math.PI*180 +"deg)"}); 
+0

這個問題不清楚 - 請張貼代碼或提供你要完成的一個小例子。 – skyline3000

+0

var nh = Math.floor(Math.random()* $(window).height() - 50); (Math.random()* $(window).width() - 50);數學公式如下: $('#plane')。animate({top:nh,left:nw}) – Koppany

+0

抱歉,如果它不那麼明顯。新的高度和新的寬度每10秒計算一次。 – Koppany

回答

1

這不是很清楚你想要達到的目標。

爲了計算基於邊緣的角度,你可以使用Math.atan2()

function getAngle(x1,y1,x2,y2){ 
    return Math.atan2(y2-y1,x2-x1)/Math.PI*180 
} 
var angle = getAngle(MARGIN-LEFT, MARGIN-TOP); 
console.log('the angle is',angle,'degrees')