图像中两点之间的像素坐标

图像中给定两点位置,可以确定一条直线,计算出图像上这两点形成的线段上像素坐标,可以先求出直线方程,然后依次计算出点坐标,但这样需对每一个像素都使用直线方程来计算,这里采用DDA(digital differential analyzer)画直线


def LineDDA(start, end, points):
    start_x = start[0]
    start_y = start[1]
    end_x = end[0]
    end_y = end[1]
    delta_x = end_x - start_x
    delta_y = end_y - start_y

    if abs(delta_x) > abs(delta_y):
        steps = abs(delta_x)
    else:
        steps = abs(delta_y)

    x_step = delta_x / steps
    y_step = delta_y / steps

    x = start_x
    y = start_y
    while steps >= 0:
        points.append([round(x), round(y)])
        x += x_step
        y += y_step
        steps -= 1

start = [6, 3]
end = [2, 8]
points = []
LineDDA(start, end, points)
print(points)

参考链接

计算机图形学----DDA、Bresenham直线算法

数值微分法(DDA)详解

用C语言绘制高效率抗锯齿直线

上一篇:凸包算法


下一篇:Codlity(1) DiamondsCount