Python 将对角线转换为正交线段

Python 将对角线转换为正交线段,python,graph,graphics,scipy,Python,Graph,Graphics,Scipy,我正在生成可以是垂直线、水平线或对角线(由端点给定)的线,并希望将这些线调整到类似于二维网格的环境中,以便将整个线分解为更小的1单位长度的线,并且每个部分仅为垂直线或水平线,而不是对角线 我目前的方法是将每条线分成1个单位的部分,抓住端点,并将它们作为我的新线处理。我使用一个集合来防止重复 newEdges = Set([]) # discretize edges into 1-unit edges for edge in cleanEdges: distance = edge[0].d

我正在生成可以是垂直线、水平线或对角线(由端点给定)的线,并希望将这些线调整到类似于二维网格的环境中,以便将整个线分解为更小的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)
newEdges=Set([])
#将边离散为1个单位的边
对于CleanEdge中的edge:
距离=边[0]。到点的距离(边[1])

如果距离你似乎在寻找某种形式的距离。一个现成可用的实现在scikit图像中显示为
skimage.draw.line_aa

,因此我所寻找的实际术语是4连接线。一旦你知道了这个词,搜索就变得非常容易了。6502有一个很好的解决方案: