0
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ Hi and welcome to the site! Since this is a site that encourages and helps with learning, it is best if you show your own ideas and efforts in solving the question. Can you edit your question to add your thoughts and ideas about it? Don't worry if it's wrong - that's what we're here for. $\endgroup$ – 5xum Mar 5 at 9:29
  • 1
    $\begingroup$ Also, don't get discouraged by the downvote. I downvoted the question and voted to close it because at the moment, it is not up to site standards (you have shown no work you did on your own). If you edit your question so that you show what you tried and how far you got, I will not only remove the downvote, I will add an upvote. $\endgroup$ – 5xum Mar 5 at 9:29
  • $\begingroup$ @5xum Hi, I'm tried to add some my thoughts, sorry fom my bad english. $\endgroup$ – evaee Mar 5 at 9:37
  • $\begingroup$ "Numbering cells inside the grid is random": how do you know the number of a cell ? $\endgroup$ – Yves Daoust Mar 5 at 10:02
  • $\begingroup$ @YvesDaoust I mean that the number of cells may be random, from -100 to 100 for example, but I knew them, they given me as a matrix (2d array). $\endgroup$ – evaee Mar 5 at 10:04
0
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ I' so sorry, but S it's what? Area of a square? $\endgroup$ – evaee Mar 5 at 10:10
  • $\begingroup$ @evaee: no, size. $\endgroup$ – Yves Daoust Mar 5 at 10:11
  • $\begingroup$ So, for ex, my square is 10x10, and point has coords (7;13), top left square coord is (5,5), than I get for x = (point_x_coord - square_top_left_point_x) => (7 - 5) = 2, and y the same => (13 - 5), here we have (2;8) => that take (2 / width or size) * 2? $\endgroup$ – evaee Mar 5 at 10:19
  • $\begingroup$ @evaee: (7 / 10, 13 / 10) = (0, 1), assuming that the top-left square has its top left corner at the origin, like on your figure and unlike in your comment. $\endgroup$ – Yves Daoust Mar 5 at 10:21
  • $\begingroup$ But what if point has negative coordinates? $\endgroup$ – evaee Mar 5 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.