views:

124

answers:

2

I have a grid of rectangular cells covering a plane sitting at some distance from the coordinate system origin and would like to identify the grid cell where a straight line starting at the origin would intersect it.

The cells on the grid have equal sizes (dx,dy) and there are no gaps between cells, but since every cell on the plane has a different distance from the origin, the solid angle they cover is not constant -- if it was I could find a simple function that translates a direction (theta,phi) into a cell index (ix,iy).

Currently I use something like a nearest-neighbor search to find cells, but this doesn't exploit the "gridded-ness" of my cells at all. Is there any algorithm that would help me improve on this?

EDIT I know I could just use simple trigonometry to get the cell, but I am more interested in what algorithms there are that do nearest-neighbor searches on regularly spaced inputs.

+1  A: 

[...]but I am more interested in what algorithms there are that do nearest-neighbor searches on regularly spaced inputs.

Though they are data structures to be very specific, I think you should take a look at the following:

dirkgently
Thanks, these links are helpful.
honk
+1  A: 

But there won't be a unique cell that will be intersected. Do you mean exactly the center of the cell, perhaps?

In general, in the first quadrant (i.e. x>0, y>0) if you have a cell that fills the rectangle (x,y) -> (x+1,y+1), then any line with angles between atan2(y+1,x) and atan2(x,y+1) will intersect at least part of the cell.

Anyway, if you want to do general nearest-neighbor searches, you should divide your data into a quad-tree. It's one of the workhorses of nearest-neighbor calculations in 2D. You can also do multiscale grids if your data is sparse (which is really just a special case of a quad-tree with a particularly regular subdivision pattern, but that gives you constant-time lookup instead of log(N)).

Rex Kerr
Why shouldn't there be a unique cell? If the straight line passes through the grid, it has to pass through one and only one cell (since the cover the plane without gaps).As for using triogometry, this doesn't really help me to index cells. I have one the order of 100k cells, so checking every single one is pretty costly.
honk
@honk: Huh? Suppose you have a grid of points (3,3) (3,4) (3,5) (4,3) (4,4) (4,5) (5,3) (5,4) (5,5). The line from the origin at 45 degrees goes through three cells: (3,3), (4,4), and (5,5).
Rex Kerr
@honk: Ah, now I understand what's going on. I didn't understand the geometry before. The quad-tree answer still stands, though, if you're really after nearest-neighbor searches and not a solution to this particular problem.
Rex Kerr
@Rex ok, I see, in that case I don't care which one to pick.
honk
@Rex: Took some time to find out how useful quadtres's are. Thanks for sharing.
honk