DDA 算法实现:修复后的代码和测试用例

非常抱歉,我之前给出的代码有一些错误。以下是修复后的可以进行测试的代码:

import math

def dda_algorithm(x1, y1, x2, y2):
    dx = x2 - x1
    dy = y2 - y1
    steps = max(abs(dx), abs(dy))
    x_inc = dx / steps
    y_inc = dy / steps
    
    x = x1
    y = y1
    
    positions = [(round(x), round(y))]  # Starting position
    
    for _ in range(steps):
        x += x_inc
        y += y_inc
        positions.append((round(x), round(y)))
    
    return positions

# Example usage for Line 1: from (-2, 3) to (10, 8)
positions_line1 = dda_algorithm(-2, 3, 10, 8)
print('Line 1 positions:', positions_line1)

# Example usage for Line 2: from (-2, 3) to (-22, 33)
positions_line2 = dda_algorithm(-2, 3, -22, 33)
print('Line 2 positions:', positions_line2)

你可以运行上述代码进行测试。我修复了计算增量的错误,并对坐标进行四舍五入以获得正确的像素位置。

DDA 算法实现:修复后的代码和测试用例

原文地址: https://www.cveoy.top/t/topic/yUV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录