title: Elegant/Clean (special case) Straight-line Grid Traversal Algorithm?
body: <p>I'm dusting off an old project of mine.  One of the things it had to do was -- given a Cartesian grid system, and two squares on the grid, find a list of all squares that a line joining the center of those two squares would pass through.</p>
  
  <p>The special case here is that all start and end points are confined to the exact center of squares/cells.</p>
  
  <p>Here are some examples -- with pairs of sample starting and ending points.  The shaded squares are the ones that should be returned by the respective function call</p>
  
  <p><em>removed dead ImageShack link - example</em></p>
  
  <p>The starting and end points are referred to by the squares they are in.  In the above picture, assuming that the bottom left is <code>[1,1]</code>, the line on the bottom right would be identified as <code>[6,2]</code> to <code>[9,5]</code>.</p>
  
  <p>That is, from the (center of the) <em>square</em> on the sixth column from the left, on the second row from the bottom to the (center of the) <em>square</em> on the ninth column from the left, on the fifth row from the bottom</p>
  
  <p>Which really doesn't seem that complicated.  However, I somehow seemed to have found some complex algorithm online and implemented it.</p>
  
  <p>I do recall that it was very, very fast.  Like, optimized-for-a-hundreds-or-thousands-of-times-per-frames fast.</p>
  
  <p>Basically, it jumped from border to border of the squares, along the line (the points where the line crosses the grid lines).  It knew where the next crossing point was by seeing which crossing point was closer -- a horizontal one or a vertical one -- and moved to that next one.</p>
  
  <p>Which is sort of okay in concept, but the actual implementation turned out to be pretty not-so-pretty, and I'm afraid that the level of optimization might be way too high for what I practically need (I'm calling this traversal algorithm maybe five or six times a minute).</p>
  
  <p>Is there a simple, easy-to-understand, transparent straight-line grid traversal algorithm?</p>
  
  <p>In programmatic terms:</p>
  
  <pre><code>def traverse(start_point,end_point)
    # returns a list of all squares that this line would pass through
  end
  </code></pre>
  
  <p>where the given coordinates identify the <em>squares</em> themselves.</p>
  
  <p>Some examples:</p>
  
  <pre><code>traverse([0,0],[0,4])
  # =&gt; [0,0], [0,1], [0,2], [0,3], [0,4]
  traverse([0,0],[3,2])
  # =&gt; [0,0], [0,1], [1,1], [2,1], [2,2], [3,2]
  traverse([0,0],[3,3])
  # =&gt; [0,0], [1,1], [2,2], [3,3]
  </code></pre>
  
  <p>Note that lines that move directly through corners should not include squares on the "wing" of the line.</p>
  
  <p><strike>(Good ol' Bresenham's might work here, but it's a bit backwards from what I want.  As far as I know, in order to use it, I'd basically have to apply it to the line and then scan every single square on the grid for true or false.  Infeasible -- or at least inelegant -- for large grids)</strike></p>
  
  <p>(I am re-looking into Bresenham, and Bresenham-based algorithms, due to a misunderstanding of mine)</p>
  
  <hr>
  
  <p>For clarification, one possible application of this would be, if I was storing all of my objects in a game inside zones (a grid), and I have a ray, and want to see which objects the ray touches.  Using this algorithm, I could test the ray to only the objects that are within the given zones, instead of every object on the map.</p>
  
  <p>The <strong>actual</strong> use of this in my application is that every tile has an effect associated with it, and an object moves through a given line segment every turn.  At every turn, it is necessary to check to see which squares the object has traversed through, and therefore, which effects to apply to the object.</p>
  
  <p>Note that, at this point, the current implementation I have does work.  This question is mostly for curiosity's purpose.  There has to be a simpler way...somehow...for such a simple problem.</p>
  
  <hr>
  
  <p>What am I looking for exactly?  Something conceptually/neat and clean.  Also, I've realized that due to what I am exactly specifying, all start and end points are always going to be in the center of squares/cells; so perhaps something that takes advantage of that would be neat as well.</p>