如果您可以使用浮点,请以浮点类型计算斜率。如果线很陡,您可能需要增加 y 而不是 x。无论哪种方式,使用斜率和您正在步进的另一个坐标将一个坐标计算为浮点数。然后(正确地)四舍五入到最接近的整数。
I suspect you're computing the slope using an integer divide.
The link Gabe gave in the comments to the Bresenham line algorithm will illustrate a purely integer solution that's fast and efficient.
If you're OK using floating point, compute the slope as a floating point type. If the line is steep, you probably want to increment in y instead of x. Either way, compute one coordinate as a floating point number using the slope and the other coordinate that you're stepping. Then round (correctly) to the nearest integer.
发布评论
评论(1)
我怀疑您正在使用整数除法来计算斜率。
Gabe 在 Bresenham 线算法的评论中给出的链接将说明快速高效的纯整数解决方案。
如果您可以使用浮点,请以浮点类型计算斜率。如果线很陡,您可能需要增加 y 而不是 x。无论哪种方式,使用斜率和您正在步进的另一个坐标将一个坐标计算为浮点数。然后(正确地)四舍五入到最接近的整数。
I suspect you're computing the slope using an integer divide.
The link Gabe gave in the comments to the Bresenham line algorithm will illustrate a purely integer solution that's fast and efficient.
If you're OK using floating point, compute the slope as a floating point type. If the line is steep, you probably want to increment in y instead of x. Either way, compute one coordinate as a floating point number using the slope and the other coordinate that you're stepping. Then round (correctly) to the nearest integer.