清洁点列表取决于近距离(+ -5)

时间:2015-07-12 19:12:58

标签: bash

如何清除变量中的点列表

  1. 同一点或
  2. 一个接一个点(+ -5)。
  3. 示例每行是坐标为的一个点:

    points="808,112\n807,113\n809,113\n155,183\n832,572"
    echo "$points"
    #808,112
    #807,113
    #809,113
    #155,183
    #832,572
    #196,652
    

    我想忽略+ -5计数范围内的点数。结果应该是:

    echo "$points_clean"
    #808,112
    #155,183
    #832,572
    #196,652
    

    我考虑过遍历列表,但是我需要帮助来检查新列表中是否已存在点坐标:

    points_clean=$(for point in $points; do
              x=$(echo "$point" | cut -d, -f1)
              y=$(echo "$point" | cut -d, -f2)
              # check if same or similar point coordinates already in $points_clean
              echo "$x,$y"
            done)
    

1 个答案:

答案 0 :(得分:1)

这似乎适用于Bash 4.x(需要支持process substitution):

#!/bin/bash
close=100
points="808,112\n807,113\n809,113\n155,183\n832,572"
echo -e "$points"

clean=()

distance()
{
    echo $(( ($1 - $3) * ($1 - $3) + ($2 - $4) * ($2 - $4) ))
}

while read x1 y1
do
    ok=1
    for point in "${clean[@]}"
    do
        echo "compare $x1 $y1 with $point"
        set -- $point
        if [[ $(distance $x1 $y1 $1 $2) -le $close ]]
        then
            ok=0
            break
        fi
    done
    if [ $ok = 1 ]
    then clean+=("$x1 $y1")
    fi
done < <( echo -e "$points" | tr ',' ' ' | sort -u )

echo "Clean:"
printf "%s\n" "${clean[@]}" | tr ' ' ','

排序是可选的,可能会降低速度。相同的点将太靠近,因此即使第一个没有,也会消除给定坐标的第二个实例。

示例输出:

808,112
807,113
809,113
155,183
832,572
compare 807 113 with 155 183
compare 808 112 with 155 183
compare 808 112 with 807 113
compare 809 113 with 155 183
compare 809 113 with 807 113
compare 832 572 with 155 183
compare 832 572 with 807 113
Clean:
155,183
807,113
832,572

Bash 3.x的解决方法(例如,在Mac OS X 10.10.4上找到)有点痛苦;您需要将echo | tr | sort命令的输出发送到文件,然后从该文件重定向该对循环的输入(并在之后清理)。或者您可以将这对循环和后面的代码(echo数组的clean)放在{ …; } command grouping的范围内。

回答“什么定义关闭?”这个问题,wittich commented

  

让我们说±5计数。例如。 808(±5,)112(±5)。这就是为什么第二和第三点将被“清理”。

行。一种看待它的方法是在我的脚本中将close值调整为50(允许相差5 2 + 5 2 ),但是但是,拒绝通过长度超过7的线连接的点。你可以修改distance函数做±5;它需要更多的工作,也许是辅助abs函数,或者你可以返回较大的delta的平方并将其与25(当然是5 2 )进行比较。你可以根据自己的心灵内容来衡量标准。

注意Bash shell算术是整数算术(仅);你需要Korn shell(ksh)或Z shell(zsh)来获得shell中的真实算术,或者你需要使用bc或其他一些计算器。