What is the fastest way to find the cells traversed by the segment (if any), sorted by traversal order ?
In the example above, the correct results are :
You can try "A Fast Voxel Traversal Algorithm for Ray Tracing" by Amanatides and Woo.
It is intended to deal with big grids, but the principle can be useful for your application too.
The following can be done using only a simple line intersect formula:
Observe that your grid consists of 6 line segments (3 horizontal, 3 vertical). If you were to extend each of these segments into infinity (making them lines, without start or end points), you have divided your 2D space into 16 areas.
Define a 4x4 array of areas. For each such area, store which line (if any) delimits it on the north side, on the east side, etc. This will be our traversal data structure.
Now, to find the cells traversed by a given query segment S, starting at u and ending at v, we will do a line walk from u to v using this traversal data structure to keep track of the area we're currently in and at which locations we're exiting that area.
Determine Au as the area in which u is located, and Av as the area in which v is located. Because of the axis-aligned nature of the areas, this shouldn't take more than 4 comparisons per area (2 on x-coordinate, 2 on y).
Also, define the current location as p and the current area as A; initially, these will be u and Au, respectively.
First, report A as an area that is traversed by S. Determine the first intersection point between the segment* (p, v] and the delimiting lines at each of the 4 sides of A. If such an intersection point q is found, the side that contains q determines which neighbouring area will become our new A - in that case our new p will be the intersection point q. Using this new A and p, repeat this step.
If no intersection point is found, p must lie in the same area as v and the walk is complete.
*(p, v] meaning: the segment between p and v, excluding p but including v.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With