【问题标题】:Calculate Bearing with lat and long but do not have ATAN2 function用 lat 和 long 计算方位,但没有 ATAN2 功能
【发布时间】:2014-03-21 14:42:29
【问题描述】:

我一直在网上搜索,试图找到一种不使用 ATAN2 函数来计算方位的方法。我找到了这段代码,但我无法让它工作。我试图在我的 PLC 程序中使用它之前验证这个方程,但数字没有正确出现。任何帮助表示赞赏。

  y = sin(lon2-lon1)*cos(lat2)
  x = cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1)
  if y > 0 then
    if x > 0 then tc1 = arctan(y/x)
    if x < 0 then tc1 = 180 - arctan(-y/x)
    if x = 0 then tc1 = 90
  if y < 0 then
    if x > 0 then tc1 = -arctan(-y/x)
    if x < 0 then tc1 = arctan(y/x)-180
    if x = 0 then tc1 = 270
  if y = 0 then
    if x > 0 then tc1 = 0
    if x < 0 then tc1 = 180
    if x = 0 then [the 2 points are the same]

基因

【问题讨论】:

    标签: atan2 bearing


    【解决方案1】:

    你的方程没问题。 这是一个关于 bash 的工作实现。

    # /bin/bash
    
    DEG_PER_RAD=$(echo "scale=10; 180 / 3.141592653589" | bc)
    function atan2 {
        if [ $(echo "scale=10; $1 > 0" | bc -l) -eq 1 ]
        then
            if [ $(echo "scale=10; $2 > 0" | bc -l) -eq 1 ]
            then
                a2=$(echo "scale=10; a($1 / $2)" | bc -l)
            fi
            if [ $(echo "scale=10; $2 < 0" | bc -l) -eq 1 ]
            then
                a2=$(echo "scale=10; 3.141592653589 - a(-1 * $1 / $2)" | bc -l)
            fi
            if [ $(echo "scale=10; $2 == 0" | bc -l) -eq 1 ]
            then
                a2=$(echo "scale=10; 3.141592653589 / 2" | bc -l)
            fi
        fi
        if [ $(echo "scale=10; $1 < 0" | bc -l) -eq 1 ]
        then
            if [ $(echo "scale=10; $2 > 0" | bc -l) -eq 1 ]
            then
                a2=$(echo "scale=10; -1 * a(-1 * $1 / $2)" | bc -l)
            fi
            if [ $(echo "scale=10; $2 < 0" | bc -l) -eq 1 ]
            then
                a2=$(echo "scale=10; a($1 / $2) - 3.141592653589" | bc -l)
            fi
            if [ $(echo "scale=10; $2 == 0" | bc -l) -eq 1 ]
            then
                a2=$(echo "scale=10; 3 * 3.141592653589 / 2" | bc -l)
            fi
        fi
        if [ $(echo "scale=10; $1 == 0" | bc -l) -eq 1 ]
        then
            if [ $(echo "scale=10; $2 > 0" | bc -l) -eq 1 ]
            then
                a2=0
            fi
            if [ $(echo "scale=10; $2 < 0" | bc -l) -eq 1 ]
            then
                a2=$(echo "scale=10; 3.141592653589" | bc -l)
            fi
            if [ $(echo "scale=10; $2 == 0" | bc -l) -eq 1 ]
            then
                a2=0
            fi
        fi
    }
    
    function get_bearing {
        rad_lat1=$(echo "scale=9; ("$1" / $DEG_PER_RAD)" | bc)
        rad_lon1=$(echo "scale=9; ("$2" / $DEG_PER_RAD)" | bc)
        rad_lat2=$(echo "scale=9; ("$3" / $DEG_PER_RAD)" | bc)
        rad_lon2=$(echo "scale=9; ("$4" / $DEG_PER_RAD)" | bc)
    
        dLon=$(echo "scale=10; $rad_lon2 - $rad_lon1" | bc -l)
        y=$(echo "scale=10; s($dLon) * c($rad_lat2)" | bc -l)
        x=$(echo "scale=10; (c($rad_lat1) * s($rad_lat2)) - (s($rad_lat1) * c($rad_lat2) * c($dLon))" | bc -l)
        atan2 $y $x
        bearing=$(echo "scale=10; $DEG_PER_RAD * $a2" | bc -l)
    }
    
    get_bearing 49.624196795 6.1256280094 49.6241653669 6.1242621755
    echo "Bearing is "$bearing
    

    关于 PLC,我没有经验,所以无法帮助您。对不起

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多