Which cell in a grid a point belongs to?

772 Views Asked by At

I have a square which is divided into an NxN grid and I have a dot. How I can find which cell this point belongs to? Assuming I know the boundaries of the square.

How to find the point belongs to the 3rd cell? Numbering cells inside the grid is random. Okay, I think to get number of cell (if takes variant from the example), it's 3 number of cell and divide it by 10 and than multiply by 2. The formula is y = floor( $\frac{cell number}{width}*2 $) and do it for each cell, but what the next step?

1

There are 1 best solutions below

8
On BEST ANSWER

For a point at (X, Y), the cell indexes are (X / S, Y / S), using integer division (if the numbers aren't integers, take the floor).

From the cell index, lookup the array of numbers.

This method will associate every grid cell its top and left edge.