擅长:python、mysql、java
<p>到目前为止,这两个答案都指向维基百科关于<a href="http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm" rel="noreferrer">Bresenhams</a>算法的一篇文章。这是这篇文章的插图,全尺寸的。请注意,这条线穿过没有高亮显示的网格正方形,因此Bresenham的算法只给出了所需的一个子集。</p>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/ab/Bresenham.svg/800px-Bresenham.svg.png" alt="alt text"/></p>
<p>既然你提到了“视线”,听起来你好像想要一个算法,枚举所有的方格线通过。这个集合有时被称为(行的)超级覆盖和<a href="http://eugen.dedu.free.fr/projects/bresenham/" rel="noreferrer">one algorithm is described here</a>。</p>
<p>还有其他一些方法,在<a href="https://stackoverflow.com/questions/3233522/elegant-clean-special-case-straight-line-grid-traversal-algorithm">this question</a>的答案中给出。</p>
<p><strong>更新:</strong><a href="http://playtechs.blogspot.ca/2007/03/raytracing-on-grid.html" rel="noreferrer">Here's another reference</a></p>