【问题标题】:Getting the shortest angle path获得最短角度路径
【发布时间】:2014-12-05 03:37:13
【问题描述】:

我需要得到两个角度之间的最短方向。就像如果 Ang1 是 5 并且 Ang2 是 355 那么我希望它返回我需要从 Ang1 中减去才能得到 Ang2 。

我有一些代码可以告诉我最短距离,但不是最短方向。

函数 getShortAngle(a1, a2)

{
var angle = (Math.abs(a1 - a2))%360;


if(angle > 180)

    angle = 360 - angle;

return angle;
};

trace(getShortAngle(360, 720));

或者在 Smallbasic 中:

Sub GetShortestpath
angle = Math.Remainder((Math.abs(a1 - a2)),360)
if angle > 180 Then
angle = 360 - angle
EndIf
Return = angle
EndSub

感谢您的帮助!

【问题讨论】:

    标签: math geometry shortest-path angle smallbasic


    【解决方案1】:

    当然,这只是根据您选择的角度设置方向。

    如果您正在计算从 a1a2 的角度/方向,以下伪代码应该可以满足您的需求:

    # precondition: [a1,a2] >= 0
    angle = ((a2 % 360) - (a1 % 360) + 360) % 360
    direction = CLOCKWISE
    if angle > 180:
        angle = 360 - angle
        direction = ANTICLOCKWISE
    

    如果相差正好 180°,它更喜欢顺时针方向。

    请原谅获取角度的复杂表达式,它只是为了确保您获得 0 到 359 的值无论负数模运算符的相对位置和工作方式。

    如果你让前置条件更严格,你可以大大简化它,确保a1a2被限制在0..359的范围内:

    # precondition: 0 <= [a1,a2] <= 359
    angle = (a2 - a1 + 360) % 360
    

    【讨论】:

    • 应该这样做!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-12-19
    • 2017-09-12
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多