【问题标题】:Bresenham line doesn't terminate布雷森汉姆线没有终止
【发布时间】:2011-08-23 18:32:46
【问题描述】:

我已经在 python 中实现了来自 Wikipedia 的 Bresenham algorithm,但是对于某些行它不起作用,例如从 1,0 到 0,1 它不会停止并继续制作超长的行

def line(x0, y0, x1, y1):
    dx = x1 - x0
    dy = y1 - y0
    sx = x0 < x1 and 1 or -1
    sy = y0 < y1 and 1 or -1
    err = dx - dy

    points = []
    x, y = x0, y0
    while True:
        points += [(x, y)]
        if x == x1 and y == y1:
            break
        e2 = err * 2
        if e2 > -dy:
            err -= dy
            x += sx
        if e2 < dx:
            err += dx
            y += sy
    return points

【问题讨论】:

    标签: python graphics bresenham


    【解决方案1】:

    您在dxdy 的初始化过程中缺少对abs 的调用:

    dx = abs(x1 - x0)
    dy = abs(y1 - y0)
    

    【讨论】:

      【解决方案2】:

      您在“if e2 &gt; -dy:”中有一个类型。减号是错误的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-15
        • 2019-06-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多