最近尝试学“布雷森汉姆直线算法”(Bresenham's line algorithm),是用来描绘由两点所决定的直线的算法。
简单来说,代码是这样的:
- plotLine(x0, y0, x1, y1)
- dx = x1 - x0
- dy = y1 - y0
- D = 2*dy - dx
- y = y0
- for x from x0 to x1
- plot(x, y)
- if D > 0
- y = y + 1
- D = D - 2*dx
- end if
- D = D + 2*dy
复制代码
plot(x,y)是在X,Y位置画一个点。
但以上并不完整,还需plotLineLow和plotLineHigh.
很开心,原来以前有人发明的了,只需明白原理后编写程序即可。
|