# Finding Coordinates on a grid given X.

This topic is 4873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok... I have a BIIIIG BIIIIG Trouble.... I have A grid. WIth X columns and X lines. An Exmaple: 8 Columns, 2 columns. And evevry field is consecutively numbered. For example: This should be grid 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 So. If i Only know the amount of Columns and Line, and someone comes and tells me, Please, Find the coordinates for number 12 on the grid. How can i Find them? Ok. You se the grid and sya "Its simple, Column 4, Line 2. But you cant's see the grid. You only know that has 8 Columns and 2 lines. and you need to find number 12 I need to find a formula that works with any grid, no matter the amount of columns and lines. HELP?? :'(

##### Share on other sites
Quote:
 I have A grid. WIth X columns and X lines.

What do you mean by this???

But I think you need to divide the number (12) by the number of columns to get which column it is on. Then take the mod to get which one it is.

But try to rephrase your question.

Edit: Fixed the [ quote ] tags. :)

[Edited by - ender7771 on September 14, 2005 8:04:39 PM]

##### Share on other sites
this method works, but you should use terminology like x/y columns/rows. it'll save some confusion :)

##### Share on other sites
Ok
I know it's complicated
But it's as complicated lso to formulate the quesdtion..
butr you got it right..
I just got how to get the column position...
but..

How do I find the LIne?

so

12 is on column 4 because 12 mod 8 = 4

But the line?

:S

P:.S: SORRY. I MEANT X, Y

##### Share on other sites
Ok

This is why i mneed these

I have a class. Wich receives as parameters, the number of columns and lines of the SPRITE SHEET.

So. It knows that it has (columns * lines ) sprites. The first one is on 1,1 and the last one is on columns, lines. But theres a class thaat tells this class, wich frame it needs by passing it only the number.

So. The first class must find this frame number by only haiving those 3 parameters. Columns, LInes and number of frame it need.

I already know how to find the column. Number mod Columns.

But now i just need the Line in wich is it..

Any Help¿?¿¿

##### Share on other sites
something like this might work...

int offset(const int& row, const int& col)
{
return row * (nCellsPerRow + col);
}

9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8

12 = row: 2, col: 4...

plug row-2 and col-4 into the function you get 12

is that what you mean?

##### Share on other sites
row = ct div width
column = ct mod width

where div is C/C++ integer division (floor of the real value)

Since indices are almost certainly zero-based, this will work. A truer representation of how things will work is as follows:

[00] [01] [02] [03] [04] [05] [06] [07]
[08] [09] [10] [11] [12] [13] [14] [15]

Thus, 12 div 8 is index 1, indicating row 2, and 12 mod 8 is index 4, indicating column 5.

To put it in C++ terms:

int field = 12;
int row = field / grid_width;
int col = field % grid_height;

Hope that helps,
Twilight Dragon

[Edited by - TDragon on September 14, 2005 7:16:30 AM]

##### Share on other sites
nOPE. I never ereceive the Column and the Row.

and with that i have to find it.

As i Said.
I got the part to find the column

Number MOD columns (if it's 0, is the last column)

How can I find the Row number by now having, Columns, Lines, and column position of teh number??

##### Share on other sites
I think this will work to find the row which the number is on,

ceiling(number/columns) //assuming your first row is row 1

##### Share on other sites

row = floor(num / rowWidth)
col = num - (row * rowWidth)

Or am I understanding this wrong..?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 12
• 15
• 14
• 46
• 22
• ### Forum Statistics

• Total Topics
634054
• Total Posts
3015275
×