如何在Python中合并两条更近的非直线(非平行)线?

时间:2018-10-09 08:14:01

标签: python math line

我在两个角之间绘制了2条线,如果两条线之间的距离越来越近,我想将它们合并。但是问题是线条不是笔直的,因此,如果尝试将它们合并,则最接近的角落将合并,而另一侧则不是。例如,第1行和第2行的结束角彼此之间的距离更近,而开始角则没有。想知道您的意见。代码如下:

def mergeLineCorners(wallLines, _MERGE_TOLERANCE = 15):
for i in range(len(wallLines)):
    lineA = wallLines[i]
    for j in range(len(wallLines)):
        lineB = wallLines[j]
        if distance(lineA[0]['x'],lineA[0]['y'], lineB[0]['x'],lineB[0]['y']) < _MERGE_TOLERANCE: #A start B start 
            mid_x = float(lineA[0]['x'] + lineB[0]['x']) / 2
            mid_y = float(lineA[0]['y'] + lineB[0]['y']) / 2

            wallLines[i][0]['x'] = mid_x
            wallLines[i][0]['y'] = mid_y

            wallLines[j][0]['x'] = mid_x
            wallLines[j][0]['y'] = mid_y


        if distance(lineA[0]['x'],lineA[0]['y'], lineB[1]['x'], lineB[1]['y']) < _MERGE_TOLERANCE: #A start B end
            mid_x = float(lineA[0]['x'] + lineB[1]['x']) / 2
            mid_y = float(lineA[0]['y'] + lineB[1]['y']) / 2     

            wallLines[i][0]['x'] = mid_x
            wallLines[i][0]['y'] = mid_y
            wallLines[j][1]['x'] = mid_x
            wallLines[j][1]['y'] = mid_y
        if distance(lineA[1]['x'], lineA[1]['y'], lineB[0]['x'], lineB[0]['y']) < _MERGE_TOLERANCE:
            mid_x = float(lineA[1]['x'] + lineB[0]['x']) / 2
            mid_y = float(lineA[1]['y'] + lineB[0]['y']) / 2

            wallLines[i][1]['x'] = mid_x
            wallLines[i][1]['y'] = mid_y
            wallLines[j][0]['x'] = mid_x
            wallLines[j][0]['y'] = mid_y
        if distance(lineA[1]['x'], lineA[1]['y'], lineB[1]['x'], lineB[1]['y']) < _MERGE_TOLERANCE:
            mid_x = float(lineA[1]['x'] + lineB[1]['x']) / 2
            mid_y = float(lineA[1]['y'] + lineB[1]['y']) / 2

            wallLines[i][1]['x'] = mid_x
            wallLines[i][1]['y'] = mid_y
            wallLines[j][1]['x'] = mid_x
            wallLines[j][1]['y'] = mid_y

return wallLines  

从JSON文件传入的角和第1行的xy位置,例如第1行:x:330,y:530。和第2行。x2:330,y2:570

左侧是第1行,右侧是第2行 the output result after draw lines

如您所见,结束角已合并,但起点未合并。因为line2不像line1那样笔直

0 个答案:

没有答案