2017-09-16 104 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')