formula to locat a specific index in an ordered list given any coordinate pair within predefined limitations

42 Views Asked by At

(i posted this in the wrong stack exchange originally and edited it there so sorry if it sounds like this is a double post or something >_<)

Apologies if im not that good at phrasing questions D:

first of all, regarding a working example, i had something i THOUGHT worked, but only worked when the total number of rooms was 100 (99 if you index at 0);

function coordsToArray(xCoord,zCoord){
var tempLoc : String = zCoord + "" + xCoord;

return int.Parse(tempLoc);
}

basically it takes the xcoord and zcoord and returns them as a string togeather. but alas, this didn't work as planned. (i.e. if you input, an xcoord of 4,4 (remember, indexed at 0), you'd want 25 back, however with this, you'd get 44...which is wrong obviously)

so heres a hopefully better explanation of the problem at hand.

assume i have an array, filled with say, 24 objects, numbered 0-23;

each of those values represents a point on a grid, that is say , 6 total units across (indexed at 0 would make 5 the farthest right coordinate), by 4 total units up(indexed at 0 would make 3 the farthest up). (all of them are positive coordinates)

the way the array is filled, is that , each row of X coordinates is added one row at a time, therefore this particular grid structure would end up looking like:

(v = down arrow pointing to coordinate pair)

0,1,2,3,4,5,6,7,8
(0,0)(1,0)(2,0)(3,0)(4,0)(5,0)next row(0,1)(1,1)(1,2) ect ect ect...

each coordinate pair is stored as the object it represents so you couldn't just ask for item 1,3 for example you'd have to reference it by index...

i need to be able to find the index of any given coordinate pair, and return the value located at that index. for example, i need to input, (x,z) and have it spit out a number telling me the index of (x,z) is, within that array.

i've currently got nothing that works at all, so any help would be appreciated!

and thanks for your time!

1

There are 1 best solutions below

2
On BEST ANSWER

Suppose you have an array with $m$ rows and $n$ columns (for the moment, let's assume everything is 1-indexed not 0-indexed). Then, to get the $ij^{\text{th}}$ coordinate (in sequence) the formula would be: $(i-1)\cdot n + j$. The reasoning is that each row contains $n$ elements; if you are in row $i$ then you have $i-1$ full rows with $n$ elements each, then $j$ elements in that last partial row.

You follow the same procedure if your $i$ and $j$ are $0$-indexed (and the entries are $0$-indexed). If you are in entry $ij$, you have $i$ rows full now, row $0$, row $1$, ... row $i-1$. Each of those rows has $n$ elements. So there are $i\cdot n$ elements seen, then you have $j+1$ elements seen in the row you are in, column $0$, column $1$, ..., column $j$ for $j+1$ elements. The total number of elements you've seen then is $i\cdot n + j+1$, but that assumes your entries started at $1$. To correct, just subtract off the $1$. The $0$-indexed count then is just $i\cdot n + j$.