Hello!! I hope you guys are having such a good day.
Ok let's get to it.
Maybe this is a dumb question, but I still don't get it.
Let's say I create a 4x4 array. Which I filled with some increasing numbers... like this:
1... 2... 3... 4...
5... 6... 7... 8...
9... 10... 11... 12...
13... 14... 15... 16...
What I need to do is: given the number of the element (1...16) find the xy coordinates of such element.
Like this:
Let's say I want to find the xy coordinates of the 11 element.
Those coordinates are (X = 3, Y = 3). (X stars on 1, Y stars on 1).
No idea how to do it.
However, I know how to find the element number given the xy coordinates:
Just follow the next formula:
Number = X + (Y-1)*4
Like if I want to find the element number given the X=4, Y=2 coordinates.
Number = 4 + (2-1)*4
Number = 4 + (1)*4
Number = 8
X=2, Y=3
Number = 2 + (3-1)*4
Number = 2 + (2)*4
Number = 10
I just don't know how to do the inverse function, finding xy coordinates given the element number...
Maybe it's too easy, but I jut can't find it out.
Thanks in advance guys!
And... very sorry for my bad english.
If I understand correctly, you have a one dimensional array and want to use an x and y coordinate on it as if it were a two dimensional array? I think this is what you want:
array[y * cols + x]
"cols" is the number of columns in the 2D representation of the array.
EDIT: stupid me, I didn't read the question at all. You want to get the x and y coordinates from a position in the index, which is a bit different. Here's how you do that:
y = ind/cols
x = ind%cols
"ind" is the index of the array you are converting to xy coordinates.
"cols" is the number of columns in the 2D representation of the array.
EDIT: remember that y, x, and the array index should start from 0, not 1
You were right!!
Thank you so, so much!