0

(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!

  • also sorry for the likely un-related tag, couldn't for the life of me find something to put there that actually made any sense D: wanted to put coordinates,formula ect but no dice so sorry >_< – user2620255 Apr 11 '15 at 00:49
  • Perhaps a programming question is better suited for stackoverflow? – TravisJ Apr 11 '15 at 00:57
  • I thought the same thing, but im really just looking for a formula, that can convert two numbers into one based on a set of predefined data, which i figured would be better categorized as a maths questions. since the answer wouldn't nessecarily be specific to programming :O but if you really think i should post it there i can do that instead! – user2620255 Apr 11 '15 at 01:03
  • I'll read again... – TravisJ Apr 11 '15 at 01:14
  • I tried to give the formula with explanation, is this what you were looking for? – TravisJ Apr 11 '15 at 01:23

1 Answers1

1

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$.

TravisJ
  • 7,426
  • $m$ is the number of horizontal rows and $n$ is the number of vertical columns. $ij$ means look in row $i$ (from top, but that doesn't really matter) and column $j$. Looking at the grid, the $i$ coordinate is like a $y$ value where $y=1$ is the top and $y=m$ is the bottom. The $j$ coordinate works as one would expect the $x$ coordinate to work. – TravisJ Apr 11 '15 at 01:35
  • OK....I am just terribly slow! haha i fully understand what you are saying now and it basically makes complete sense. you count the number of rows that would be considdered passed/tallied/counted,accounted for, or whatever. then you add up the current distance into that row. (sorry i butcherd your wonderful explanation!) thank you kind sir you have really helped me out! (the answer really was kind of simple XD im surprised i didn't figure it out myself)..anywho wonderful job and thank you so much! – user2620255 Apr 11 '15 at 01:43