将对角线转换为正交段

时间:2016-03-15 19:36:08

标签: python graph graphics scipy

我生成的行可以是垂直,水平或对角线(由端点给出),并希望将这些行调整为类似2D网格的环境,以便整个行被分解分为较小的1单位长度线,每个部分只是垂直或水平,不是对角线

我目前的方法是将每一行划分为1个单元,抓住端点,并将它们视为我的新线。我使用一套来防止重复。

newEdges = Set([])
# discretize edges into 1-unit edges
for edge in cleanEdges:
    distance = edge[0].distance_to_point(edge[1])

    if distance <= d:
        newEdges.add(edge)
    else:
        numNewPoints = int(ceil(distance / d))  # number of new points to add
        # break up into smaller pieces and add to path
        prevPoint = edge[0]
        for i in xrange(numNewPoints):
            # get x, y coords of new points
            newX = int(round(edge[0].x + (i * d) / distance * (edge[1].x - edge[0].x)))
            newY = int(round(edge[0].y + (i * d) / distance * (edge[1].y - edge[0].y)))
            newPoint = (newX, newY)

            if prevPoint != newPoint:
                newEdge = (prevPoint, newPoint)
                newEdges.add(newEdge)
                prevPoint = newPoint

        if prevPoint != edge[1]:
            newEdge = (prevPoint, edge[1])
            newEdges.add(newEdge)

然而,这不仅是笨重的,而且偶尔会产生对角线段。

对所有线路进行离散化并将对角线转换为水平/垂直线段的好方法是什么?

2 个答案:

答案 0 :(得分:0)

您似乎正在寻找某种形式的Bresenham algorithm。一个现成可用的实现在scikit-image中为skimage.draw.line_aa

答案 1 :(得分:0)

因此,我所寻找的实际术语是4连接线。一旦你知道这个词就很容易搜索。 6502这里有一个很好的解决方案:Algorithm for drawing a 4-connected line