I have a 2-dimmensional unit grid, and a bunch of line segments that start and end at any rational number. I need an efficient way to calculate which grid cells the line passes through. For example, the line:
From (2.1, 3.9) to (3.8, 4.8) passes through the grid cells with lower left points (2, 3), (2, 4), and (3, 4).
Is there a quick, efficient way to calculate these quadrants from the line’s endpoints?
I’ll be working in R, but an answer in Python or pseudocode would work too. Thanks!
Folks who work with spatial data deal with this kind of question all the time, so it may be worth piggy-backing on their efforts. Here’s a solution that uses R’s raster package (and functions from the sp package on which it depends):