Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I should be able to figure this out, but my brain isn't cooperating with me. Say I have a grid of 5x5, with each cell in the grid numbered from 0-24, going from left to right. Given a cell number such as 17, how do I determine that cell's x and y coordinates? I've been able to do it vice versa, where if given the coordinates I can calculate cell number:

$Cell number=x+y*width$

(The x and y coordinates represent the upper left point of each cell)

But now I want the opposite. Any ideas?

share|improve this question
    
Note that you have 25 cells and 26 numbers. Do you start from 0 or 1? There are slight changes to the formula depending on the base. –  Ross Millikan Apr 21 '11 at 23:36
    
@Ross: I hadn't noticed this! My answer below is for the first square being 1 as opposed to 0. It's not a big change, just an aesthetic one. –  mixedmath Apr 21 '11 at 23:43
    
@mixedmath: your answer goes from (0,0) to (4,4) for values from 0 to 24 (nothing can give a row or column of 5 in base 5). You are correct, the changes are small (some +1's and -1's here and there). Maybe OP will let us know what s/he wants. –  Ross Millikan Apr 22 '11 at 0:20
    
@Ross: yes, it is very odd that I index my rows at 0 and my columns at 1. I suppose I should really stay consistent, and so consider the number $n-1$. Thus $17-1 = 16$ can be written as $(31)_5$, indicating the 4th row and the 2nd column. At least this way, both index at 0, so I really would start from $(0,0)$ and go to $(4,4)$. –  mixedmath Apr 22 '11 at 0:25
add comment

2 Answers 2

up vote 2 down vote accepted

Imagine you are writing the number in base (length or width). So here, we want to write the number in base 5. 17 can be written as $(32)_5$. But then this is all we need, as this says it's in the 3rd row, 2nd column.

The idea is that our 'index' goes up by 5 for every row we go down. Thus 1 will be the top left square, 1 + 5 = 6 will be the square below it, and so on. But this has a convenient notation base-5. 1 is written as $(01)_5$ in base 5, and 6 is written as $(11)_5$ in base 5 (I now let the subscript tell the base). In this way, we can see that the first digit tells which row we are in, and the second digit tells which column. So 11, which is written as $(21)_5$, is below $(11)_5 = 6$ and to the left of $(12)_5 = 7$.

This is actually the same process as the other answer, but it has the additional benefit of having all the uniqueness arguments that go along with bases. And I think the representation is very cute.

share|improve this answer
    
....wait, what? –  maq Apr 21 '11 at 23:27
    
@mohabitar: I have edited my comment to include a better response and explanation –  mixedmath Apr 21 '11 at 23:32
    
@mohabitar: Are you familiar with writing numbers in different bases? As opposed to base 10, for instance? –  mixedmath Apr 21 '11 at 23:37
    
mohabitar, are you familiar with long division? If yes, mixed's answer can be written the follwoing way: Divide your number by 5, the possition is (reminder, quotient). For example 17:3 =5 reminder 2; which means the possition is (2,5). This is if your counting starts at 0. –  N. S. Apr 21 '11 at 23:49
    
If you are not familiar with long division, here is how u can get around: x coordinate= [fractional part of number/5]*5 ; y coordinate = [integral part of number/5]. i.e. 17/5= 3.4 means x= .4*5=2 ; y = 3. Again, it works if you start counting at 0. –  N. S. Apr 21 '11 at 23:54
add comment

mohabitar, your equation for determining cell_number from x=column and y=row, works if you define the first row = 0 and the first column = 0 AND define your coordinate system such that y increases to the right and x increases down (or have cells 0-4 on the bottom row and work up).

Keeping with the reference system which you imply, you can extract x and y from cell_number as follows:

y = INT(cell_number/width)

x = MOD(cell_number/width)

where INT(z) returns the highest integer value which is equal to or smaller than z; and MOD(z) returns the modulus of z, which is defined as (z - INT(z)).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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