Reputation: 1212
In a 2D grid world with known boundary, there are :-
org
)How to ray trace from center of every white block in the grid to center of org
efficiently?
For each block, I want a single boolean - whether it is lit.
In other words, I want the determine whether org
can see each block (for whole world) directly or not.
Use standard ray tracing to trace each and every white block toward org
, but its performance is very bad. I feel that a lot of computation is redundant.
Related : https://en.wikipedia.org/wiki/Any-angle_path_planning : The algorithm is still for one white block - not the whole world.
Upvotes: 1
Views: 1588
Reputation: 59243
First make a std::map
containing pairs of double
s. This will hold the angle ranges from which org is visible. Initially populate it with [0,2*PI]
Then, process squares in order of their distance from org. For each square:
lower_bound
to find the range that contains the angle from org to the center of the block, if any. If the angle is in one of the ranges, then the block is visible.That will work in O(N log N) time, which is not bad. Once you're comfortable with how that works, there are a bunch of optimizations you can do. Most importantly, if you process squares in a predefined order, so that you will visit the set of ranges in order, then you can use vectors instead of a map, because you don't have to search. That will get the runtime down to O(N).
Upvotes: 1
Reputation: 2779
You can use Line algorithms like Bresenham's line algorithm or Xiaolin Wu's line algorithm to find the pixels in the path.
The same can be used for multiple lights as well. This will be efficient, as you only calculate one line for each pixel.
Upvotes: 2