<分区>
我需要仅使用 PutPixel(x,y) 来实现 Line(x1,y1,x2,y2)。 最好没有 float 。 有任何语言的例子吗?
<分区>
我需要仅使用 PutPixel(x,y) 来实现 Line(x1,y1,x2,y2)。 最好没有 float 。 有任何语言的例子吗?
最佳答案
你可以实现 Bresenham's line algorithm .这是一个伪代码 implementation :
function line(x0, y0, x1, y1)
dx := abs(x1-x0)
dy := abs(y1-y0)
if x0 < x1 then sx := 1 else sx := -1
if y0 < y1 then sy := 1 else sy := -1
err := dx-dy
loop
setPixel(x0,y0)
if x0 = x1 and y0 = y1 exit loop
e2 := 2*err
if e2 > -dy then
err := err - dy
x0 := x0 + sx
end if
if e2 < dx then
err := err + dx
y0 := y0 + sy
end if
end loop
关于algorithm - Line(x1,y1,x2,y2) 只需要使用 PutPixel(x,y) 示例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8282051/